Windows Scripting Forums

Full Version: [SOLVED] Need batch file to reset dual core affinity..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

platinumsteel

Is its possible to write a batch file that executes an game.exe that wud change the process affinity to the 1st core only then back to 2nd core only then back to both cores as normal?There is a game that needs that kind of weird tweak to work flawlessly in vista and win7.Please let me know if that can be accomplished..thanks..
In general, it's a bad idea to choose your own affinity. The CPU scheduler will often do a better job than you will and this can result in performance loss.

However, if you're sure of your situation there are two different ways to launch a program from batch with a specific affinity. The first uses NT Batch's start command, the second uses Microsoft SysInternals' psexec program. Either one will work.
    NTBATCH Programming
  1. start /affinity 1 program.exe
  2. start /affinity 2 program.exe

or
    BATCH Programming
  1. psexec -a 1 program.exe
  2. psexec -a 2 program.exe

well i am sure of my situation but the thing is i have no experience with creating batch files and really would like u to help me and explain in detail and from scratch what i would need to do.....Meanwhile I would check out the psexec program from Microsoft and see if i can understand it and get started...thanks
A batch file is just a simple text file that lists a series of command lines to be executed in order. You can create one in Notepad and save it with a .bat file extension.
Ok well I was able to understand what a batch file is.But my problem really is too put in the correct commands for it to work..The name of the game on the desktop icon is GTR Evolution,but in the program directory for the game I see ''RACE07'' which is the .exe that actually opens the game up...But I don't understand what am i to put in notepad and save it as .bat...Am I to just put in notepad:
start /affinity 1 program.exe
start /affinity 2 program.exe
close of and save it as a .bat extension file?I am new to this and would really like some more detailed information ok..thank you
I'm not sure what you're trying to do.
Ok i would explain again.I have a game that i would like the affinity to be adjusted or reset so that the game would play good on win7 or vista.I am hoping that when i launch the game the batch command would switch of core 1 apply it...then switch off core 2 apply it then finally switch back to all cores or all processors...as long as the cores are switched on and off or reset i think I would be good...Please let me know if that can be accomplished.thanks...
In standard scripting, there is no method of changing the affinity of a running process. That means that both batch and VBScript are out as options. There is however, a method provided by a .Net class that will allow you to change the affinity of a running process; and luckily for us, it's exposed to PowerShell, the next generation scripting technology available in both Windows Vista and Windows 7. To begin, you need to grab the current running process' instance using the Get-Process cmdlet.
Code:
$game = Get-Process -name race07
Here, race07 is the name of the game's process as you see it listed in Task Manager.

Once you have an object reference for the process, you can change its processor affinity using the ProcessorAffinity property of the System.Diagnostics.Process (the class object that was returned by the Get-Process cmdlet) .Net class. If you don't know what all of that means, that's ok. You can ignore the over speak and look at the example below.
Code:
$game.ProcessorAffinity = 0x0001
In the above example, we change the property to have a value of 0x0001. The hex value of 1 represents the first processor. Incidentally, the hex value 0x0002 represents the second processor. So to switch it to the second processor, you add this line:
Code:
$game.ProcessorAffinity = 0x0002
Here's where it gets slightly confusing. To switch the affinity to both processors, you will use the hex value 0x0003.
Code:
$game.ProcessorAffinity = 0x0003
While it looks like the two values are added together, it's actually a bitmask value that is the result of a bitwise conjunction. (0x0001 AND 0x0002 = 0x0003) Here again, bitwise calculations are far beyond the scope of this post. Just trust me that that's what's really going on behind the scenes.

Phew! Ok, so let's turn this jumbled mess into a script already! Open up notepad or some other simple text editor and add the following lines.
Code:
$game = Get-Process -name race07
$game.ProcessorAffinity = 0x0001
$game.ProcessorAffinity = 0x0002
$game.ProcessorAffinity = 0x0003
Now save your text file with a .PS1 extension (the default extension for Windows PowerShell scripts) and you're all set. Once your game is running, just double click your script file to run it and it should go through the motions you described.

Sorry it's not a batch script, but it gets the job done!
Why do you have to grab the game's instance? Why can't you just run the affinities?
well thanks for this info...when i re-install back to windows 7.i woud put this into action..Thanks again ok.
Pages: 1 2
Reference URL's