Autor Beitrag
Stevie
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85

Windows 7
Delphi XE Professional
BeitragVerfasst: Mo 16.06.03 13:15 
Hi,

ich hab folgendes ausprobiert:
ausblenden 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:
ausblenden 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 user profile icontommie-lie: Code- durch Delphi-Tags ersetzt
Popov
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1655
Erhaltene Danke: 13

WinXP Prof.
Bei Kleinigkeiten D3Pro, bei größeren Sachen D6Pro oder D7
BeitragVerfasst: Mo 16.06.03 13:28 
Geh da

free.pages.at/popov3/pub/topten/faq.txt

und guck dir Tipp #9 an.

_________________
Popov
iaby
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: 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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85

Windows 7
Delphi XE Professional
BeitragVerfasst: Mo 16.06.03 13:54 
Motzi hat folgendes geschrieben:
Statt einem Mutex sollte ein Semaphore verwendet werden.

Hab ich das probiert:
ausblenden 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 user profile icontommie-lie: Code- durch Delphi-Tags ersetzt
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: 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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85

Windows 7
Delphi XE Professional
BeitragVerfasst: Mo 16.06.03 14:19 
Hier (jetzt hab ich das, was ich oben gepostet hab wieder rausgenommen):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
program MyProc;

uses
  Forms,
  frMyProc in 'frMyProc.pas' {frmMyProc};

{$R *.res}

begin
  Application.Initialize;
  Application.Title := 'MeinProgramm';
  Application.CreateForm(TfrmMyProc, frmMyProc);
  Application.Run;
end.



Moderiert von user profile icontommie-lie: Code- durch Delphi-Tags ersetzt
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Mo 16.06.03 14:24 
ausblenden 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' {frmMyProc};

{$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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85

Windows 7
Delphi XE Professional
BeitragVerfasst: Mo 16.06.03 14:29 
Jau, es klappt, danke!
Wie kann ich jetzt noch das bereits aktive Fenster in den Vordergrund bringen?
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: 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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85

Windows 7
Delphi XE Professional
BeitragVerfasst: 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



BeitragVerfasst: Mo 16.06.03 14:56 
ausblenden Delphi-Quelltext
1:
2:
findwindow('TfrmMyProc', // <-- Name deiner Form
  nil);                  // <-- Titel interessiert zu 99% nicht

Wenn du den Namen der Form nicht für alle deine Programme verwendest, solltest du fündig werden.
Stevie Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 85

Windows 7
Delphi XE Professional
BeitragVerfasst: Mo 16.06.03 15:03 
Jo, alles klappt wunderbar, danke nochmal!!