 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
HuBa
Joined: 24 Feb 2007 Posts: 175 Location: Budapest, Hungary
|
Posted: Sun Apr 22, 2007 4:41 pm Post subject: Parent ProcessID, ProcessName, ProcessThreadCount |
|
|
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 Thu Apr 26, 2007 8:27 pm; edited 2 times in total |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 4511 Location: Belgrade
|
Posted: Sun Apr 22, 2007 4:56 pm Post subject: |
|
|
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. _________________
 |
|
| Back to top |
|
 |
HuBa
Joined: 24 Feb 2007 Posts: 175 Location: Budapest, Hungary
|
Posted: Sun Apr 22, 2007 9:00 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Thu Apr 26, 2007 7:53 pm Post subject: |
|
|
| Quote: | | Full filename by process ID (GetModuleFileNameEx() function) | thx for this function. This is what I needed for such a long time. |
|
| Back to top |
|
 |
HuBa
Joined: 24 Feb 2007 Posts: 175 Location: Budapest, Hungary
|
Posted: Thu Apr 26, 2007 8:32 pm Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
David Andersen
Joined: 15 Jul 2005 Posts: 140 Location: Denmark
|
Posted: Mon Jul 27, 2009 1:23 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Mon Jul 27, 2009 2:14 pm Post subject: |
|
|
| Once you find the fullpath of a process, you may use FileGetVersionInfo() to retrieve the Description |
|
| Back to top |
|
 |
David Andersen
Joined: 15 Jul 2005 Posts: 140 Location: Denmark
|
Posted: Mon Jul 27, 2009 2:21 pm Post subject: |
|
|
| Thanks SKAN! |
|
| Back to top |
|
 |
GregL Guest
|
Posted: Tue Mar 16, 2010 7:09 am Post subject: Small fix to GetModuleFileNameEx(ProcessID) needed |
|
|
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! |
|
| Back to top |
|
 |
Moderator! Guest
|
Posted: Tue Mar 16, 2010 7:35 am Post subject: |
|
|
| GregL wrote: | | Re: Small fix to GetModuleFileNameEx(ProcessID) needed |
thanks for notifying it. the shimanov's posts have been fixed. |
|
| Back to top |
|
 |
ahkiot0
Joined: 31 May 2008 Posts: 36
|
Posted: Wed May 19, 2010 7:34 am Post subject: |
|
|
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 |
|
|
| Back to top |
|
 |
a_h_k
Joined: 02 Feb 2008 Posts: 626
|
Posted: Wed May 19, 2010 1:07 pm Post subject: |
|
|
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
}
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
| Back to top |
|
 |
ATom1234 Guest
|
Posted: Thu Sep 16, 2010 6:51 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|