Sign in to follow this  
Followers 0
rasim

Get process ID used by process name WinAPI

7 posts in this topic

#1 ·  Posted (edited)

Hello! How can get process ID by process name (e.g. "notepad.exe") used WinAPI? I found example code on C++, but how interpreter this code to AutoIT?

bool GetProcessByExeName(DWORD* Pid,AnsiString ExeName)
{
HANDLE hProcessSnap = NULL;
PROCESSENTRY32 pe32   = {0};
AnsiString temp="";

hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
if(hProcessSnap == (void*)-1)
        {
        MessageBox(NULL, "oblom", "oblom", MB_OK|MB_ICONWARNING|MB_SYSTEMMODAL);
        return false;
        }
pe32.dwSize=sizeof(PROCESSENTRY32);

if(Process32First(hProcessSnap,&pe32)) temp=pe32.szExeFile;
if((temp.UpperCase()).Pos(ExeName))
       {
       *Pid= pe32.th32ProcessID;
       CloseHandle (hProcessSnap);
       return true;
       }
pe32.dwSize=sizeof(PROCESSENTRY32);

while(Process32Next(hProcessSnap,&pe32))
        {
        temp=pe32.szExeFile;
        if((temp.UpperCase()).Pos(ExeName))
                {
                *Pid= pe32.th32ProcessID;
                CloseHandle(hProcessSnap);
                return true;
                }
        pe32.dwSize=sizeof(PROCESSENTRY32);
        }
        
CloseHandle(hProcessSnap);
*Pid=0;
return false;
}
Edited by rasim

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Well I guess this is easier:

$list=ProcessList("notepad.exe")

For $i=1 To $list[0][0]
    MsgBox(0,"",$list[$i][0] & "'s PID: " & $list[$i][1])
Next

-edit-

Don't know why I included Process.au3...

-edit2-

check Post 5

Edited by Nahuel

Share this post


Link to post
Share on other sites

Is there some reason ProcessExists("notepad.exe") isn't sufficient?

Share this post


Link to post
Share on other sites

Because he asked for returning the name. I think ProcessExists checks to see if it exists.

I am probably wrong about how it works now.

Well weaponx is right. ProcessExists() does return the pid of a process if it exists (check help file). However, if there are more than just one process with the same name, it will only return one of them (Don't know which one though... test it yourself :) ) So I came up with this:

Func _ProcessGetPid($sName)
    If Not ProcessExists($sName) Then
        SetError(1)
        Return ''
    EndIf
    Local $aPList=ProcessList($sName)
    If @error Then Return ''
    Local $aMatches[UBound($aPList)]
    $aMatches[0] = $aPList[0][0]
    For $i=1 To $aPList[0][0]
        $aMatches[$i] = $aPList[$i][1]
    Next
    Return $aMatches
EndFunc

It will return an array of matches. Example:

$NotepadPid=_ProcessGetPid("notepad.exe")

For $i=1 To $NotepadPid[0]
    MsgBox(0,"","Notepad's PID: " & $NotepadPid[$i])
Next

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Is there some reason ProcessExists("notepad.exe") isn't sufficient?

ProcessClose() don`t terminate some processes, e.g. "nod32krn.exe" (it`s NOD32 antivirus service). I tryed kill this process with WinAPI function "TerminateProcess", but unsuccessful, mayb therefore that ProcessExists() returned uncorrect PID and API function "OpenProcess" don`t returned handle of "nod32krn.exe" process? I found temporary solution with "taskkill.exe", but me interested solution with WinAPI. :) Edited by rasim

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0