The simplest way to communicate between processes is to send wm_copydata messages, which are implemented in the following example.
Send Wm_copydata message :
SendMessage (Hrecvwnd,
Wm_copydata,
(WPARAM) Hsendwnd,
(LPARAM) ©data);
The CopyData is the copydatastruct struct type, and the structure is defined as follows:
typedef struct TAGCOPYDATASTRUCT {
DWORD Dwdata;
DWORD cbdata;
PVOID lpdata;
} copydatastruct;
Parameters:
Dwdata; //Specifies data to being passed to the receiving application.
cbdata; //specifies the size, in bytes, the of the data pointed to by the Lpdata member.
lpdata; //Pointer to data to being passed to the receiving application. Can is NULL.
Note : The message can only be sent by SendMessage (), not by using PostMessage (). Because the system must manage the lifetime of the buffer used to pass the data, if PostMessage () is used, the data buffer is purged and reclaimed by the system before the recipient (thread) has the opportunity to process the data. Also be careful when lpdata points to an object with a pointer or a virtual function.
If the incoming handle is not a valid window or when the receiver process terminates unexpectedly, SendMessage () returns immediately, so the sender does not fall into an infinite wait state in this case.
Receive Wm_copydata message :
Just use copydatastruct *pcopydata = (copydatastruct*) LParam; Yes, the receiver should assume that the data is read-only.
Because the sender is waiting before the receiver processes the WM_COPYDATA message, the receiving party should process the WM_COPYDATA message as soon as possible.
Code Implementation :
//Send side:voidCsendcopydatadlg::onbnclickedbtnsend () {CString strsendtext; M_editsend.getwindowtext (Strsendtext); if(Strsendtext.isempty ()) {AfxMessageBox (_t ("send content cannot be empty! ")); return; } //gets the Receive side window handleHWND hwnd =:: FindWindow (null,_t ("Recvcopydata")); if(HWnd = =NULL) {AfxMessageBox (_t ("The data receiving end is not started! ")); return; } //Send Datacopydatastruct CopyData; Copydata.dwdata=1; Copydata.cbdata = Strsendtext.getlength () *2; //Allocate sufficient buffer length to prevent garbled reception Copydata.lpdata= (void*) Strsendtext.getbuffer (); :: SendMessage (HWnd, Wm_copydata, NULL, (LPARAM)©data); Strsendtext.releasebuffer ();} //Receive end:afx_msg BOOL oncopydata(CWnd* PWnd, copydatastruct*pcopydatastruct); On_wm_copydata () BOOL Crecvcopydatadlg::oncopydata(CWnd*pWnd, Copydatastruct*pcopydatastruct) { //append data in the edit boxSYSTEMTIME St; CString Strdatetime; GetSystemTime (&St); Strdatetime.format (_t ("%04d-%02d-%02d%02d:%02d:%02d.%0 4d"), St.wyear, St.wmonth, St.wday, St.whour, St.wminute, St.wsecond, S T.wmilliseconds); CString Strrecvtext= Strdatetime + (LPWSTR) (pcopydatastruct->lpdata); M_richeditrecv.setsel (-1, -1); M_richeditrecv.replacesel (Strrecvtext+ _t ("\ n")); returnCdialog::oncopydata (pWnd, pcopydatastruct);}
Compile run :
Example Download: Http://pan.baidu.com/s/1i5n6Kex
Using WM_COPYDATA messages for interprocess communication