Autor Beitrag
delphiuserno1
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82


Delphi 7 enterprise, Delphi 2007
BeitragVerfasst: Sa 02.09.06 19:08 
Hallo

Ich hab mich gefragt, wie man überprüfen kann ob ein belibiges Programm noch läuft? Also das soll später so funktionieren, wenn das Programm XY beendet wird dann wird wieder das Formular von meinem Programm aufgerufen. Wie kann ich jetzt überprüfen ob es beendet wurde? Kann man evtl die laufenden Prozesse nach der bestimmten Exe durchsuchen oder so ähnlich?

Wäre super wenn jemand ne antwort hat


Gruß
MarkusBauer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Win XP Pro
Delphi 7 PE+Indy
BeitragVerfasst: Sa 02.09.06 19:35 
Versuchs mal damit (nicht getestet):
ausblenden Delphi-Quelltext
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:
28:
29:
30:
Var 
  StartupInfo : TStartupInfo; 
  ProcessInfo : TProcessInformation; 
  Result: Boolean; 
Begin 
  FillChar (StartupInfo, SizeOf (TStartupInfo), 0); 
  StartupInfo.cb := Sizeof (TStartupInfo); 

  StartupInfo.dwFlags := STARTF_USESHOWWINDOW OR STARTF_USEPOSITION OR STARTF_USESIZE ; 
  StartupInfo.wShowWindow := SW_SHOWDEFAULT; 

  Result := CreateProcess ( 
      nil, 
      pChar ('XXX'),    //Name der exe
      nil, 
      nil, 
      False, 
      NORMAL_PRIORITY_CLASS, 
      nil, 
      nil, 
      StartupInfo, 
      ProcessInfo 
    ); 

  If Result then  
    WaitForSingleObject (ProcessInfo.hProcess, INFINITE);  

  if ProcessInfo.hProcess <> 0 then   
    CloseHandle (ProcessInfo.hProcess);  
End;
delphiuserno1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82


Delphi 7 enterprise, Delphi 2007
BeitragVerfasst: So 03.09.06 14:16 
Könntest du das auch näher erklären? weil ich werd da nich ganz schlau raus. was wird den in dieser prozedur genau gemacht?

Vieleicht sollte ich noch dazu sagen, das das programm was ich prüfen will die meiste zeit im tray sitzt und auch darüber beendet wird. also es ist kein fenster sichtbar
evilsoft.de
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 39

XP
Delphi 7
BeitragVerfasst: Mo 04.09.06 18:25 
Benutze nächstes mal die SUFU!

Mit folgendem Code kann geprüft werden, ob eine Exe-Datei läuft. Über Die TProcessEntry32-Struktur kann auch gleich ein Handle auf den Prozess ermittelt werden. Nähere Informationen im PSDK unter dem Stichwort ProcessEntry32.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
uses TlHelp32;

function IsExeRunning(const AExeName: string): boolean;
var
  h: THandle;
  p: TProcessEntry32;
begin
  Result := False;

  p.dwSize := SizeOf(p);
  h := CreateToolHelp32Snapshot(TH32CS_SnapProcess, 0);
  try
    Process32First(h, p);
    repeat
      Result := AnsiUpperCase(AExeName) = AnsiUpperCase(p.szExeFile);
    until Result or (not Process32Next(h, p));
  finally
    CloseHandle(h);
  end;
end;

Beispielaufruf

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Button1Click(Sender: TObject);
begin
 if IsExeRunning('Notepad.exe') then
   ShowMessage('Notepad läuft')
 else
   ShowMessage('Notepad läuft nicht');
end;

Es wird dabei nur überprüft, ob eine Anwendung mit dem gleichen Dateinamen läuft. Es spielt keine Rolle in welchem Verzeichnis sich die Anwendung befindet. Aus diesem Grund muss der Funktion auch nur der Dateiname der EXE-Datei übergeben werden.

Quelle: Easy Helper www.Delphi-Source.de