| Autor |
Beitrag |
Stevie
      
Beiträge: 85
Windows 7
Delphi XE Professional
|
Verfasst: Mo 16.06.03 13:15
Hi,
ich hab folgendes ausprobiert:
Delphi-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
| var Handle: THandle;
begin Handle := FindWindow('TApplication', PChar('Anwendungstitel')); if Handle = 0 then begin Application.Initialize; Application.Title := 'Anwendungstitel'; Application.CreateForm(TfrmWin, frmWin); Application.Run; end else begin SetForegroundWindow(Handle); Application.Terminate; end; |
Wenn ich jetzt das Programm (zum ersten mal) starte, bricht es gleich wieder ab.
Ein Blick in den Debugger zeigt, dass ein Handle zurückgeliefert wird, das ungleich 0 ist!?
Angeblich soll es doch so funktionieren. Ich hab auch schon das versucht:
Delphi-Quelltext 1:
| Handle := FindWindow('TfrmWin',nil); |
Das brachte aber das gleiche Ergebnis! Was mache ich denn falsch?
P.S. Es gibt doch auch eine Methode mutex oder so. Ich hab nur am Rande gesehen, funktioniert die auch mit Win98?
Moderiert von tommie-lie: Code- durch Delphi-Tags ersetzt
|
|
Popov
      
Beiträge: 1655
Erhaltene Danke: 13
WinXP Prof.
Bei Kleinigkeiten D3Pro, bei größeren Sachen D6Pro oder D7
|
Verfasst: Mo 16.06.03 13:28
|
|
iaby
      
Beiträge: 21
|
Verfasst: Mo 16.06.03 13:44
ich glaube das application.terminate soll da nicht hin, da es die application ja eigentlich noch gar nicht gibt!
wenn du es weg lässt sollte es funktionieren!
damit ist das problem mit dem handle aber noch nicht gelöst!?!
|
|
Motzi
      
Beiträge: 2931
XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
|
Verfasst: Mo 16.06.03 13:45
Statt einem Mutex sollte ein Semaphore verwendet werden.. es gibt Dokumentation in denen ein Mutex (unter Win9x) möglicherweise nicht korrekt freigegeben wird -> das Prog kann dann während dieser Windows-Sitzung nicht mehr gestartet werden...
_________________ gringo pussy cats - eef i see you i will pull your tail out by eets roots!
|
|
Stevie 
      
Beiträge: 85
Windows 7
Delphi XE Professional
|
Verfasst: Mo 16.06.03 13:54
| Motzi hat folgendes geschrieben: | | Statt einem Mutex sollte ein Semaphore verwendet werden. |
Hab ich das probiert:
Delphi-Quelltext 1: 2: 3: 4: 5: 6:
| var Semaphore: THandle; begin Semaphore := CreateSemaphore(nil, 0, 1, 'MeinWindowsProgramm'); if not (Semaphore = 0) and (GetLastError = ERROR_ALREADY_EXISTS) then CloseHandle(Semaphore); |
Es geht trotzdem nicht!!! (WinNT4.0 SP6)
Moderiert von tommie-lie: Code- durch Delphi-Tags ersetzt
|
|
Motzi
      
Beiträge: 2931
XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
|
Verfasst: Mo 16.06.03 14:11
Zeig mal deinen ganze Code (aus der dpr-Datei)...
_________________ gringo pussy cats - eef i see you i will pull your tail out by eets roots!
|
|
Stevie 
      
Beiträge: 85
Windows 7
Delphi XE Professional
|
Verfasst: Mo 16.06.03 14:19
|
|
Motzi
      
Beiträge: 2931
XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
|
Verfasst: Mo 16.06.03 14:24
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:
| program MyProc;
uses Forms, frMyProc in 'frMyProc.pas' ;
{$R *.res}
var Semaphore: THandle; begin Semaphore := CreateSemaphore(nil, 0, 1, 'MeinWindowsProgramm'); if (GetLastError = ERROR_ALREADY_EXISTS) then begin CloseHandle(Semaphore); MessageBox(0, 'Anwendung läuft bereits'); end else begin Application.Initialize; Application.Title := 'MeinProgramm'; Application.CreateForm(TfrmMyProc, frmMyProc); Application.Run; end; end. |
_________________ gringo pussy cats - eef i see you i will pull your tail out by eets roots!
|
|
Stevie 
      
Beiträge: 85
Windows 7
Delphi XE Professional
|
Verfasst: Mo 16.06.03 14:29
Jau, es klappt, danke!
Wie kann ich jetzt noch das bereits aktive Fenster in den Vordergrund bringen?
|
|
Motzi
      
Beiträge: 2931
XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
|
Verfasst: Mo 16.06.03 14:39
Ersetz einfach die MessageBox mit deinem Code (FindWindow + SetForegroundWindow)...
_________________ gringo pussy cats - eef i see you i will pull your tail out by eets roots!
|
|
Stevie 
      
Beiträge: 85
Windows 7
Delphi XE Professional
|
Verfasst: Mo 16.06.03 14:49
Wie bekomme ich denn mit FindWindow das richtige Handle?
Ich bekomm immer 0 raus.
|
|
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1
|
Verfasst: Mo 16.06.03 14:56
Delphi-Quelltext 1: 2:
| findwindow('TfrmMyProc', nil); |
Wenn du den Namen der Form nicht für alle deine Programme verwendest, solltest du fündig werden.
|
|
Stevie 
      
Beiträge: 85
Windows 7
Delphi XE Professional
|
Verfasst: Mo 16.06.03 15:03
Jo, alles klappt wunderbar, danke nochmal!!
|
|