两个进程间如何发送消息(要传自定义结构)(80)

  • 主题发起人 主题发起人 taibu
  • 开始时间 开始时间
T

taibu

Unregistered / Unconfirmed
GUEST, unregistred user!
如题! 希望能给出代码!
 
进程之间发送字符串 procedure PostStrToOther(H: HWND; OtherStr: string); var DS: TCopyDataStruct; begin ShowMessage('发送:' + OtherStr); Ds.cbData := Length(OtherStr) + 1; //或者是结构体长度 sizeof(结构体变量) GetMem(Ds.lpData, Ds.cbData); StrCopy(ds.lpData, PChar(OtherStr)); SendMessage(H, WM_COPYDATA, Application.Handle, Cardinal(@ds)); FreeMem(Ds.lpData); end; procedure TFrmOther.MyMessage(var t: TWMCopyData); //Message WM_COPYDATA; var tmpStr:String; begin //接受消息并显示。 tmpStr:=StrPas(t.CopyDataStruct^.lpData); Memo1.Lines.Add(tmpStr); end;
 
TCopyDataStruct改成我的自定义结构体就行了吗?
 
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type _MyRecord = record Int: Integer; Name: string[50]; end; PMyRecord = ^_MyRecord; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } procedure MyMessage(var t: TWMCopyData); Message WM_COPYDATA; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure PostInfoToOther(H: HWND; AInfo: _MyRecord);var DS: TCopyDataStruct;begin Ds.cbData := Sizeof(AInfo); GetMem(Ds.lpData, Ds.cbData); PMyRecord(Ds.lpData)^ := AInfo; SendMessage(H, WM_COPYDATA, Application.Handle, Cardinal(@ds)); FreeMem(Ds.lpData);end;{ TForm1 }procedure TForm1.MyMessage(var t: TWMCopyData);var Info:_MyRecord;begin Info := PMyRecord(t.CopyDataStruct.lpData)^; ShowMessage( Info.Name );end;procedure TForm1.Button1Click(Sender: TObject);var test:_MyRecord;begin test.Int:=30; test.Name:='ZNXIA'; PostInfoToOther(Self.Handle,test);//测试,就直接发给当前窗体句柄了,发给其它窗体也一样,呵呵end;end.===================给分,嘿嘿
 
后退
顶部