We get the process identifier (PID) by its name.
1 2 3 4 5 6 7 8 9 10 11 12 |
Uses Tlhelp32; ... var procName: string; pid: Integer; begin procName:= 'taskmgr.exe'; pid:= getPIDbyProcessName(procName); if pid <> 0 then ShowMessage('Process ID: ' + IntToStr(pid)) else ShowMessage('Process not found'); end; |
Options
procName - process name
Result
Process ID, or 0 if the process was not found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function getPIDbyProcessName(processName:String): integer; var GotProcess: Boolean; tempHandle: tHandle; procE: tProcessEntry32; begin processName:= AnsiLowerCase(processName); tempHandle:=CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0); procE.dwSize:=SizeOf(procE); GotProcess:= Process32First(tempHandle, procE); {$B-} if GotProcess and (AnsiLowerCase(procE.szExeFile) <> processName) then repeat GotProcess := Process32Next(tempHandle, procE); until (not GotProcess) or (AnsiLowerCase(procE.szExeFile) = processName); {$B+} if GotProcess then result := procE.th32ProcessID else result := 0; CloseHandle(tempHandle); end; |