// uses Tlhelp32;
function GetProcessIDandHDL(const ProcessName: string; var ProcessID,
ProcessHandle: Cardinal): boolean;
var
ProcessEntry32: TProcessEntry32;
ShotHdl: THandle;
begin
Result := False;
ProcessEntry32.dwSize := SizeOf(ProcessEntry32);
ShotHdl := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
try
if ShotHdl = -1 then
Exit;
if Process32First(ShotHdl, ProcessEntry32) then
begin
while Process32Next(ShotHdl, ProcessEntry32) do
begin
if AnsiSameText(ProcessEntry32.szExeFile, ProcessName) then
begin
ProcessID := ProcessEntry32.th32ProcessID;
ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessID);
if ProcessHandle <> 0 then
Result := true;
Break;
end;
end;
end;
finally CloseHandle(ShotHdl);
end;
end;
//Exemple d'utilisation:
var
ProcessHDL: THandle;
ProcessID: Cardinal;
begin
if GetProcessIDandHDL('UnProcessus', ProcessID, ProcessHDL) then
begin
try
//... Utilisation de ProcessHDL et/ou de ProcessID ...
finally CloseHandle(ProcessHDL);
end;
end;