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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

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

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
Contact:

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

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")
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, Rohwedder and 242 guests