|
#Requires AutoHotkey v2.0 |
|
#SingleInstance Force |
|
DetectHiddenWindows true |
|
|
|
global IsProcessSuspended := false |
|
global IntentionalExit := false |
|
global Debug := false |
|
StateFile := A_Temp "\AutoPauseResume.state" |
|
|
|
OnExit(Cleanup) |
|
|
|
if FileExist(StateFile) |
|
FileDelete(StateFile) |
|
|
|
LockFile := A_Temp "\AutoPauseResume.lock" |
|
if FileExist(LockFile) |
|
FileDelete(LockFile) |
|
|
|
ExcludedProcesses := Map( |
|
"explorer.exe", true, |
|
"csrss.exe", true, |
|
"winlogon.exe", true, |
|
"svchost.exe", true, |
|
"dwm.exe", true, |
|
"cmd.exe", true, |
|
"chrome.exe", true, |
|
"powershell.exe", true, |
|
"sublime_text.exe", true, |
|
"sublime_merge.exe", true, |
|
"steam.exe", true, |
|
"steamwebhelper.exe", true, |
|
"openconsole.exe", true, |
|
"windowsterminal.exe", true, |
|
"playnite.desktopapp.exe", true, |
|
"playnite.fullscreenapp.exe", true, |
|
) |
|
|
|
if !WinExist("A") |
|
{ |
|
if Debug |
|
MsgBox "No active window detected. Please focus a window to suspend." |
|
ExitApp |
|
} |
|
|
|
pid := WinGetPID("A") |
|
if !pid |
|
{ |
|
if Debug |
|
MsgBox "Failed to get the active window's PID." |
|
ExitApp |
|
} |
|
|
|
processName := WinGetProcessName("ahk_pid " pid) |
|
if !processName |
|
{ |
|
if Debug |
|
MsgBox "Failed to retrieve the active window's process name." |
|
ExitApp |
|
} |
|
|
|
if ExcludedProcesses.Has(StrLower(processName)) |
|
ExitApp |
|
|
|
WinMinimize("ahk_pid " pid) |
|
Sleep(200) |
|
if !CallNtProcessFunc(pid, "NtSuspendProcess") |
|
{ |
|
if Debug |
|
MsgBox "Failed to suspend process (PID: " pid ")." |
|
ExitApp |
|
} |
|
IsProcessSuspended := true |
|
|
|
FileAppend(pid "|" processName, StateFile) |
|
|
|
IntentionalExit := true |
|
ExitApp |
|
|
|
CallNtProcessFunc(pid, funcName) |
|
{ |
|
PROCESS_SUSPEND_RESUME := 0x0800 |
|
hProc := DllCall("OpenProcess", "UInt", PROCESS_SUSPEND_RESUME, "Int", 0, "UInt", pid, "Ptr") |
|
if !hProc |
|
return false |
|
|
|
hNtdll := DllCall("GetModuleHandle", "Str", "ntdll.dll", "Ptr") |
|
if !hNtdll |
|
{ |
|
DllCall("CloseHandle", "Ptr", hProc) |
|
return false |
|
} |
|
|
|
pFunc := DllCall("GetProcAddress", "Ptr", hNtdll, "AStr", funcName, "Ptr") |
|
if !pFunc |
|
{ |
|
DllCall("CloseHandle", "Ptr", hProc) |
|
return false |
|
} |
|
|
|
DllCall(pFunc, "Ptr", hProc) |
|
DllCall("CloseHandle", "Ptr", hProc) |
|
return true |
|
} |
|
|
|
Cleanup(exitReason, exitCode) |
|
{ |
|
global pid, IsProcessSuspended, IntentionalExit, StateFile |
|
if IsProcessSuspended && !IntentionalExit |
|
{ |
|
CallNtProcessFunc(pid, "NtResumeProcess") |
|
if FileExist(StateFile) |
|
FileDelete(StateFile) |
|
} |
|
} |