Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Parent ProcessID, ProcessName, ProcessThreadCount


  • Please log in to reply
17 replies to this topic
HuBa
  • Members
  • 175 posts
  • Last active: Feb 13 2012 09:51 AM
  • Joined: 24 Feb 2007
ProcessInfo.ahk - Function library to retrieve various application process informations:
[*:dbje01cx]Script's own process identifier
[*:dbje01cx] Parent process ID of a process (the caller application)
[*:dbje01cx] Process name by process ID (filename without path)
[*:dbje01cx] Thread count by process ID (number of threads created by process)
[*:dbje01cx] Full filename by process ID (Modified version of shimanov's GetModuleFileNameEx() function)
; ProcessInfo.ahk - Function library to retrieve various application process informations:
; - Script's own process identifier
; - Parent process ID of a process (the caller application)
; - Process name by process ID (filename without path)
; - Thread count by process ID (number of threads created by process)
; - Full filename by process ID (GetModuleFileNameEx() function)
;
; Tested with AutoHotkey 1.0.46.10
;
; Created by HuBa
; Contact: http://www.autohotkey.com/forum/profile.php?mode=viewprofile&u=4693
;
; Portions of the script are based upon the GetProcessList() function by wOxxOm
; (http://www.autohotkey.com/forum/viewtopic.php?p=65983#65983)

GetCurrentProcessID()
{
  Return DllCall("GetCurrentProcessId")  ; http://msdn2.microsoft.com/ms683180.aspx
}

GetCurrentParentProcessID()
{
  Return GetParentProcessID(GetCurrentProcessID())
}

GetProcessName(ProcessID)
{
  Return GetProcessInformation(ProcessID, "Str", 260, 36)  ; TCHAR szExeFile[MAX_PATH]
}

GetParentProcessID(ProcessID)
{
  Return GetProcessInformation(ProcessID, "UInt *", 4, 24)  ; DWORD th32ParentProcessID
}

GetProcessThreadCount(ProcessID)
{
  Return GetProcessInformation(ProcessID, "UInt *", 4, 20)  ; DWORD cntThreads
}

GetProcessInformation(ProcessID, CallVariableType, VariableCapacity, DataOffset)
{
  hSnapshot := DLLCall("CreateToolhelp32Snapshot", "UInt", 2, "UInt", 0)  ; TH32CS_SNAPPROCESS = 2
  if (hSnapshot >= 0)
  {
    VarSetCapacity(PE32, 304, 0)  ; PROCESSENTRY32 structure -> http://msdn2.microsoft.com/ms684839.aspx
    DllCall("ntdll.dll\RtlFillMemoryUlong", "UInt", &PE32, "UInt", 4, "UInt", 304)  ; Set dwSize
    VarSetCapacity(th32ProcessID, 4, 0)
    if (DllCall("Process32First", "UInt", hSnapshot, "UInt", &PE32))  ; http://msdn2.microsoft.com/ms684834.aspx
      Loop
      {
        DllCall("RtlMoveMemory", "UInt *", th32ProcessID, "UInt", &PE32 + 8, "UInt", 4)  ; http://msdn2.microsoft.com/ms803004.aspx
        if (ProcessID = th32ProcessID)
        {
          VarSetCapacity(th32DataEntry, VariableCapacity, 0)
          DllCall("RtlMoveMemory", CallVariableType, th32DataEntry, "UInt", &PE32 + DataOffset, "UInt", VariableCapacity)
          DllCall("CloseHandle", "UInt", hSnapshot)  ; http://msdn2.microsoft.com/ms724211.aspx
          Return th32DataEntry  ; Process data found
        }
        if not DllCall("Process32Next", "UInt", hSnapshot, "UInt", &PE32)  ; http://msdn2.microsoft.com/ms684836.aspx
          Break
      }
    DllCall("CloseHandle", "UInt", hSnapshot)
  }
  Return  ; Cannot find process
}

GetModuleFileNameEx(ProcessID)  ; modified version of shimanov's function
{
  if A_OSVersion in WIN_95, WIN_98, WIN_ME
    Return GetProcessName(ProcessID)
  
  ; #define PROCESS_VM_READ           (0x0010)
  ; #define PROCESS_QUERY_INFORMATION (0x0400)
  hProcess := DllCall( "OpenProcess", "UInt", 0x10|0x400, "Int", False, "UInt", ProcessID)
  if (ErrorLevel or hProcess = 0)
    Return
  FileNameSize := 260
  VarSetCapacity(ModuleFileName, FileNameSize, 0)
  CallResult := DllCall("Psapi.dll\GetModuleFileNameExA", "UInt", hProcess, "UInt", 0, "Str", ModuleFileName, "UInt", FileNameSize)
  DllCall("CloseHandle", hProcess)
  Return ModuleFileName
}


majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Thx

I think you should getter some more process functions in this library, and also add shimanovs function so we can have 1 place for all of them.

You should also create documentation or at least put the header in the ahk source so we don't have to scan source file for the funcitions but see imediatly what functions do what.
Posted Image

HuBa
  • Members
  • 175 posts
  • Last active: Feb 13 2012 09:51 AM
  • Joined: 24 Feb 2007
Good ideas.

I usually upload the scripts to my AHK file storage and also supply them with headers.
In this case: <!-- m -->https://ahknet.autoh...ProcessInfo.ahk<!-- m -->

I thought it is unnecessary to include the headers in the forum post since you can read it before the code.

I had no big plans with this script, I just posted here to help others who interested in it.

But maybe you are right, it would be useful to make a documentation and include some more functions. Is there any convention at the AHK community that you can show me as a sample (e.g. a script-library)?


The script has been updated, I included the GetModuleFileNameEx function and the header.

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006

Full filename by process ID (GetModuleFileNameEx() function)

thx for this function. This is what I needed for such a long time.

HuBa
  • Members
  • 175 posts
  • Last active: Feb 13 2012 09:51 AM
  • Joined: 24 Feb 2007

Full filename by process ID (GetModuleFileNameEx() function)

thx for this function. This is what I needed for such a long time.

It was available in this topic. But I included in this library to keep these functions together.

David Andersen
  • Members
  • 140 posts
  • Last active: Jun 28 2011 04:54 PM
  • Joined: 15 Jul 2005
Would it also be possible to get the description of the process, like:
"Microsoft Office Word"

This information is available in the Task Manager on Vista, though I do not think it is available under XP.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Once you find the fullpath of a process, you may use FileGetVersionInfo() to retrieve the Description

David Andersen
  • Members
  • 140 posts
  • Last active: Jun 28 2011 04:54 PM
  • Joined: 15 Jul 2005
Thanks SKAN!

GregL
  • Guests
  • Last active:
  • Joined: --
Hey HuBa,

Thanks for compiling this little library, it came in handy for a project I was doing. However, I found a small omission in the GetModuleFileNameEx(ProcessID) function, with the second last line:

DllCall("CloseHandle", hProcess)

You (understandably) forgot to include the type parameter for hProcess, so this should be:

DllCall("CloseHandle", "UInt", hProcess)

Of course, the quotes around UInt are optional.

The consequence was that the handles to the process were never closed, so in my script where I called that function once every second, I saw an avalanche of process handles in Process Explorer that kept getting bigger.

This may be only an issue with the latest version of AutoHotKey that I am using, v1.0.48.05. And I know it is not your function, but I figured I would bring this issue to your attention anyway.

Thanks again!

Moderator!
  • Guests
  • Last active:
  • Joined: --

Re: Small fix to GetModuleFileNameEx(ProcessID) needed

thanks for notifying it. the shimanov's posts have been fixed.

ahkiot0
  • Members
  • 51 posts
  • Last active: Mar 11 2015 12:45 AM
  • Joined: 31 May 2008
how use that? coz i see only bunch of lines and noone example. not easy understand

need someting like that

F1::
WinGet, aI, ID, A
WinGetClass, aC,  A ;i know active class
WinGetTitle, aT, A ;i even know active window title!
bunch of codes
MsgBox %show path of active window%
return


a_h_k
  • Members
  • 685 posts
  • Last active: Sep 28 2015 12:32 AM
  • Joined: 02 Feb 2008
I have tried to make the library a bit more user-friendly... :)
; ProcessInfo.ahk - Function library to retrieve various application process informations:

; - Script's own process identifier

; - Parent process ID of a process (the caller application)

; - Process name by process ID (filename without path)

; - Thread count by process ID (number of threads created by process)

; - Full filename by process ID (GetModuleFileNameEx() function)

;

; Tested with AutoHotkey 1.0.46.10

;

; Created by HuBa

; Contact: http://www.autohotkey.com/forum/profile.php?mode=viewprofile&u=4693

;

; Portions of the script are based upon the GetProcessList() function by wOxxOm

; (http://www.autohotkey.com/forum/viewtopic.php?p=65983#65983)

;

;

; EXAMPLE USAGE:

;

; Press Ctrl-Space to view process details for current window...

/*

  ^Space::

    WinGet, ProcessID, PID, A



    ProcessName := GetProcessName(ProcessID)

    ProcessPathName := GetModuleFileNameEx(ProcessID)



    MsgBox Window's executable name = %ProcessName%

    MsgBox Window's executable pathname = %ProcessPathName%

  Return



  #Include %A_ScriptDir%\ProcessInfo.ahk          ;Place all of below in ProcessInfo.ahk

*/

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

; PID

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

GetCurrentProcessID()

{

  Return DllCall("GetCurrentProcessId")  ; http://msdn2.microsoft.com/ms683180.aspx

} ;........................................................................................

GetCurrentParentProcessID()

{

  Return GetParentProcessID(GetCurrentProcessID())

} ;........................................................................................

GetParentProcessID(ProcessID)

{

  Return GetProcessInformation(ProcessID, "UInt *", 4, 24)  ; DWORD th32ParentProcessID

}

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

; NAME

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

GetProcessName(ProcessID)

{

  Return GetProcessInformation(ProcessID, "Str", 260, 36)  ; TCHAR szExeFile[MAX_PATH]

}

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

; PATHNAME

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

GetModuleFileNameEx(ProcessID)  ; modified version of shimanov's function

{

  if A_OSVersion in WIN_95, WIN_98, WIN_ME

    Return GetProcessName(ProcessID)

 

  ; #define PROCESS_VM_READ           (0x0010)

  ; #define PROCESS_QUERY_INFORMATION (0x0400)

  hProcess := DllCall( "OpenProcess", "UInt", 0x10|0x400, "Int", False, "UInt", ProcessID)

  if (ErrorLevel or hProcess = 0)

    Return

  FileNameSize := 260

  VarSetCapacity(ModuleFileName, FileNameSize, 0)

  CallResult := DllCall("Psapi.dll\GetModuleFileNameExA", "UInt",hProcess, "UInt",0, "Str",ModuleFileName, "UInt",FileNameSize)

  DllCall("CloseHandle", "UInt", hProcess)

  Return ModuleFileName

}

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

; THREAD COUNT

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

GetProcessThreadCount(ProcessID)

{

  Return GetProcessInformation(ProcessID, "UInt *", 4, 20)  ; DWORD cntThreads

}

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

; INFORMATION

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

GetProcessInformation(ProcessID, CallVariableType, VariableCapacity, DataOffset)

{

  hSnapshot := DLLCall("CreateToolhelp32Snapshot", "UInt", 2, "UInt", 0)  ; TH32CS_SNAPPROCESS = 2

  if (hSnapshot >= 0)

  {

    VarSetCapacity(PE32, 304, 0)  ; PROCESSENTRY32 structure -> http://msdn2.microsoft.com/ms684839.aspx

    DllCall("ntdll.dll\RtlFillMemoryUlong", "UInt", &PE32, "UInt", 4, "UInt", 304)  ; Set dwSize

    VarSetCapacity(th32ProcessID, 4, 0)

    if (DllCall("Process32First", "UInt", hSnapshot, "UInt", &PE32))  ; http://msdn2.microsoft.com/ms684834.aspx

      Loop

      {

        DllCall("RtlMoveMemory", "UInt *",th32ProcessID, "UInt",&PE32 + 8, "UInt",4)  ; http://msdn2.microsoft.com/ms803004.aspx

        if (ProcessID = th32ProcessID)

        {

          VarSetCapacity(th32DataEntry, VariableCapacity, 0)

          DllCall("RtlMoveMemory", CallVariableType, th32DataEntry, "UInt", &PE32 + DataOffset, "UInt", VariableCapacity)

          DllCall("CloseHandle", "UInt", hSnapshot)  ; http://msdn2.microsoft.com/ms724211.aspx

          Return th32DataEntry  ; Process data found

        }

        if not DllCall("Process32Next", "UInt", hSnapshot, "UInt", &PE32)  ; http://msdn2.microsoft.com/ms684836.aspx

          Break

      }

    DllCall("CloseHandle", "UInt", hSnapshot)

  }

  Return  ; Cannot find process

}

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



ATom1234
  • Guests
  • Last active:
  • Joined: --
Hi, i have problem with GetModuleFileNameEx(ProcessID)

\SystemRoot\System32\smss.exe
\??\C:\WINDOWS\system32\csrss.exe
\??\C:\WINDOWS\system32\winlogon.exe

It isnt correct path.

step
  • Members
  • 21 posts
  • Last active: Feb 21 2012 11:04 PM
  • Joined: 23 Jan 2012
Note that a_h_k's version lacks gregL's fix (see above).

GodlyCheese
  • Members
  • 719 posts
  • Last active: Nov 11 2014 07:12 PM
  • Joined: 30 Aug 2012
For some unknown reason my tests only return asian characters. I'm running AHK_L on Windows 7 (English.)