This tutorial will show you how to send param into your program and
close the last instance of your program when finished sending param.
examples like this illustration .
when you execute your application the first instance will be created.
and when you execute your application a second time , then a second
instance will be created . so there are two applications run.
we are going to apply here is , when you execute a second application,
the second application will not run but only send params.
The first time your application run goes like this :
C:/program files/myapp/myapp.exe
The second time your application run goes like this :
C:/program files/myapp/myapp.exe -Hello " how are you ? "
where:
myapp.exe is param(1).
-hello is param(2).
"how are you" is Param(3)
so the second instance your application send param(2) : " how are you "
and the first instance your applications will receive param(2) is " how are you "
here is the code
add this code to your drp program
program Project1;
uses
Forms,
rxvclutils,//or vclutils
messages,
windows,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
var
previnstance:HWND;
cds :TCopyDataStruct;
s :string;
title :string ='Test Param';
begin
previnstance:= FindPrevInstance(TForm1.ClassName,title);
CloseHandle(previnstance);
if previnstance<>0 then begin
if ParamStr(1)='-halo' then begin
with cds do begin
s:= paramstr(2);
cbdata:=length(s)+1;
lpData:=@s[1];
end;
SendMessage(previnstance,wm_CopyData,0,integer(@cds));
WaitForSingleObject(previnstance,INFINITE);
ActivatePrevInstance(TForm1.ClassName,title);
CloseHandle(previnstance);
end;
exit;
end;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
form1.Caption:= title;
Application.Run;
CloseHandle(previnstance);
end.
and add this to your unit
....
public
procedure wmcopydata(var theMsg : TWMCopyData); message WM_COPYDATA;
......
/////////////
procedure Tform1.wmcopydata(var theMsg : TWMCopyData);
var
s:string;
begin
with theMsg.CopyDataStruct^ do
begin
s:=pchar(lpData);
showmessage(s);
end;
//////////////
on create
/////////////
procedure TForm1.FormCreate(Sender: TObject);
begin
if ParamStr(1)= '-halo' then begin
if ParamStr(2) <> '' then
showmessage(paramstr(2));
end;
end;
No comments:
Post a Comment