...Parameter an einer schon laufenden Programminstanz übergeben?
Autor: Loïs Bégué
{+++++++++++++++++++++ English +++++++++++++++++++++++++++++++++++++++++++++
You can control the start of your program and:
1) disallow a second start
2) call the already running application and make it active
3) send the "new" command line parameters to it
++++++++++++++++++++++ Deutsch +++++++++++++++++++++++++++++++++++++++++++++
Sie kontrollieren den Programmstart und:
1) verhindern, daß eine zweite Instanz gestartet wird
2) aktivieren die erste Instanz
3) übergeben die neuen Kommandozeilenparameter an dieser Instanz
++++++++++++++++++++++ Français ++++++++++++++++++++++++++++++++++++++++++++
Vpous controllez le démarage du programme et:
1) empêchez une deuxième instance
2) activez la première instance
3) passez les paramêtres au démarrage de la 2ème vers la 1ère instance
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
{####################### MAIN PROG FILE #####################################}
Programm Number_5;
//...
const
cMutexID = 'Number_5_is_alive';
var
Mutex: THandle;
HAPPLICATION: HWND;
HMAINFORM: HWND;
StartParams: string;
AnzahlParams: Integer;
AtomSend: Integer;
begin
Mutex := CreateMutex(nil, True, cMutexID);
// +++ identify an already running app +++
if (Mutex <> 0) and (GetLastError = 0) then
begin
// +++ none was found, so proceed as usual +++
Application.Initialize;
Application.Title := 'Number 5 is alive';
Application.CreateForm(TMF_Number5, MF);
Application.Run;
if (Mutex <> 0) then CloseHandle(Mutex);
end
else
begin
// +++ look for the running app windows handle +++
HAPPLICATION := 0;
HMAINFORM := 0;
Application.Initialize;
repeat
HAPPLICATION := FindWindowEx(0,HAPPLICATION, 'TApplication',
PChar('Number 5 is alive'));
until HAPPLICATION <> Application.Handle;
// +++ found it, so activate the running app and proceed with the parameters +++
if HAPPLICATION <> 0 then
begin
Windows.ShowWindow(HAPPLICATION, SW_Normal);
Windows.SetForegroundWindow(HAPPLICATION);
// +++ look for the Form handle, wich will be used as a target of the message +++
HMAINFORM := FindWindowEx(0,0,'TMF_Number5', nil);
if (HMAINFORM <> 0) then
begin
StartParams := '';
// +++ code your parameters +++
for AnzahlParams := 1 to ParamCount do
StartParams := StartParams + ' ' + ParamStr(AnzahlParams);
try
// +++ Send the parameter info +++
AtomSend := GlobalAddAtom(PChar(StartParams));
SendMessage(HMAINFORM, WM_ACTIVATENumber5, Length(StartParams), AtomSend);
finally
GlobalDeleteAtom(AtomSend);
end;
end;
end;
// all is done: parameters had been sent so we can quit safelly
Halt;
end;
end.
{####################### FORM WITH MESSAGE HANDLING ROUTINE #################}
unit MFU;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
...;
const
WM_ACTIVATENumber5 = WM_USER + 101;
type
TMF_Number5 = class(TForm)
procedure FormShow(Sender: TObject);
procedure WMACTIVATENumber5(var Msg: TMessage); message WM_ACTIVATENumber5;
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
MF: TMF_Number5;
implementation
{$R *.dfm}
procedure TMF_Number5.FormShow(Sender: TObject);
begin
// +++ the app was normaly started: parameters are maybe pending in the command line buffer +++
if ParamCount > 0 then
begin
// +++ Handle with this parameters.... +++
end;
end;
procedure TMF_Number5.WMACTIVATENumber5(var Msg: TMessage);
var
Buffer: PChar;
S: string;
begin
// +++ the app was "reactivated": the command line cannot be used thru "ParamStr(..)" yet +++
try
// +++ get the length of the "command line message" +++
Buffer := StrAlloc(Msg.wParam + 1);
// +++ hold its value +++
GlobalGetAtomName(Msg.lParam, Buffer, Msg.wParam + 1);
S := StrPas(Buffer);
// +++ Analyse S and handle with parameters at will.... +++
finally
StrDispose(Buffer);
end;
end;
end.
printed from
www.swissdelphicenter.ch
developers knowledge base