Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Windows API / MS.NET Framework API (https://www.delphipraxis.net/20-library-windows-api-ms-net-framework-api/)
-   -   Delphi Prüfen, ob eine Anwendung läuft (https://www.delphipraxis.net/26833-pruefen-ob-eine-anwendung-laeuft.html)

Luckie 30. Jul 2004 02:34


Prüfen, ob eine Anwendung läuft
 
Oft wird ja gefragt, wie man rausfinden kann, ob eine bestimmte Anwendung läuft. Meist wird geantwortet, man soll einen SnapShot machen oder mit EnumProcesses die Prozesse sich auflisten lassen und dann kucken, ob der gesuchte Prozess auftaucht. Es geht aber auch eleganter. ;)

Delphi-Quellcode:
uses
  tlhelp32, ShellAPI;

function GetProcessID(Exename: string): DWORD;
var
  hProcSnap: THandle;
  pe32: TProcessEntry32;
begin
  result := 0;
  hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  if hProcSnap <> INVALID_HANDLE_VALUE then
  begin
    pe32.dwSize := SizeOf(ProcessEntry32);
    if Process32First(hProcSnap, pe32) = true then
    begin
      while Process32Next(hProcSnap, pe32) = true do
      begin
        if pos(Exename, pe32.szExeFile) <> 0 then
          result := pe32.th32ProcessID;
      end;
    end;
    CloseHandle(hProcSnap);
  end;
end;

function GetProcessHandleFromID(ID: DWORD): THandle;
begin
  result := OpenProcess(SYNCHRONIZE, False, ID);
  CloseHandle(result);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  hProcess: THandle;
  wf: DWORD;
begin
  bClose := False;
  Button1.Enabled := False;
  while bClose = False do
  begin
    hProcess := GetProcessHandleFromID(GetProcessID('notepad.exe'));
    if hProcess = 0 then
      Label1.Caption := 'Notepad läuft nicht'
    else
      Label1.Caption := 'Notepad läuft';
    Application.HandleMessage;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  bClose := True;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:46 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz