sendmessage沒反應就是沒反應... |
答題得分者是:christie
|
oliver1973
一般會員 ![]() ![]() 發表:3 回覆:6 積分:1 註冊:2007-11-30 發送簡訊給我 |
各位大大:
爬很多有關sendmessage的文了 2個exe之間想要用sendmessage互通訊息 但就是沒反應,請各位大大指教一下...感激 第一個sender_ap.exe [code delphi] unit sender; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var MyCopyDataStruct: TCopyDataStruct; hTargetWnd: HWND; begin with MyCopyDataStruct do //待傳送的數據 begin dwData := 0; // may use a value do identify content of message cbData := StrLen(PChar('message')) 1; //長度 lpData := PChar('message'); //內容 end; hTargetWnd := FindWindow(nil,PChar('reciver_ap')); // 查找窗体 if hTargetWnd <> 0 then SendMessage(hTargetWnd,WM_COPYDATA,0,Longint(@MyCopyDataStruct)); end; end. [/code] 第二個reciver_ap.exe [code delphi] unit reciver; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Label1: TLabel; private procedure WMCopyData(var Msg: TWMCopyData);message WM_CopyData; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.WMCopyData(var Msg: TWMCopyData); begin Label1.Caption:='ok'; end; end. [/code] |
christie
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
{ 請參考
The WM_COPYDATA messages makes it possible to transfer information between 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 structure of the following: } type TCopyDataStruct = packed record dwData: DWORD; // anwendungsspezifischer Wert cbData: DWORD; // Byte-Länge der zu übertragenden Daten lpData: Pointer; // Adresse der Daten end; {************************************************************************} {************************************************************************} { Sender Application } unit SenderApp; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Button2: TButton; Image1: TImage; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private procedure SendCopyData(hTargetWnd: HWND; ACopyDataStruct:TCopyDataStruct); public end; var Form1: TForm1; implementation {$R *.DFM} // Sender: Send data procedure TForm1.SendCopyData(hTargetWnd: HWND; ACopyDataStruct:TCopyDataStruct); begin if hTargetWnd <> 0 then SendMessage(hTargetWnd, WM_COPYDATA, Longint(Handle), Longint(@ACopyDataStruct)) else ShowMessage('No Recipient found!'); end; // Send Text from Edit1 to other app // Text von Edit1 an andere Anwendung schicken procedure TForm1.Button1Click(Sender: TObject); var MyCopyDataStruct: TCopyDataStruct; hTargetWnd: HWND; begin // Set up a COPYDATASTRUCT structure for use with WM_COPYDATA with MyCopyDataStruct do begin dwData := 0; // may use a value do identify content of message cbData := StrLen(PChar(Edit1.Text)) 1; //Need to transfer terminating #0 as well lpData := PChar(Edit1.Text) end; // Find the destination window for the WM_COPYDATA message hTargetWnd := FindWindow(nil,PChar('Message Receiver')); // send the structure to the receiver SendCopyData(hTargetWnd, MyCopyDataStruct); end; // Send Image1 to other app // Image1 an andere Anwendung schicken procedure TForm1.Button2Click(Sender: TObject); var ms: TMemoryStream; MyCopyDataStruct: TCopyDataStruct; hTargetWnd: HWND; begin ms := TMemoryStream.Create; try image1.Picture.Bitmap.SaveToStream(ms); with MyCopyDataStruct do begin dwData := 1; cbData := ms.Size; lpData := ms.Memory; end; // Search window by the window title // Empfänger Fenster anhand des Titelzeilentextes suchen hTargetWnd := FindWindow(nil,PChar('Message Receiver')); // Send the Data // Daten Senden SendCopyData(hTargetWnd,MyCopyDataStruct); finally ms.Free; end; end; end. {*********************************************************************} { Receiver Application } unit ReceiverApp; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls; type TForm1 = class(TForm) Image1: TImage; Label1: TLabel; private procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA; { Private-Deklarationen } public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.WMCopyData(var Msg: TWMCopyData); var sText: array[0..99] of Char; ms: TMemoryStream; begin case Msg.CopyDataStruct.dwData of 0: { Receive Text, Text empfangen} begin StrLCopy(sText, Msg.CopyDataStruct.lpData, Msg.CopyDataStruct.cbData); label1.Caption := sText; end; 1: { Receive Image, Bild empfangen} begin ms := TMemoryStream.Create; try with Msg.CopyDataStruct^ do ms.Write(lpdata^, cbdata); ms.Position := 0; image1.Picture.Bitmap.LoadFromStream(ms); finally ms.Free; end; end; end; end; end. //Form1.Caption:='Message Receiver'
------
What do we live for if not to make life less difficult for each other?
編輯記錄
christie 重新編輯於 2009-05-30 09:50:53, 註解 無‧
|
oliver1973
一般會員 ![]() ![]() 發表:3 回覆:6 積分:1 註冊:2007-11-30 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |