Delphi-PRAXiS
Seite 1 von 3  1 23      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Doppelstart einer Anwendung (https://www.delphipraxis.net/81000-doppelstart-einer-anwendung.html)

Hador 18. Nov 2006 20:45


Doppelstart einer Anwendung
 
Hallo,

ich versuche grad den doppelstart einer Anwendung zu verhindern.
Dazu hab ich in der CodeLib einen Beitrag von Luckie gefunden.

Meine Umsetzung sieht folgendermaßen aus:
Delphi-Quellcode:
var
  mHandle: THandle;
  i: Integer;

begin
  mHandle := CreateMutex(nil, True, PAnsiChar(ParamStr(0)));
  if GetLastError = ERROR_ALREADY_EXISTS then
  begin
    for i := 1 to ParamCount do
      CopyData(mHandle, ParamStr(i)); // Parameter der 1. Instanz übergeben
    Application.Terminate;
  end;
  Application.Initialize; // Ggf. Formulare erzeugen
  Application.CreateForm(TGUI, GUI);
  Application.Run;
  if mHandle <> 0 then
    CloseHandle(mHandle)
end.
Mein Problem ist nur, dass es so aus irgendeinem Grund nicht funktioniert.
Nur der Grund ist mir nicht bekannt :|
Ich hoffe also mal wieder auf euch :wink:

Sunlight7 18. Nov 2006 21:09

Re: Doppelstart einer Anwendung
 
Und was funktioniert nicht?

Übrigens: Statt Application.Terminate ein Exit; :wink:

Hador 18. Nov 2006 21:33

Re: Doppelstart einer Anwendung
 
Der Sinn dieses Codes soll sein, dass die Anwendung nicht doppelt gestartet wird.
Und genau das funktioniert nicht.

Ob Exit oder App.Terminate ist egal.

bigg 18. Nov 2006 22:03

Re: Doppelstart einer Anwendung
 
Hi,

was liefert dir mHandle, wenn deine Anwendung bereits läuft und was wenn sie nicht läuft?

Hador 18. Nov 2006 22:15

Re: Doppelstart einer Anwendung
 
Jeweils 0

Also sollte es daran liegen, aber ich weiß immer noch nicht, was faclsch ist.

Sunlight7 18. Nov 2006 22:37

Re: Doppelstart einer Anwendung
 
Das liegt daran, das Du Deinen Pfad & Dateinamen übergibst, verwende einen eindeutigen Namen, dann klappts.

Den Fehlercode von GetLastError beachten. :wink:

Edit: Und dennoch: Statt Application.Terminate ein Exit; :wink:

bigg 18. Nov 2006 22:42

Re: Doppelstart einer Anwendung
 
Ich zitiere mal das msdn:

Zitat:

Return Value

If the function succeeds, the return value is a handle to the newly created mutex object.

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

If the mutex is a named mutex and the object existed before this function call, the return value is a handle to the existing object, GetLastError returns ERROR_ALREADY_EXISTS, bInitialOwner is ignored, and the calling thread is not granted ownership. However, if the caller has limited access rights, the function will fail with ERROR_ACCESS_DENIED and the caller should use the OpenMutex function.
Was liefert GetlastError?

Sunlight7 18. Nov 2006 22:47

Re: Doppelstart einer Anwendung
 
ERROR_PATH_NOT_FOUND (3) = 'Pfad nicht gefunden'

bigg 18. Nov 2006 22:51

Re: Doppelstart einer Anwendung
 
Der Pfadname kann variieren, er sollte allerdings konstant sein, ansonsten bräuchte man das Programm nur in einen anderen Ordner kopieren und starten. Habe das ganze nicht getestet, meine Vermutung sollte dennoch stimmen.

Sunlight7 18. Nov 2006 22:59

Re: Doppelstart einer Anwendung
 
Zitat:

Zitat von Windows SDK
lpName:
Points to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.

Tut es aber nicht, CreateMutex nimmt den Pfad erst gar nicht an, das Handle ist IMMER Null, deshalb erledigt sich die Frage schon...

Edit: Also wenn schon, dann ExtractFileName(ParamStr(0));
Aber wenn man die Exe kopiert, umbenennt und nochmal startet...


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:53 Uhr.
Seite 1 von 3  1 23      

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