Autor Beitrag
Hansi@OMG
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: Mi 30.04.03 14:48 
Ich habe im Internet nach einer Funktion gesucht, die überprüft, ob eine EXE-Datei gerade geöffnet ist. Ich habe folgende Funktion gefunden, ich glaube, sie müsste eigentlich auch funktionieren, tut sie aber leider nicht, weis jemand wieso?
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
function ExeFileIsRunning(ExeFile: string): boolean;
var
  H:word;
begin
  H := CreateFile(PChar(ExeFile),
                  GENERIC_READ,
                  0,
                  nil,
                  OPEN_EXISTING,
                  0,
                  0);
  Result := (H >= 65535);
  CloseHandle(H);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if exefileisrunning('C:\test.exe') = true then label1.caption:='Läuft';
if exefileisrunning('C:\test.exe') = false then label1.caption:='Läuft net';
end;


Diese Funktion müsste doch eigentlich funktionieren. Ich weis allerdings nicht, wieso sie nicht funktioniert. Kann mir da einer weiterhelfen?

_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 30.04.03 15:46 
Eventuell hilft es, wenn man die Hilfe liest:
Zitat:

Return Values
If the function succeeds, the return value is an open handle to the specified file. If the specified file exists before the function call and dwCreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file does not exist before the call, GetLastError returns zero.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

Ich würde auf INVALID_HANDLE_VALUE testen und nicht auf dieses obskure >= 65535.
Desweitern liefert CreateFile ein Handle zurück und das wird nicht in eine Variable vom Typ WORD passen. nimm da mal THandle oder Cardinal.
Hansi@OMG Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: Sa 03.05.03 13:30 
Ich krieg die Funktion immer noch nicht zum laufen, des könnte allerdings an meinem Mangel an Delphikenntnissen lernen, bin noch relativ am Anfang, zwar nicht mehr so stark, aber im Vergleich zu anderen hier im Forum halt schon noch. Könnte mir nicht eventuell einer diese Funktion, so posten, dass sie läuft?

_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 04.05.03 09:13 
Habe ich selbst mal irgendwo im Netz gefunden:
ausblenden 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:
function IsFileInUse(s: string): boolean;
var
  HFileRes : HFILE;
begin
  // Standardergebnis (= FALSE = Datei wird nicht benutzt)
  Result   := false;

  // wenn die Datei nicht existiert, Funktion beenden
  if(not fileexists(s)) then exit;

  // Datei öffnen
  HFileRes := CreateFile(pchar(s),
    GENERIC_READ or GENERIC_WRITE,
    0,
    nil,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);

  // wenn die Datei benutzt wird, ist das Ergebnis von
  // "HFileRes" = INVALID_HANDLE_VALUE; die Funktion
  // liefert dann TRUE zurück
  Result   := (HFileRes = INVALID_HANDLE_VALUE);

  // wenn nicht, muss die geöffnete Datei geschlossen
  // werden
  if(not Result) then CloseHandle(HFileRes);
end;

Anwendung:
ausblenden Quelltext
1:
2:
3:
4:
5:
procedure TForm1.Button1Click(Sender: TObject);
begin
  if(IsFileInUse('c:\test.exe')) then Label1.Caption := 'läuft'
    else Label1.Caption := 'läuft nicht';
end;
Hansi@OMG Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: So 04.05.03 09:34 
Danke, diese Funktion funktioniert super!!! :D

_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?