On the path to the executable file, we determine whether it is running.
1 2 3 4 5 6 7 8 9 10 11 12 |
Uses Winapi.TlHelp32; ... var exePath: WideString; pid: Integer; begin exePath:= 'C:\Windows\explorer.exe'; pid:= isExeRunning(exePath); if pid <> 0 then ShowMessage('The file is running') else ShowMessage('The file is not running'); end; |
Options
exePath - path to the executable file
Result
Its process identifier (PID) if the file is running or 0 if not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
function isExeRunning(exePath: WideString): Integer; var hSnapshot: THandle; procPath: string; lppe: TProcessEntry32; begin Result := 0; hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); lppe.dwSize := sizeof(lppe); if Process32First(hSnapshot, lppe) then repeat try procPath:= GetFilePathByProcessID(lppe.th32ProcessID); except procPath:= ''; end; if AnsiCompareText(procPath, exePath) = 0 then begin Result := lppe.th32ProcessID; Break; end; until not Process32Next(hSnapshot, lppe); CloseHandle(hSnapshot); end; |