进程间数据共享

      DELPHI 2006-2-24 9:47
/*********************发送*********************/
//定义共享数据结构
PShare=^TShare;
TShare=record
Data:array[0..255] of char;
end;
//创建并打开共享
Hmaping:=windows.CreateFileMapping($ ffffffff,nil,PAGE_READWRITE,0,sizeof(TShare),pchar('share char'));
if Hmaping=0 then
begin
showmessage('create maping file fail!!');
application.Terminate;
exit;
end;

Pmem:=PShare(MapViewOfFile(Hmaping,FILE_MAP_ALL_ACCESS,0,0,0));
if Pmem=nil then
begin
closehandle(Hmaping);
showmessage('map file fail!');
application.Terminate;
exit;
end;
//拷贝数据并通知另一进程
fillchar(pmem^,sizeof(TShare),0);

CopyMemory(@(pmem^.data),pointer(str),length(str));
postmessage(findwindow(nil,pchar('getf')),WM_DATA,1,1);
/**********************接收******************/
//打开共享并初始化
maphandle:=windows.OpenFileMapping(FILE_MAP_WRITE,false,pchar('share char'));
if maphandle=0 then
begin
showmessage('open maping file fail!!');
application.Terminate;
exit;
end;

Pmem:=PShare(MapViewOfFile(maphandle,FILE_MAP_ALL_ACCESS,0,0,0));
if Pmem=nil then
begin
closehandle(maphandle);
showmessage('map file fail!');
application.Terminate;
exit;
end;

fillchar(Pmem^,sizeof(TShare),0);
//接受通知消息
procedure getmsg(var msg:TMessage);message WM_DATA;
procedure TForm1.getmsg(var msg: TMessage);
begin
if msg.LParam=1 then
begin
self.Edit1.Text:=Pmem^.Data;
end;
end;
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}