Delphi 两个应用程序(进程)之间的通信
兩個應用程序之間的通信實際上是兩個進程之間的通信。由于本人知識有限,決定應用消息來實現。需要用到的知識:
1.RegisterWindowMessage(); //參數類型:pchar;返回值:LongInt;
2.FindWindow(
??? lpClassName,??????? {窗口的類名}
??? lpWindowName: PChar {窗口的標題}
): HWND;????????????? {返回窗口的句柄; 失敗返回 0}
3.Wndproc();//每個窗口會有一個稱為窗口過程的回調函數(WndProc),它帶有四個參數,分別為:窗口句柄(Window Handle),消息ID(Message ID),和兩個消息參數(wParam, lParam)
4.PostMessage(); //該函數將一個消息放入(寄送)到與指定窗口創建的線程相聯系消息隊列里,不等待線程處理消息就返回,是異步消息模式。消息隊列里的消息通過調用GetMessage和PeekMessage取得。取得后交由WndProc進行處理。
好了,需要的知識都在這里了,現在開始我們的應用程序之間通信。
首先在兩個應用程序的主窗體的創建過程注冊消息,消息編號一定要不小于WM_USer,然后在程序1中得到程序2的主窗體句柄,并通過PostMessage向其發送消息;接下來在程序2的主窗體創建過程注冊和程序1相同編號的消息,然后重載程序2的Wndproc過程。廢話就不多說了,直接貼代碼:
程序1//
unit Unit1;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons;typeTForm1 = class(TForm)BitBtn1: TBitBtn;BitBtn2: TBitBtn;Edit1: TEdit;procedure FormShow(Sender: TObject);procedure BitBtn1Click(Sender: TObject);procedure BitBtn2Click(Sender: TObject);private{ Private declarations }strWM:Cardinal;procedure CallAgent(msg:string);public{ Public declarations }end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.BitBtn1Click(Sender: TObject); varh1: HWND; beginh1:= FindWindow(nil,'接收消息窗口');? //發送消息方法一PostMessage(h1,strWM,0,0); end;procedure TForm1.BitBtn2Click(Sender: TObject); beginCallAgent(Edit1.Text);?? //發送消息方法二 end;procedure TForm1.CallAgent(msg: string); varHlAgent:HWND;ds:TCopyDatastruct;??? //定義一個TCopyDatastruct結構體變量 beginds.cbData := (Length(Msg)+1)*SizeOf(Char); //結構體的第一個元素: 長度cbDataGetMem(ds.lpData,ds.cbData); //分配內存,結構體的第二個參數:? 數據的指針lpDATAtryStrCopy(ds.lpData,PChar(Msg)); //復制值到結構指針HlAgent :=FindWindow('TForm2','接收消息窗口');? //查找目標窗體的Handleif? HlAgent <> 0 thenbegin//ShowMessage('主' + IntToStr(Cardinal(@ds)));SendMessage(HlAgent,WM_COPYDATA,0,Cardinal(@ds));?? //發送WM_COPYDATA消息,并帶上參數 @dsend;finallyFreeMem(ds.lpData); //釋放數據內存end; end;procedure TForm1.FormShow(Sender: TObject); beginstrWM:= RegisterWindowMessage('UserDefMessage'); end;end. 程序2/unit Unit2;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;//const //? My_MousL = WM_USER+100; typeTForm2 = class(TForm)Edit1: TEdit;Label1: TLabel;procedure FormShow(Sender: TObject);private{ Private declarations }strWM:Cardinal;public{ Public declarations }procedure WndProc(var m:TMessage);override;procedure MyMessage(var m:TWmCopyData);message WM_CopyData;?? //定義一個消息響應過程,并傳入一個TWmCopyData的參數end;varForm2: TForm2;implementation{$R *.dfm}{ TForm2 }procedure TForm2.FormShow(Sender: TObject); beginstrWM := RegisterWindowMessage('UserDefMessage'); end;procedure TForm2.MyMessage(var m: TWmCopyData); varmsg:String;pStr:PChar; beginpStr := m.CopyDataStruct^.lpData;msg := system.SysUtils.StrPas(pStr);??? //獲取參數數據Edit1.Text := msg;? //顯示或者作其他處理 end;procedure TForm2.WndProc(var m: TMessage); beginif m.Msg = strWM thenEdit1.Text := Format('得到方式一發送的消息:%d',[m.Msg])elseinherited; end;end.至此,應用程序間通信就完成了,這里需要注意:FindWindow一定要找到你想要得到消息的應用程序,也就是說如果用FindWindow(nil,'Form2'),你一定得保證窗體的caption:= Form2的程序是唯一的。
---------------------------------------------------------------------------------------------------------------------------------
另:delphi 進程間通信的兩種方法
WIN下面進程間通信的最常用辦法就是消息了.
下面記錄兩種消息通信的方式:
?--------------------------------------------------------------------------------------------
一.第一種辦法,利用注冊Windows全局的消息.并覆蓋wndProc過程來監聽消息處理.
1. 發送消息方:
2. 接收消息方:
privatestrWM:Cardinal; //定義一個局部變量procedure wndProc(var msg:Tmessage);override; //覆蓋這個方法,可以監聽所有的Windows消息回調函數 ... proccedure Form1.Create(sender:TObject);beginstrWM:= RegisterWindowMessage('newspopMessage'); //注冊一個windows全局消息,這個相當于暗號end;procecure form1.wndProc(var msg:TMessage);begin//在這里處理這個消息就行了showmessage(strpas(PChar(meg.lparam))); //這樣寫會報錯的.但可以處理其它無參數的事情end;
----------------------------------------------------------------------------------------------
二.第二種辦法,發送一個WM_COPYDATA的消息.并且可以帶一個TCopyDataStruct的結構類型參數.
?1. 發送消息方:
2.接收方程序:
unit Unit2;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;//const //? My_MousL = WM_USER+100; typeTForm2 = class(TForm)Edit1: TEdit;Label1: TLabel;procedure FormShow(Sender: TObject);private{ Private declarations }strWM:Cardinal;public{ Public declarations }procedure WndProc(var m:TMessage);override;procedure MyMessage(var m:TWmCopyData);message WM_CopyData;?? //定義一個消息響應過程,并傳入一個TWmCopyData的參數end;varForm2: TForm2;implementation{$R *.dfm}{ TForm2 }procedure TForm2.FormShow(Sender: TObject); beginstrWM := RegisterWindowMessage('UserDefMessage'); end;procedure TForm2.MyMessage(var m: TWmCopyData); varmsg:String;pStr:PChar; beginpStr := m.CopyDataStruct^.lpData;msg := system.SysUtils.StrPas(pStr);??? //獲取參數數據Edit1.Text := msg;? //顯示或者作其他處理 end;procedure TForm2.WndProc(var m: TMessage); beginif m.Msg = strWM thenEdit1.Text := Format('得到方式一發送的消息:%d',[m.Msg])elseinherited; end;end.
?
轉載于:https://www.cnblogs.com/xieyunc/p/9126532.html
總結
以上是生活随笔為你收集整理的Delphi 两个应用程序(进程)之间的通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 广发临时额度要不要调
- 下一篇: 兴业银行股票为啥跌 具体原因解析