Tuesday, January 22, 2008

PSKill: Kills Processes Dead (great for gaming!)

As my computer gets older, playing current generation games on my computer becomes all the more resource intensive. This is why it's important for me to shut down as many nonessential processes to free up RAM and CPU before playing my game.

Unfortunately, most of us probably have a LOT of background programs running, and it's a pain to close them one by one from the taskbar and the system tray. They're even worse when the processes can only be shut down from the Task Manager (CTRL-ALT-DEL), by force ending the process.

PSKill is one of the PSTools Microsoft and Sysinternals released to locally and remotely administer certain Windows functions via the command line. PSKill's purpose is to force the shutdown of open processes.

I use PSKill with Autohotkey to ensure the immediate shutdown of all the processes I don't need, and I keep it all in an autohotkey file called close.ahk. Running the script immediately shuts down the list of processes I've specified.

Please make sure that you know what the process you're shutting down does, as it may cause Windows to crash or behave improperly, or for you to lose unsaved changes.

To use within Autohotkey, just use this script:

Run, C:\Program Files\PSTools\pskill.exe "firefox.exe"
Where "firefox.exe" can be any process you want to kill. I put PSKill in a PSTools folder in Program Files myself. You can do the same or put it anywhere else you want, just change the path.

I also use the following script to clear the icons that might remain in the system tray even after you kill the process. Don't ask me to explain how it works, it just does, and credit goes to a certain Autohotkey forum user's post which I can no longer find. If you read this, dear user, let me know and I'll give you proper credit!
;;;; REMOVE ICONS FROM SYSTRAY;;;;
TrayIcons:
TrayIcons()
Return

TrayIcons(sExeName = "")
{
DetectHiddenWindows, On
idxTB := GetTrayBar()
WinGet, pidTaskbar, PID, ahk_class Shell_TrayWnd

hProc := DllCall("OpenProcess", "Uint", 0x38, "int", 0, "Uint", pidTaskbar)
pRB := DllCall("VirtualAllocEx", "Uint", hProc, "Uint", 0, "Uint", 20, "Uint", 0x1000, "Uint", 0x4)

VarSetCapacity(btn, 20)
VarSetCapacity(nfo, 24)

SendMessage, 0x418, 0, 0, ToolbarWindow32%idxTB%, ahk_class Shell_TrayWnd ; TB_BUTTONCOUNT
max = %errorlevel%

Loop, %max%
{
i := max - A_index
SendMessage, 0x417, i, pRB, ToolbarWindow32%idxTB%, ahk_class Shell_TrayWnd ; TB_GETBUTTON

DllCall("ReadProcessMemory", "Uint", hProc, "Uint", pRB, "Uint", &btn, "Uint", 20, "Uint", 0)

dwData := NumGet(btn,12)

DllCall("ReadProcessMemory", "Uint", hProc, "Uint", dwData, "Uint", &nfo, "Uint", 24, "Uint", 0)

hWnd := NumGet(nfo, 0)

WinGet, pid, PID, ahk_id %hWnd%

ifwinnotexist, ahk_id %hWnd%
{
idx := i+1
;MsgBox, 4, , Delete tray icon %idx%?
;IfMsgBox Yes
deletetrayicon(idx)
}

tmp = index=%a_index%, i=%i%, pid=%pid%`n
strayicons .= tmp

}

DllCall("VirtualFreeEx", "Uint", hProc, "Uint", pRB, "Uint", 0, "Uint", 0x8000)
DllCall("CloseHandle", "Uint", hProc)

Return sTrayIcons
}

DeleteTrayIcon(idx)
{
idxTB := GetTrayBar()
SendMessage, 0x416, idx - 1, 0, ToolbarWindow32%idxTB%, ahk_class Shell_TrayWnd ; TB_DELETEBUTTON
SendMessage, 0x1A, 0, 0, , ahk_class Shell_TrayWnd
}

GetTrayBar()
{
WinGet, ControlList, ControlList, ahk_class Shell_TrayWnd
RegExMatch(ControlList, "(?<=ToolbarWindow32)\d+(?!.*ToolbarWindow32)", nTB)

Loop, %nTB%
{
ControlGet, hWnd, hWnd,, ToolbarWindow32%A_Index%, ahk_class Shell_TrayWnd
hParent := DllCall("GetParent", "Uint", hWnd)
WinGetClass, sClass, ahk_id %hParent%
If (sClass <> "SysPager")
Continue
idxTB := A_Index
Break
}

Return idxTB
}

I've even customized the Steam (the game launcher for games like Half-Life and Team Fortress 2) shortcut to instead run a special steam.ahk script that first runs close.ahk, then runs steam.exe (it also shuts down my two other displays). This way I only have to click one icon to everything.

Next I'll go over the script that turns everything back on after I play the game. Also all automatic!

Related Posts by Categories



Widget by Hoctro | Jack Book

No comments: