两个程序之间传递数据可以使用wm_copydata

下面是我的回答:
var
dt: TYourStruct;

procedure Txxxx.WMCopyData(var Message: TWMCopyData);
var
p: PChar;
begin
p :=Message.lpData;
Move(p^, dt.a, SizeOf(Integer));
Inc(p, SizeOf(Integer));
Move(p^, dt.b, SizeOf(Integer));
Inc(p, SizeOf(Integer));
GetMem(dt.pFileStr, StrLen(p) + 1);
Move(p^, dt.pFileStr^, StrLen(p) + 1);
end;



来自:jame, 时间:2000-9-25 20:41:00, ID:348950
public
 procedure Mymessage(var t:TWmCopyData);message WM_COPYDATA;
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.DFM}

{ TForm2 }

procedure TForm2.Mymessage(var t: TWmCopyData);
begin
 Edit1.text:=StrPas(t.CopyDataStruct^.lpData);//接受数据并显示。
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
 ds: TCopyDataStruct;
 hd: THandle;
begin
 ds.cbData := Length(Edit1.Text) + 1;
 GetMem(ds.lpData, ds.cbData); //为传递的数据区分配内存
 StrCopy(ds.lpData, PChar(Edit1.Text));
 Hd := FindWindow(nil, 'Form2'); // 获得接受窗口的句柄
 if Hd <> 0 then
SendMessage(Hd, WM_COPYDATA, Handle,
 Cardinal(@ds)) // 发送WM_COPYDATA消息
 else
ShowMessage('目标窗口没找到!');
 FreeMem(ds.lpData); //释放资源
end;


下面是共享内存法
unit ccc;
interface
uses
Windows, Messages, SysUtils,
Classes, Graphics, Controls, Forms,
Dialogs,ShellAPI, StdCtrls, Buttons;

const
WM_BARICON=WM_USER+200;

type
TForm1 = class(TForm)
btnwrite: TBitBtn;
btnread: TBitBtn;
Editwrite: TEdit;
Editread: TEdit;
procedure FormCreate(Sender: TObject);
procedure btnwriteClick(Sender: TObject);
procedure btnreadClick(Sender: TObject);
private
hMapfile&: THandle;
MapFilePointer: Pointer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
hMapFile := CreateFileMapping (
$FFFFFFFF, // 特殊内存映射句柄
nil, page_ReadWrite, 0,10000,
'DdhDemoMappedFile'); // 文件名
if hMapFile <> 0 then
MapFilePointer := MapViewOfFile (
hMapFile, // 上面映象文件的句柄
File_Map_All_Access,
0, 0, 0) // 访问整个映象文件
else
ShowMessage ('hMapFile = 0');
if MapFilePointer = nil then
ShowMessage ('MapFilePointer = nil');
end;

procedure TForm1.BtnWriteClick(Sender: TObject);
begin
StrCopy (PChar (MapFilePointer),
PChar (EditWrite.Text));//把内容写入共享内存
end;

procedure TForm1.BtnReadClick(Sender: TObject);
var
S: string;
begin
S := PChar (MapFilePointer);//从共享内存读出内容
EditRead.Text := S;
end;



end.
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭