Jump to content

Recommended Posts

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
Posted

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...