![]() |
OneInst - Problem bei der Übergabe von Parameter
Hallo,
ich benutze folgenden Code in meiner Anwendung, um die Anwedung nur einmal zu starten und um sie in den Vordergrund zu bringen. Dass funktioniert alles super, nur beim Übertragen eines Paramters habe ich ein paar Probleme: Zunächst die oneinst: ![]() Nun habe ich vor SetForegroundWindow(Wnd); folgenden Code eingefügt:
Delphi-Quellcode:
Dann habe ich in meiner Unit die Prozedur für das Abfangen des Paramters eingebaut:
var
MyCopyDataStruct: TCopyDataStruct; begin If ParamStr(1) <> '' then with MyCopyDataStruct do begin dwData:=SecondInstID; // ID mit der sich das zweite Programm meldet cbData:=Length(PChar(ParamStr(1))) + 1; lpData:=PChar(ParamStr(1)); SendMessage(Wnd, WM_COPYDATA, 0, LPARAM(@MyCopyDataStruct)); end; end;
Delphi-Quellcode:
Mein Problem liegt nun darin, dass diese Prozedur gar nicht auferufen wird.
procedure TForm1.WMCOPYDATA(var Msg: TWMCopyData);
var sText: array of Char; begin If Msg.CopyDataStruct.dwData = SecondInstID then //siehe Oben begin SetLength(sText,Msg.CopyDataStruct.cbData); StrLCopy(PChar(sText), Msg.CopyDataStruct.lpData, Msg.CopyDataStruct.cbData); NewAVObject(PChar(sText)); end; end; Hat jemand eine Ahnung woran das liegen könnte? Gruß Mazel |
Re: OneInst - Problem bei der Übergabe von Parameter
Moin Marcel,
wie hast Du denn die Methode in das Formular eingebunden? |
Re: OneInst - Problem bei der Übergabe von Parameter
Wie meinst du das? Welche Methode eingebunden?
Meinst du die WMCopy? |
Re: OneInst - Problem bei der Übergabe von Parameter
Moin Marcel,
ja, die, die nicht aufgerufen wird. |
Re: OneInst - Problem bei der Übergabe von Parameter
Delphi-Quellcode:
Um noch alles komplett zu machen, hier der Code was gleich am Anfang passiert:
private
procedure WMCOPYDATA(var Msg: TWMCopyData); Message WM_COPYDATA; . . . end; procedure TForm1.WMCOPYDATA(var Msg: TWMCopyData); var sText: array of Char; begin If Msg.CopyDataStruct.dwData = SecondInstID then //siehe Oben begin SetLength(sText,Msg.CopyDataStruct.cbData); StrLCopy(PChar(sText), Msg.CopyDataStruct.lpData, Msg.CopyDataStruct.cbData); NewAVObject(PChar(sText)); end; end;
Delphi-Quellcode:
initialization
SecondInstID:=RegisterWindowMessage(PChar(MYGUID)); // InstanzID EnsureSingleInstance(MyGUID); //hier geht es in die oneinst weiter |
Re: OneInst - Problem bei der Übergabe von Parameter
Moin Marcel,
soweit ok. Und woher stammt das Handle, an das Du WM_COPYDATA schickst? Das müsste ja das Handle des Fensters der laufenden Instanz sein. |
Re: OneInst - Problem bei der Übergabe von Parameter
Ok, ich habe jetzt hier mal die oneinst:
Delphi-Quellcode:
unit OneInst;
interface { Make a call to this procedure in your project source immediately before the first call to CreateForm. That should ensure it is after the Application.Title assignment that can muck up the logic. If you haven't set an application title yet, then do so to ensure this works } procedure EnsureSingleInstance (MyGUID : string) ; implementation uses WinTypes, WinProcs, Forms, SysUtils, Messages; procedure EnsureSingleInstance (MyGUID : string) ; var Wnd: HWnd; WndClass, WndText: array[0..255] of char; begin {$ifdef Win32} { Try and create a semaphore. If we succeed, then check } { if the semaphore was already present. If it was } { then a previous instance is floating around. } { Note the OS will free the returned semaphore handle } { when the app shuts so we can forget about it } if (CreateSemaphore(nil, 0, 1, PChar(MyGUID)) <> 0) and (GetLastError = Error_Already_Exists) then {$else} if HPrevInst <> 0 then {$endif} begin Wnd := GetWindow(Application.Handle, gw_HWndFirst); //hier erfahre ich das Handle while Wnd <> 0 do begin { Look for the other TApplication window out there } if Wnd <> Application.Handle then begin { Check it's definitely got the same class and caption } GetClassName(Wnd, WndClass, Pred(SizeOf(WndClass))); GetWindowText(Wnd, WndText, Succ(Length(Application.Title))); if (StrPas(WndClass) = Application.ClassName) and (StrPas(WndText) = Application.Title) then begin { This technique is used by the VCL: post } { a message then bring the window to the } { top, before the message gets processed } PostMessage(Wnd, wm_SysCommand, sc_Restore, 0); {$ifdef Win32} {meine Abfrage von ParamStr und dann an das erhaltene Handle sendeen} If ParamStr(1) <> '' then with MyCopyDataStruct do begin dwData:=SecondInstID; cbData:=Length(PChar(ParamStr(1))) + 1; lpData:=PChar(ParamStr(1)); SendMessage(Wnd, WM_COPYDATA, 0, LPARAM(@MyCopyDataStruct)); end; SetForegroundWindow(Wnd); {$else} BringWindowToTop(Wnd); {$endif} Halt end end; Wnd := GetWindow(Wnd, gw_HWndNext) end end end; end. |
Re: OneInst - Problem bei der Übergabe von Parameter
Moin Marcel,
ich habe mal geprüft, ob der SendMessage-Aufruf überhaupt funktioniert, da ich so keinen Fehler entdecken konnte. Da der Aufruf fehlschlug, habe ich mal meine Samples zum Thema WM_COPYDATA (mit SingelInstance) rausgesucht und ausprobiert. Auf einmal erhalte ich da den gleichen Fehler. Im Moment kann ich mir nicht erklären warum das fehlschlägt, bislang haben die Beispiele nämlich funktioniert :gruebel: :? |
Re: OneInst - Problem bei der Übergabe von Parameter
Moin Marcel,
so, jetzt habe ich was. Bei mir hat es erst funktioniert, nachdem
Seltsam finde ich nur, dass das bislang auch so geklappt hatte, und jetzt nicht mehr :gruebel: Ich weiss nicht, was sich seit dem letzten Mal an dem ich das genutzt habe geändert hat. |
Re: OneInst - Problem bei der Übergabe von Parameter
Hi,
danke Christian, ich konnte nicht antworten, da ich im Kurzurlaub war. Ich finde es ja auch seltsam, denn alle die die OneInst verwenden sagen ja, dass es funktioniert. Damit mein Programm aber so funktioniert wie ich es will, brauch ich aber die OneInst. Oder gibt es da noch eine andere Möglichkeit? Gruß Mazel |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:14 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz