Process Explorer

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Process Explorer

13 Jun 2022, 02:59

Thanks for your input teadrinker

An alternative would be the:

Code: Select all

SystemProcessInformation(ProcessID)
{
	static STATUS_SUCCESS              := 0x00000000
	static SYSTEM_PROCESS_INFORMATION  := 0x00000005
	static hModule := DllCall("LoadLibrary", "Str", "ntdll.dll", "Ptr")

	if (DllCall("ntdll\NtQuerySystemInformation", "Int", SYSTEM_PROCESS_INFORMATION, "Ptr", &Buf, "UInt", 0, "UInt*", Size, "UInt") != STATUS_SUCCESS)
	{
		Size := VarSetCapacity(Buf, Size, 0)
		if (DllCall("ntdll\NtQuerySystemInformation", "Int", SYSTEM_PROCESS_INFORMATION, "Ptr", &Buf, "UInt", Size, "UInt*", 0, "UInt") = STATUS_SUCCESS)
		{
			Addr := &Buf, InheritedFromUniqueProcessId := ""
			while (Addr)
			{
				if (NumGet(Addr + 0x0050, "Ptr") = ProcessID)
				{
					InheritedFromUniqueProcessId := NumGet(Addr + 0x0058, "Ptr*")
					break
				}
				if !(NumGet(Addr + 0x0000, "UInt"))
					break
				Addr += NumGet(Addr + 0x0000, "UInt")
			}
			return InheritedFromUniqueProcessId
		}
	}
	return false
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
c7aesa7r
Posts: 209
Joined: 02 Jun 2016, 21:09

Re: Process Explorer

13 Jun 2022, 13:26

teadrinker wrote:
12 Jun 2022, 13:22
You can see here: PROCESSENTRY32
Im using the function to retrieve the ParentProcessId of the browser chrome, as it launch multiple instances
Using comobj it takes around 5sec to find the pid, with your function 500ms, its still a high time, taking in count when you have multiple instances of the browser.

Do you have any other idea to retrieve the 'main' PID of a chrome instance?

Code: Select all

WinGet, BrowserPID, PID, New Tab - Google Chrome
arr := GetPID("chrome.exe")



;-----------------------------------------------------------

Time := (A_TickCount)

For PID in arr["chrome.exe"] {

   If (PID = BrowserPID)
      Continue

   Parent := GetProperty(PID, "ParentProcessId")

}


Time := (A_TickCount - Time)
FileAppend, Time: %Time%`n,*



;-----------------------------------------------------------

Time := (A_TickCount)

For PID in arr["chrome.exe"] {

   If (PID = BrowserPID)
      Continue

   Parent := GetProcessParent(PID)

}

Time := (A_TickCount - Time)
FileAppend, Time: %Time%`n,*
Return



GetPid(proc, proc2:="") {

   ; WTSEnumerateProcessesEx()
   ; https://www.autohotkey.com/boards/viewtopic.php?t=19323

   static hWTSAPI := DllCall("LoadLibrary", "str", "wtsapi32.dll", "ptr")

   If !(DllCall("wtsapi32\WTSEnumerateProcessesEx", "ptr", 0, "uint*", 0, "uint", -2, "ptr*", buf, "uint*", TTL))
      Throw Exception("WTSEnumerateProcessesEx failed", -1)

   addr       := buf
   arr        := {}
   arr[proc]  := []
   If (proc2)
      arr[proc2] := []

   Loop %TTL% {

      ProcessName := StrGet(NumGet(addr+8, "ptr"))

      If (ProcessName = proc) or (ProcessName = proc2) {
         PID      := NumGet(addr+4, "uint")
         ;WinGetTitle, Title, ahk_pid %PID%
         arr[ProcessName][PID]:=1
      }

      addr += 8 + (A_PtrSize * 2)
      
   }

   If !(DllCall("wtsapi32\WTSFreeMemoryEx", "int", 0, "ptr", buf, "uint", TTL))
      Throw Exception("WTSFreeMemoryEx failed", -1)
   
   Return arr

}



GetProperty(Pid, Property) {

   Select = Select %Property% from Win32_Process where ProcessId= %pid%

   For process in ComObjGet("winmgmts:").ExecQuery(Select)
      Return, process[Property]

}



GetProcessParent(PID) {
   static MAX_PATH := 260, TH32CS_SNAPPROCESS := 2
   hSnap := DllCall("CreateToolhelp32Snapshot", "UInt", TH32CS_SNAPPROCESS, "UInt", 0, "Ptr")
   VarSetCapacity(PROCESSENTRY32, sz := 4*7 + A_PtrSize*2 + MAX_PATH << !!A_IsUnicode, 0)
   NumPut(sz, PROCESSENTRY32, "UInt")
   DllCall("Process32First", "Ptr", hSnap, "Ptr", &PROCESSENTRY32)
   Loop {
      if NumGet(PROCESSENTRY32, 4*2, "UInt") = PID {
         ParentPID := NumGet(PROCESSENTRY32, 4*4 + A_PtrSize*2, "UInt")
         break
      }
   } until !DllCall("Process32Next", "Ptr", hSnap, "Ptr", &PROCESSENTRY32)
   DllCall("CloseHandle", "Ptr", hSnap)
   Return ParentPID
}

teadrinker
Posts: 4364
Joined: 29 Mar 2015, 09:41
Contact:

Re: Process Explorer

13 Jun 2022, 21:54

For me this always returns the main PID:

Code: Select all

WinGet, PID, PID, ahk_class Chrome_WidgetWin_1
MsgBox, % PID
c7aesa7r
Posts: 209
Joined: 02 Jun 2016, 21:09

Re: Process Explorer

14 Jun 2022, 17:25

teadrinker wrote:
13 Jun 2022, 21:54
For me this always returns the main PID:

Code: Select all

WinGet, PID, PID, ahk_class Chrome_WidgetWin_1
MsgBox, % PID
And if you have multiple instances of chrome? how you would identify the main pid?
teadrinker
Posts: 4364
Joined: 29 Mar 2015, 09:41
Contact:

Re: Process Explorer

14 Jun 2022, 18:11

The main pid is the same for all Chrome windows, so my previouse code returns the correct result. Unless it would be worth adding ahk_exe chrome.exe.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: KruschenZ and 55 guests