Any way to get the PID of a child process from a CMD.exe PID?

Get help with using AutoHotkey and its commands and hotkeys
SvenBent
Posts: 260
Joined: 09 Aug 2015, 01:34

Any way to get the PID of a child process from a CMD.exe PID?

Post by SvenBent » 20 Mar 2017, 23:40

If i have the PID of a CMD.exe running on the system. Is there a way to find the PID of the process that runs inside that CLI box ?
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Any way to get the PID of a child process from a CMD.exe PID?

Post by qwerty12 » 21 Mar 2017, 05:47

If you can attach to the cmd.exe console:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

scriptPID := DllCall("GetCurrentProcessId", "UInt")
Process, Exist, cmd.exe
targetCmdPID := ErrorLevel
if (targetCmdPID && DllCall("AttachConsole", "UInt", targetCmdPID)) {
	processCount := 10
	Loop {
		VarSetCapacity(processList, processCount * 4, 0)
		actualCapacity := DllCall("GetConsoleProcessList", "Ptr", &processList, "UInt", processCount, "UInt") ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms683170(v=vs.85).aspx
		if (!actualCapacity) {
			break
		} else if (actualCapacity > processCount) {
			processCount := actualCapacity
			continue
		}
		Loop %actualCapacity% {
			dwPID := NumGet(processList, (A_Index - 1) * 4, "UInt")
			if (dwPID == scriptPID || dwPID == targetCmdPID)
				continue
			MsgBox % dwPID
		}
		break
	}
	DllCall("FreeConsole")
}
Post Reply

Return to “Ask For Help”