AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Easier obtaining process list (and works on Vista)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
pvi



Joined: 31 Mar 2008
Posts: 6

PostPosted: Mon Mar 31, 2008 7:30 pm    Post subject: Easier obtaining process list (and works on Vista) Reply with quote

I wanted to obtain a list of processes to search for the process id's of a specific running processes. So I used example 4 in the help of the Process command to obtain this. This all worked ok on XP, but somehow I was not able to retrieve the names of all processes with the DLLCall OpenProcess when I ran the script under Vista (and of course exactly the ones that I was looking for did not show up). I tried all kinds of access types and variations of the script mentioned in the forum, but none worked.

So I went looking for another way to obtain the process list and found the function CreateToolhelp32Snapshot. This works great to obtain a process list and is much simpler that the example in the help file.

Here is the code:
Code:

d = `n ; string separator

hProcessSnap := DllCall("CreateToolhelp32Snapshot", "UInt", 2, "Int", 0) ; Obtain list of processes

VarSetCapacity(pe32, 300, 0) ; reserve memory for struct
InsertInteger(296, pe32, 0, 4) ; put length of struct in first item

result := DllCall("Process32First", "UInt", hProcessSnap, "UInt", &pe32 ) ; Get first process
counter := 0
l := ""

if result then
Loop
{
   counter := counter + 1
   
   PID := ExtractInteger(pe32, 8, false, 4) ; Obtain process-id
   str := GetStringFromStruct(pe32, 36) ; Obtain process name at position 36 in struct
   
   l = %l%%PID%-%str%%d%


   result := DllCall("Process32Next", "UInt", hProcessSnap, "UInt", &pe32 )
   if !result
      break
}
DllCall("CloseHandle", "UInt", hProcessSnap) ; close process handle to save memory

MsgBox, 0, %counter% Processes, %l%

GetStringFromStruct(ByRef pSource, pOffset = 0)
{
    str := ""
    kar := *(&pSource + pOffset)
    Loop
    {
   if kar = 0
   {
      return str
   }
   else
   {
      str := str . chr(kar)
        }
   if a_index > 500
   {
      return "groter dan 500"
   }
        kar := *(&pSource + pOffset + a_index)
    }
}


I dont know if there are easier ways to extract the process name from the struct than the way I did it with GetStringFromStruct, so if someone knows a better way, please let me know.

The script probably wont work on Vista 64, because I asume the struct size and the position of the process name will be different, but little experimenting will get you this. To obtain the struct size for example, do the following:

Code:

hProcessSnap := DllCall("CreateToolhelp32Snapshot", "UInt", 2, "Int", 0) ; Obtain list of processes

VarSetCapacity(pe32, 1000, 0) ; reserve memory for struct
Loop 500
{
   InsertInteger(a_index, pe32, 0, 4) ; put length of struct in first item
   result := DllCall("Process32First", "UInt", hProcessSnap, "UInt", &pe32 ) ; Get first process
   if result
   {
      Msgbox, Length of struct is %a_index%
      break
   }
}
DllCall("CloseHandle", "UInt", hProcessSnap) ; close process handle to save memory

_________________
Paul Vintges
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group