功能:
第一次執行的時候…會開啟project1.exe
第二次執行的時候…不開啟project1.exe(因程式已執行)…但會將參數會丟給已執行的project1.exe
第3次執行的時候…不開啟project1.exe(因程式已執行)…但會將參數會丟給已執行的project1.exe
...
[公共單元文件]
unit Unlib;
interface
type
par = record
str1 : string[255];
str2 : String[255];
Str3 : string[255];
end ;
implementation
end.
[工程文件]
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Windows,
Messages,
Dialogs,
unlib in 'unlib.pas';
const
appname='myname';
var
myhandle:hwnd;
RexData : ^COPYDATASTRUCT ;
ppar:^par;
{$R *.res}
begin
myhandle:=findwindow(appname,nil);
begin
GetMem(RexData,SizeOf(COPYDATASTRUCT));
GetMem(ppar,Sizeof(par));
with ppar^ do
begin
str1 :=ParamStr(1);
str2 :=ParamStr(2);
Str3 :=ParamStr(3);
end;
with RexData^ do
begin
dwData := myhandle ;
cbData := Sizeof(par) ;
lpData := ppar;
end;
if myhandle>0 then SendMessage(myhandle,WM_COPYDATA,0,LONGINT(REXdATA));
FreeMem(RexData);
FreeMem(ppar);
if myhandle>0 then exit;
end ;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
[單元文件]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,unlib;
const
appname='myname';
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
private
{ Private declarations }
public
procedure createparams(var params:tcreateparams);override;
procedure restorerequest(var msg:TWMCopyData);message WM_CopyData;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.createparams(var params:tcreateparams);
begin
inherited createparams(params);
params.WinClassName:=appname;
end;
procedure TForm1.restorerequest(var msg:TWMCopyData);
var
RexData :TCopyDataStruct ;
ppar: ^par ;
begin
if msg.CopyDataStruct<> nil then
begin
RexData := msg.CopyDataStruct^ ;
ppar:= RexData.lpData ;
if ppar^.str1='' then
memo1.Lines.Add('Not Parameter')
else
memo1.Lines.Add (ppar^.str1);
if ppar^.str2='' then
memo2.Lines.Add('Not Parameter')
else
memo2.Lines.Add (ppar^.str2);
if ppar^.str3='' then
memo3.Lines.Add('Not Parameter')
else
memo3.Lines.Add (ppar^.str3);
end ;
end;