共享内存方式或发copydata消息 下面代码仅仅演示,细节错误不予考虑procedure Init;vars : pchar;h : hwnd;buf:tagCOPYDATASTRUCT;beginh := FindWindow('TForm1', 'Form1');if h <> 0 thenbeginGetMem(s,100);buf.lpData :=s;buf.cbData:=100;buf.dwData :=100;strpcopy(s, ParamStr(0));SendMessage(h, WM_COPYDATA, 0, integer(@buf));end;end;procedure TForm1.WM_COPYDATA(var msg: TMessage);varP:^tagCOPYDATASTRUCT;beginp:=Pointer(Msg.lParam);ShowMessage(strpas(p.lpData));end;***********************************************************************************{The WM_COPYDATA messages makes it possible to transfer informationbetween processes. It does this by passing the data through the kernel.Space is allocated in the receiving process to hold the information that is copied,by the kernel, from the source process to the target process.The sender passes a pointer to a COPYDATASTRUCT, which is defined as a structureof the following:}typeTCopyDataStruct = packed recorddwData: DWORD; // anwendungsspezifischer WertcbData: DWORD; // Byte-L?nge der zu übertragenden DatenlpData: Pointer; // Adresse der Datenend;{ Sender Application }unit SenderApp;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, ExtCtrls;typeTForm1 = class(TForm)Button1: TButton;Edit1: TEdit;Button2: TButton;Image1: TImage;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);privateprocedure SendCopyData(hTargetWnd: HWND; ACopyDataStruct:TCopyDataStruct);publicend;varForm1: TForm1;implementation{$R *.DFM}// Sender: Send data// Sender: Daten schickenprocedure TForm1.SendCopyData(hTargetWnd: HWND; ACopyDataStruct:TCopyDataStruct);beginif hTargetWnd <> 0 thenSendMessage(hTargetWnd, WM_COPYDATA, Longint(Handle), Longint(@ACopyDataStruct))elseShowMessage('No Recipient found!');end;// Send Text from Edit1 to other app// Text von Edit1 an andere Anwendung schickenprocedure TForm1.Button1Click(Sender: TObject);varMyCopyDataStruct: TCopyDataStruct;hTargetWnd: HWND;begin// Set up a COPYDATASTRUCT structure for use with WM_COPYDATA// TCopyDataStruct mit den Sende-Daten Infos ausfüllenwith MyCopyDataStruct dobegindwData := 0; // may use a value do identify content of messagecbData := StrLen(PChar(Edit1.Text)) + 1; //Need to transfer terminating #0 as welllpData := PChar(Edit1.Text)end;// Find the destination window for the WM_COPYDATA message// Empf?nger Fenster anhand des Titelzeilentextes suchenhTargetWnd := FindWindow(nil,PChar('Message Receiver'));// send the structure to the receiver// Die Struktur an den Empf?nger schickenSendCopyData(hTargetWnd, MyCopyDataStruct);end;// Send Image1 to other app// Image1 an andere Anwendung schickenprocedure TForm1.Button2Click(Sender: TObject);varms: TMemoryStream;MyCopyDataStruct: TCopyDataStruct;hTargetWnd: HWND;beginms := TMemoryStream.Create;tryimage1.Picture.Bitmap.SaveToStream(ms);with MyCopyDataStruct dobegindwData := 1;cbData := ms.Size;lpData := ms.Memory;end;// Search window by the window title// Empf?nger Fenster anhand des Titelzeilentextes suchenhTargetWnd := FindWindow(nil,PChar('Message Receiver'));// Send the Data// Daten SendenSendCopyData(hTargetWnd,MyCopyDataStruct);finallyms.Free;end;end;end.{*********************************************************************}{ Receiver Application }unit ReceiverApp;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ExtCtrls, StdCtrls;typeTForm1 = class(TForm)Image1: TImage;Label1: TLabel;privateprocedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;{ Private-Deklarationen }public{ Public-Deklarationen }end;varForm1: TForm1;implementation{$R *.DFM}procedure TForm1.WMCopyData(var Msg: TWMCopyData);varsText: array[0..99] of Char;ms: TMemoryStream;begincase Msg.CopyDataStruct.dwData of0: { Receive Text, Text empfangen}beginStrLCopy(sText, Msg.CopyDataStruct.lpData, Msg.CopyDataStruct.cbData);label1.Caption := sText;end;1: { Receive Image, Bild empfangen}beginms := TMemoryStream.Create;trywith Msg.CopyDataStruct^ doms.Write(lpdata^, cbdata);ms.Position := 0;image1.Picture.Bitmap.LoadFromStream(ms);finallyms.Free;end;end;end;end;end
写个例子给你吧.1. //程序H向程序A发送数据 //数据内容为Edit1的文本内容 //程序A的窗体名字为FromA procedure TFormH.Button1Click(Sender: TObject); var DataStruct: TCopyDataStruct; h: THandle; begin DataStruct.cbData := Length (Edit1.Text) + 1; GetMem (DataStruct.lpData, DataStruct.cbData ); StrCopy (DataStruct.lpData, PChar (Edit1.Text)); h:= FindWindow (nil, 'FormA'); if h <> 0 then SendMessage (h, WM_COPYDATA, Handle,Cardinal(@DataStruct)) FreeMem (DataStruct.lpData); end; 2. //程序A接受来自程序H的数据,并显示在Edit1上 ... type TFormA = class(TForm) ... public procedure ReceiveData(var Data:TWmCopyData);Message WM_COPYDATA; ...procedure TFormA.ReceiveDta(var Data:TWmCopyData); begin Edit1.Text:=StrPas(Data.CopyDataStruct^.lpData); end;
以前也有用DDE的.
下面代码仅仅演示,细节错误不予考虑procedure Init;vars : pchar;h : hwnd;buf:tagCOPYDATASTRUCT;beginh := FindWindow('TForm1', 'Form1');if h <> 0 thenbeginGetMem(s,100);buf.lpData :=s;buf.cbData:=100;buf.dwData :=100;strpcopy(s, ParamStr(0));SendMessage(h, WM_COPYDATA, 0, integer(@buf));end;end;procedure TForm1.WM_COPYDATA(var msg: TMessage);varP:^tagCOPYDATASTRUCT;beginp:=Pointer(Msg.lParam);ShowMessage(strpas(p.lpData));end;***********************************************************************************{The WM_COPYDATA messages makes it possible to transfer informationbetween processes. It does this by passing the data through the kernel.Space is allocated in the receiving process to hold the information that is copied,by the kernel, from the source process to the target process.The sender passes a pointer to a COPYDATASTRUCT, which is defined as a structureof the following:}typeTCopyDataStruct = packed recorddwData: DWORD; // anwendungsspezifischer WertcbData: DWORD; // Byte-L?nge der zu übertragenden DatenlpData: Pointer; // Adresse der Datenend;{ Sender Application }unit SenderApp;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, ExtCtrls;typeTForm1 = class(TForm)Button1: TButton;Edit1: TEdit;Button2: TButton;Image1: TImage;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);privateprocedure SendCopyData(hTargetWnd: HWND; ACopyDataStruct:TCopyDataStruct);publicend;varForm1: TForm1;implementation{$R *.DFM}// Sender: Send data// Sender: Daten schickenprocedure TForm1.SendCopyData(hTargetWnd: HWND; ACopyDataStruct:TCopyDataStruct);beginif hTargetWnd <> 0 thenSendMessage(hTargetWnd, WM_COPYDATA, Longint(Handle), Longint(@ACopyDataStruct))elseShowMessage('No Recipient found!');end;// Send Text from Edit1 to other app// Text von Edit1 an andere Anwendung schickenprocedure TForm1.Button1Click(Sender: TObject);varMyCopyDataStruct: TCopyDataStruct;hTargetWnd: HWND;begin// Set up a COPYDATASTRUCT structure for use with WM_COPYDATA// TCopyDataStruct mit den Sende-Daten Infos ausfüllenwith MyCopyDataStruct dobegindwData := 0; // may use a value do identify content of messagecbData := StrLen(PChar(Edit1.Text)) + 1; //Need to transfer terminating #0 as welllpData := PChar(Edit1.Text)end;// Find the destination window for the WM_COPYDATA message// Empf?nger Fenster anhand des Titelzeilentextes suchenhTargetWnd := FindWindow(nil,PChar('Message Receiver'));// send the structure to the receiver// Die Struktur an den Empf?nger schickenSendCopyData(hTargetWnd, MyCopyDataStruct);end;// Send Image1 to other app// Image1 an andere Anwendung schickenprocedure TForm1.Button2Click(Sender: TObject);varms: TMemoryStream;MyCopyDataStruct: TCopyDataStruct;hTargetWnd: HWND;beginms := TMemoryStream.Create;tryimage1.Picture.Bitmap.SaveToStream(ms);with MyCopyDataStruct dobegindwData := 1;cbData := ms.Size;lpData := ms.Memory;end;// Search window by the window title// Empf?nger Fenster anhand des Titelzeilentextes suchenhTargetWnd := FindWindow(nil,PChar('Message Receiver'));// Send the Data// Daten SendenSendCopyData(hTargetWnd,MyCopyDataStruct);finallyms.Free;end;end;end.{*********************************************************************}{ Receiver Application }unit ReceiverApp;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ExtCtrls, StdCtrls;typeTForm1 = class(TForm)Image1: TImage;Label1: TLabel;privateprocedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;{ Private-Deklarationen }public{ Public-Deklarationen }end;varForm1: TForm1;implementation{$R *.DFM}procedure TForm1.WMCopyData(var Msg: TWMCopyData);varsText: array[0..99] of Char;ms: TMemoryStream;begincase Msg.CopyDataStruct.dwData of0: { Receive Text, Text empfangen}beginStrLCopy(sText, Msg.CopyDataStruct.lpData, Msg.CopyDataStruct.cbData);label1.Caption := sText;end;1: { Receive Image, Bild empfangen}beginms := TMemoryStream.Create;trywith Msg.CopyDataStruct^ doms.Write(lpdata^, cbdata);ms.Position := 0;image1.Picture.Bitmap.LoadFromStream(ms);finallyms.Free;end;end;end;end;end
//程序H向程序A发送数据
//数据内容为Edit1的文本内容
//程序A的窗体名字为FromA
procedure TFormH.Button1Click(Sender: TObject);
var
DataStruct: TCopyDataStruct;
h: THandle;
begin
DataStruct.cbData := Length (Edit1.Text) + 1;
GetMem (DataStruct.lpData, DataStruct.cbData );
StrCopy (DataStruct.lpData, PChar (Edit1.Text));
h:= FindWindow (nil, 'FormA');
if h <> 0 then
SendMessage (h, WM_COPYDATA, Handle,Cardinal(@DataStruct))
FreeMem (DataStruct.lpData);
end;
2.
//程序A接受来自程序H的数据,并显示在Edit1上
...
type
TFormA = class(TForm)
...
public
procedure ReceiveData(var Data:TWmCopyData);Message WM_COPYDATA;
...procedure TFormA.ReceiveDta(var Data:TWmCopyData);
begin
Edit1.Text:=StrPas(Data.CopyDataStruct^.lpData);
end;