rasim Posted December 27, 2007 Share Posted December 27, 2007 (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? expandcollapse popupbool 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 December 27, 2007 by rasim Link to comment Share on other sites More sharing options...
Nahuel Posted December 27, 2007 Share Posted December 27, 2007 (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 December 27, 2007 by Nahuel Link to comment Share on other sites More sharing options...
weaponx Posted December 27, 2007 Share Posted December 27, 2007 Is there some reason ProcessExists("notepad.exe") isn't sufficient? Link to comment Share on other sites More sharing options...
James Posted December 27, 2007 Share Posted December 27, 2007 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. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Nahuel Posted December 27, 2007 Share Posted December 27, 2007 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 Link to comment Share on other sites More sharing options...
James Posted December 27, 2007 Share Posted December 27, 2007 Very clever Nahuel! I thought it was another function that would return the PID. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
rasim Posted December 28, 2007 Author Share Posted December 28, 2007 (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 December 28, 2007 by rasim Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now