Autor Beitrag
Windoof
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34

Windows 95/98 SE/NT4.0 WS/2000 Professional/XP Home & Professional, SuSE Linux 7.0/9.0 Professional, OpenBSD
Dev-C++ 4.9.9.0, Borland C++ Builder 3 Personal/5 Professional/6 Enterprise, Borland Delphi 6 Enterprise/7 Enterprise
BeitragVerfasst: Di 16.03.04 13:06 
Hallo.

Ich habe eine Frage: Ich möchte wissen, ob meine Anwendung schonmal gestartet wurde. In C++ (Das ist meine Hauptsprache) geht das so:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
// überprüfen ob programm schon gestartet ist 
CreateMutex(NULL, TRUE, "Anwendung gestartet"); 
if(GetLastError() == ERROR_ALREADY_EXISTS) 

  HWND        hWndFirst; 
  hWndFirst = FindWindow(SZWNDCLASS, NULL); 
  BringWindowToTop(hWndFirst); 
  SetForegroundWindow(hWndFirst); 
  return -1; 
}
Das habe ich versucht in Delphi umzuwandeln:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var hWndFirst: Integer;
begin
  Application.Initialize;
  Application.Title := 'NetSMS Messenger';
  // überprüfen ob programm schon gestartet ist
  CreateMutex(nil, TRUE, 'Anwendung gestartet');
  if GetLastError() = ERROR_ALREADY_EXISTS then
  begin
    hWndFirst := FindWindow(SZWNDCLASS, NULL);
    BringWindowToTop(hWndFirst);
    SetForegroundWindow(hWndFirst);
    Application.Terminate;
  end;
//...
Jetzt sagt mir der Borland Delphi 6, dass CreateMutex undefiniert ist. Welche Unit muss ich einbinden?

MfG Windoof


Zuletzt bearbeitet von Windoof am Di 16.03.04 13:19, insgesamt 1-mal bearbeitet
MaxiTB
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 679

Win2000, WinXp, Workbench ;-)
D7 Ent, VS2003 Arch.
BeitragVerfasst: Di 16.03.04 13:16 
Erstens mußt du den Mutex mit ReleaseMutex wieder freigeben, wenn du keinen Fehler zurückbekommst.

Zweitens - wenns nicht in Windows.pas definiert ist, dann mußt du es nachbasteln:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
HANDLE CreateMutex(
  LPSECURITY_ATTRIBUTES lpMutexAttributes,  // SD
  BOOL bInitialOwner,                       // initial owner
  LPCTSTR lpName                            // object name
);

typedef struct _SECURITY_ATTRIBUTES { 
  DWORD  nLength; 
  LPVOID lpSecurityDescriptor; 
  BOOL   bInheritHandle; 
} SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES;


Dürfte ja nicht so schwierig sein ;-) ...

_________________
Euer Mäxchen
Wer früher stirbt, ist länger tot.
Windoof Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34

Windows 95/98 SE/NT4.0 WS/2000 Professional/XP Home & Professional, SuSE Linux 7.0/9.0 Professional, OpenBSD
Dev-C++ 4.9.9.0, Borland C++ Builder 3 Personal/5 Professional/6 Enterprise, Borland Delphi 6 Enterprise/7 Enterprise
BeitragVerfasst: Di 16.03.04 13:23 
Jo, habs:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
var hWndFirst: Integer;
begin
  Application.Initialize;
  Application.Title := 'NetSMS Messenger';
  // überprüfen ob programm schon gestartet ist
  CreateMutex(nil, TRUE, 'Anwendung gestartet');
  if GetLastError() = ERROR_ALREADY_EXISTS then
  begin
    hWndFirst := FindWindow('TForm'nil);
    BringWindowToTop(hWndFirst);
    SetForegroundWindow(hWndFirst);
    Application.Terminate;
  end;
  ReleaseMutex(hWndFirst);
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.CreateForm(TForm3, Form3);
  Application.Run;
end.
MaxiTB
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 679

Win2000, WinXp, Workbench ;-)
D7 Ent, VS2003 Arch.
BeitragVerfasst: Di 16.03.04 13:47 
Falsch.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
var 
 hWndFirst: HWND;
 hMutex: THandle;
begin
 Application.Initialize;
 Application.Title := 'NetSMS Messenger';
 // überprüfen ob programm schon gestartet ist
 hMutex:=CreateMutex(nil, TRUE, 'Anwendung gestartet');
 if GetLastError() = ERROR_ALREADY_EXISTS then
 begin
  hWndFirst := FindWindow('TForm'nil);
  BringWindowToTop(hWndFirst);
  SetForegroundWindow(hWndFirst);
  Application.Terminate;
 end;
 ReleaseMutex(hMutex);
 Application.CreateForm(TForm1, Form1);
 Application.CreateForm(TForm2, Form2);
 Application.CreateForm(TForm3, Form3);
 Application.Run;
end.

_________________
Euer Mäxchen
Wer früher stirbt, ist länger tot.
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 16.03.04 15:39 
MaxiTB hat folgendes geschrieben:
Falsch.

Stimmt. :mrgreen:


Ich mach´s meist so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
hMutex:=CreateMutex(nil, TRUE, 'Anwendung gestartet');
if GetLastError() = ERROR_ALREADY_EXISTS then
begin
  { ... }
end;

Application.*
{ ... }

// und erst den Mutex freigeben, wenn auch
// das Programm beendet wird
CloseHandle(hMutex);

Und noch Lektüre: Suche in: Delphi-Forum, Delphi-Library MUTEX, Suche in: Delphi-Forum, Delphi-Library SEMAPHORE. Gab´s nämlich alles schon, Jungs. ;)