如何在两个应用程序间通过自定义消息交换数据?(小弟不是小气鬼)(200分)

  • 主题发起人 主题发起人 banelon
  • 开始时间 开始时间
B

banelon

Unregistered / Unconfirmed
GUEST, unregistred user!
最近遇到点麻烦,想在两个应用程序间传输数据(字符串),要求快速、安全,请教各位大侠有没有好的解决方案,最好能有源代码?急急急!!!
 
&nbsp; &nbsp; 通过注册表,Ini文件,Dll,和发送消息都可以交换数据。<br>你说的用消息交换数据很容易,在一个程序中定义一个义消息,在<br>另一个程序中通过Lparam参数发给他就行了。这几种方式都符合你<br>的要求,不知你要什么样的程序说详细点可以给你一个。
 
用内存映象文件吧。<br><br><br>unit memfileu;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>&nbsp; RPtValue &nbsp; &nbsp; =record<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x,y:double;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Pt_id:integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pt_value:single;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pt_state:integer;<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pt_unit:string[10];<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>//读memfile<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; hMap:THandle;<br>&nbsp; data,d:^RPtValue;<br>&nbsp; i:integer;<br>begin<br>&nbsp; hMap:=CreateFileMapping(-1,nil,PAGE_READONLY,0,1024,'JZS_SHARE_MEMORY');<br>&nbsp; if hMap=NULL then exit;<br>&nbsp; data:=Mapviewoffile(hMap,FILE_MAP_READ,0,0,0);<br>&nbsp; d:=data;<br>&nbsp; for i:=0 to 25 do<br>&nbsp; begin<br>&nbsp; &nbsp; memo1.Lines.Add(inttostr(d^.Pt_id)+':'+floattostr(d^.pt_value)+':'+inttostr(d^.pt_state));<br>&nbsp; &nbsp; inc(d);<br>&nbsp; end;<br>end;<br><br>//创建/写memfile<br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; hFile:THandle;<br>&nbsp; hMapping:THandle;<br>&nbsp; data,d:^integer;<br>&nbsp; i:integer;<br>begin<br>&nbsp; hMapping:=CreateFileMapping($ffffffff,nil,PAGE_READWRITE ,0,1024,pchar('JZS_SHARE_MEMORY'));<br>&nbsp; data:=MapViewOfFile(hMapping,FILE_MAP_WRITE,0,0,0);<br><br>&nbsp; &nbsp; d:=data;<br>&nbsp; for i:=0 to 25 do<br>&nbsp; begin<br>&nbsp; &nbsp; d^:=ord('a')+i;<br>&nbsp; &nbsp; inc(d);<br>&nbsp; end;<br><br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>var<br>&nbsp; r: &nbsp;RPtValue;<br>begin<br>&nbsp; showmessage(inttostr(sizeof(r)));<br>end;<br><br>end.<br><br>
 
DDE也可以.
 
用剪贴板,可以试试。
 
实在懒的话,就看看Program Files/Borland/Delphi4/Demos/Ipcdemos下面<br>的例子,改一下就可以了.
 
1、<br>CLIENT端<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; hMapFile:=CreateFileMapping($FFFFFFFF,nil,page_ReadWrite,0,10000,'Ddh');<br>&nbsp; if hMapFile&lt;&gt;0 &nbsp;then<br>&nbsp; &nbsp; MapFilePointer:=MapViewOfFile(hMapFile,File_Map_All_Access,0,0,0)<br>&nbsp; else<br>&nbsp; &nbsp; ShowMessage('hMapFile==0');<br>&nbsp; if MapFilePointer=nil then<br>&nbsp; &nbsp; ShowMessage('MapFilePointer=nil');<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; StrCopy(PChar(MapFilePointer),PChar(edit2.text));<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; s:string;<br>begin<br>&nbsp; s:=PChar(MapFilePointer);<br>&nbsp; edit1.text:=s;<br>end;<br><br><br>Server 端<br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; h:Thandle;<br>&nbsp; MapFilePointer:Pointer;<br>begin<br>&nbsp; h:=OpenFileMapping(FILE_MAP_ALL_ACCESS,true,PChar('Ddh'));<br>&nbsp; MapFilePointer:=MapViewOfFile(h,File_Map_All_Access,0,0,0);<br>&nbsp; edit1.text:=PChar(MapFilePointer);<br>end;<br><br>2、<br>Server 端<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; ds:TCopyDataStruct;<br>&nbsp; hd:THandle;<br>begin<br>&nbsp; ds.cbData:=length(edit1.text)+1;<br>&nbsp; GetMem(ds.lpData,ds.cbData);<br>&nbsp; StrCopy(ds.lpData,PChar(edit1.text));<br>&nbsp; hd:=FindWindow(nil,'Form2');<br>&nbsp; if hd&lt;&gt;0 then<br>&nbsp; &nbsp; SendMessage(hd,WM_COPYDATA,Handle,Cardinal(@ds))<br>&nbsp; else<br>&nbsp; &nbsp; ShowMessage('Not Find');<br>&nbsp; FreeMem(ds.lpData);<br><br>end;<br><br>CLIENT端<br><br>type<br>&nbsp; TForm2 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure MyMessage(var t:TWmCopyData);message Wm_CopyData;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>procedure TForm2.MyMessage(var t:TWmCopyData);<br>begin<br>&nbsp; edit1.text:=strpas(t.copydatastruct^.lpData);<br>end;<br><br>3、使用dde<br>4、剪切板<br>5、CreateMailsolt<br>6、使用原子<br>..............
 
可以直接send WM_char message.
 
有一个WM_COPYDATA(大概是它,记不太清)可在两个应用间交换数据。<br>如果要自定义消息:<br>用API:RegisterWindowMessage,在两个程序间用相同的消息字符串<br>将要传输的数据放在LParam中就行了
 
menxin的是最简单的方法
 
感谢各位的意见,问题我已解决了,通过WM_COPYDATA系统消息发送数据,(KZL提到的),曾考虑用ATOM TABLE,可惜有255 BYTE 的限制,所以否定了,当然AMO提出的用内存映象文件,也是一种好方法,由于时间紧迫,来不急测试了。<br>
 
banelon:<br>通过鼠标钩子,东方快车和金山词霸都用这种方法。感兴趣可以给你一个例子。<br>请给我发个例子.<br>heruimin@263.net
 
hrm,请给我一个,谢谢了
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
I
回复
0
查看
623
import
I
后退
顶部