About me| Chi sono?| BLOG
Chiara| Paola| Valentina| Francesca
Home| Download| Poesie | Poems| Immagini | Pictures| Articoli | Papers| Winamp Plugins
English
› Home › Download › Poems › Pictures » Technical Papers › Winamp Plugins
Italiano
› Principale › Download › Poesie › Immagini » Articoli IoProgrammo › Plugins per Winamp

Come rilevare lo stato di esecuzione di un processo?

Il codice seguente consente di verificare la presenza di un processo e di ottenere il PID relativo:
// Salvatore Meschini - 27/08/2005
program TestGetPid; // DELPHI

{$APPTYPE CONSOLE}

uses Windows, PSApi, SysUtils;

// True -> Windows NT/2000/XP/2003, False -> Windows ME/9x
function isWinNT: boolean;
var
  OSInfo: TOSVersionInfo;
begin
  Result := False;
  OSInfo.dwOSVersionInfoSize := SizeOf(OSInfo);
  if GetVersionEx(OSInfo) then
    Result := OSInfo.dwPlatformId = VER_PLATFORM_WIN32_NT;
end;

// p = processname.exe (ex. p := 'explorer.exe');
function GetPidFromProcessName(p: string): Integer;
var
  PIDList: array[0..4096] of DWORD;
  c,i: DWORD;
  H: THandle;
  N: string;
begin
	i := 0;
  Result := -1; // Assuming Failure
  if IsWinNT then
    if EnumProcesses(@PIDList, SizeOf(PIDList), c) then
      while (i < (c div SizeOf(DWORD))) and (Result = -1) do
      begin
        H := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PIDList[i]);
        try
          SetLength(N, MAX_PATH);
          if GetModuleBaseName(H, 0, pchar(N), MAX_PATH) > 0 then
          begin
            SetLength(N, Length(PChar(N)));
            // Case-insensitive:
            if CompareText(p, N) = 0 then
               Result := PIDList[i]; // Bingo!
          end;
          inc(i); // Next PID
        finally
          CloseHandle(H);
        end;
      end;
end;

// True -> process p is running

function isRunning(p: string): boolean;
begin
  Result := GetPidFromProcessName(p) <> -1;
end;

begin
  if isRunning('getpid.exe') then
    Writeln(GetPidFromProcessName('getpid.exe'))
  else
    Writeln('Processo non trovato');
  readln;
end.
Click here - Clicca qui