Announcement

Collapse
No announcement yet.

mehrfach Programmstart, Probleme

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • mehrfach Programmstart, Probleme

    Hallo zusammen,<br>
    mir stellt sich folgendes Problem. <br>
    Mein Programm hat registrierte Dateien die beim öffnen im z.B. Explorer das Programm starten und geöffnet werden (shell\open\command). Im onCreate Ereigniss der MainForm wird dann über Params die jeweilige Datei geöffnet. Jetzt habe ich aber den mehrfach Programm start verhindert ....<br>
    <pre><b>
    unit only_one;
    interface
    implementation
    uses forms, windows;
    var mutex : THandle;
    h : HWnd;

    initialization
    mutex := CreateMutex(nil,true,'AcoboMutex');
    if getLastError = ERROR_ALREADY_EXISTS then begin
    h := 0;
    repeat
    h := FindWindowEx(0,h,'TApplication',PChar('Admin CookBook'))
    until h <> application.handle;
    if h <> 0 then begin
    //hier die Datei zu öffnen quitiert Windows mit vielen Fehlern ;-(
    Windows.ShowWindow(h, SW_ShowNormal);
    windows.SetForegroundWindow(h);
    end;
    halt;
    end;
    finalization
    ReleaseMutex(mutex);
    end.
    </b></pre><br>
    ..... und bekomme es nicht hin das passende Ereigniss zu finden um die Registrierte Datei zu öffnen.<br>
    Habt Ihr ne Idee ??

  • #2
    Hab erst einmal folgende Lösung gefunden:
    <pre>
    unit only_one;

    interface

    implementation
    uses forms, windows,Messages,Sysutils;
    var mutex : THandle;
    h1,h2 : HWnd; //Application und Mainform Handle
    aCopyData : TCopyDataStruct; // Instanz des Records

    initialization

    mutex := CreateMutex(nil,true,'NurMeinProgrammHatDiesenMute x');
    if getLastError = ERROR_ALREADY_EXISTS then
    begin
    h1 := FindWindow(nil,PChar(myApplicationTitel));
    h2 := FindWindow(nil,PChar(myMainFormCaption));
    if (h1 <> 0)and(h1 <> application.handle)then
    begin
    Windows.ShowWindow(h1, SW_ShowNormal);
    windows.SetForegroundWindow(h1);
    if ParamCount = 1 then
    begin
    with aCopyData do begin
    dwData := 0;
    cbData := StrLen(PChar(ParamStr(1))) + 1;
    lpData := PChar(ParamStr(1));
    end;
    SendMessage(h2,WM_CopyData,LongInt(Application.Han dle),LongInt(@aCopyData));
    end;
    end;
    halt;
    end;
    finalization
    ReleaseMutex(mutex);
    end.
    </pre>
    und der Empfänger
    <pre>
    procedure TMainForm.WMCopyData(var Message: TWMCopyData);
    var
    sText : array[0..MAX_PATH] of Char;
    begin
    StrLCopy(sText, Message.CopyDataStruct.lpData,
    Message.CopyDataStruct.cbData);

    LoadCommandFile(sText);
    end;
    </pre>
    Das ist bestimmt eleganter zu lösen, am wenigsten gefällt mir das ich 2 Handle benötige. Es ist ziehmlich blöd sich mit FindWindow das Handle für TForm.caption zu besorgen. Und mein Bauch sagt mir das ich das Application.Handle benötige für windows.SetForegroundWindow(H1) obwohl es mit dem H2 Handle auch funktioniert. <br>
    Gruß Andreas H

    Comment

    Working...
    X