AutoHotkey Community

It is currently May 26th, 2012, 9:15 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: April 22nd, 2007, 5:41 pm 
Offline

Joined: February 24th, 2007, 6:02 pm
Posts: 175
Location: Budapest, Hungary
ProcessInfo.ahk - Function library to retrieve various application process informations:


Code:
; 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
}


Last edited by HuBa on April 26th, 2007, 9:27 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 22nd, 2007, 5:56 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
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.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 22nd, 2007, 10:00 pm 
Offline

Joined: February 24th, 2007, 6:02 pm
Posts: 175
Location: Budapest, Hungary
Good ideas.

I usually upload the scripts to my AHK file storage and also supply them with headers.
In this case: http://www.autohotkey.net/~HuBa/ProcessInfo.ahk

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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 26th, 2007, 8:53 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Quote:
Full filename by process ID (GetModuleFileNameEx() function)
thx for this function. This is what I needed for such a long time.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 26th, 2007, 9:32 pm 
Offline

Joined: February 24th, 2007, 6:02 pm
Posts: 175
Location: Budapest, Hungary
Tuncay wrote:
Quote:
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 27th, 2009, 2:23 pm 
Offline

Joined: July 15th, 2005, 3:19 pm
Posts: 140
Location: Denmark
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 27th, 2009, 3:14 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Once you find the fullpath of a process, you may use FileGetVersionInfo() to retrieve the Description


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 27th, 2009, 3:21 pm 
Offline

Joined: July 15th, 2005, 3:19 pm
Posts: 140
Location: Denmark
Thanks SKAN!


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 16th, 2010, 8:09 am 
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:

Code:
  DllCall("CloseHandle", hProcess)


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

Code:
  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!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 16th, 2010, 8:35 am 
GregL wrote:
Re: Small fix to GetModuleFileNameEx(ProcessID) needed

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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: May 19th, 2010, 8:34 am 
Offline

Joined: May 31st, 2008, 3:22 pm
Posts: 47
how use that? coz i see only bunch of lines and noone example. not easy understand

need someting like that

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 19th, 2010, 2:07 pm 
Offline

Joined: February 2nd, 2008, 4:35 am
Posts: 643
I have tried to make the library a bit more user-friendly... :)
Code:
; 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", 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
}
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 16th, 2010, 7:51 pm 
Hi, i have problem with GetModuleFileNameEx(ProcessID)

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


It isnt correct path.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 29th, 2012, 10:56 am 
Offline

Joined: January 23rd, 2012, 9:13 am
Posts: 18
Note that a_h_k's version lacks gregL's fix (see above).


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group