Autor |
Beitrag |
Windoof
      
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
|
Verfasst: 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: 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: 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'; 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
      
Beiträge: 679
Win2000, WinXp, Workbench ;-)
D7 Ent, VS2003 Arch.
|
Verfasst: 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:
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 
      
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
|
Verfasst: Di 16.03.04 13:23
Jo, habs: 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'; 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
      
Beiträge: 679
Win2000, WinXp, Workbench ;-)
D7 Ent, VS2003 Arch.
|
Verfasst: Di 16.03.04 13:47
Falsch.
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'; 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
|
Verfasst: Di 16.03.04 15:39
MaxiTB hat folgendes geschrieben: | Falsch. |
Stimmt.
Ich mach´s meist so:
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.*
CloseHandle(hMutex); |
Und noch Lektüre: MUTEX, SEMAPHORE. Gab´s nämlich alles schon, Jungs. 
|
|
|