The simplest way to complete interprocess communication is to send a wm_copydata message.
(1) Send Wm_copydata message
SendMessage (Receive window handle, Wm_copydata, (WPARAM) Send window handle, (LPARAM) ©data);
Where the copydata is the data to be sent, the type is copydatastruct struct:
typedef struct TAGCOPYDATASTRUCT
{
DWORD Dwdata;
DWORD cbdata;
PVOID lpdata;
} copydatastruct;
dwdata : Specifies up to $ bits of data to being passed to the receiving application.
cbdata : Specifies the size, in bytes, of the data pointed to by the Lpdata member.
lpdata : Long pointer to data to being passed to the receiving application. This member can is NULL.
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.
If the incoming receive window handle is invalid or when the receiver process terminates unexpectedly, SendMessage () returns immediately and the sender does not fall into an infinite wait state.
Also be aware that:
The data being passed must not contain pointers or other references to objects not accessible to the application receiving The data. (The information sent cannot contain pointers or object references that the data receiver cannot access)
While this message was being sent, the referenced data must not being changed by another thread of the sending process. (after the message is sent, Make sure that the data referenced by lpdata cannot be modified by another thread (until the SendMessage function returns))
Sample code Snippet (MFC):
" Copydatareceiver " ); if (Receivewindow = = NULL ) return 0== senddata.getlength ();:: SendMessage (Receivewindow, Wm_copydata , (WPARAM) GetSafeHwnd (), (LPARAM)&CopyData); Senddata.releasebuffer ();
(2) Receive WM_COPYDATA message:
wm_copydata
wparam = (WParam) (HWND) HWND;
lparam = (LParam) (pcopydatastruct) PCDs;
 
hwnd : Handle to the window passing the data. (a handle to the sender, which can be fed back to the sender via this handle)
PCDs : Pointer to a COPYDATASTRUCT structure that contains the data to being passed.
 
Return Values
if the receiving application processes this message, it should return TRUE; otherwise, it should return FA LSE.
The lparam contains the received data and needs to be converted to the copydatastruct struct before processing. The receiver should assume that the data is read-only and is valid only in the process of processing the message. If you want to modify this data, you should copy them to local buffer. (The receiving application should consider the data read-only. The PCDs parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by PCDs. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer. )
Because the sender is waiting (SendMessage blocking) before the receiver processes the WM_COPYDATA message, the receiver should process the WM_COPYDATA message as soon as possible.
Sample code Snippet (MFC):
BOOL Ccopydatareceiverdlg::oncopydata (cwnd* pWnd, copydatastruct* pcopydatastruct) { if0 { char recvdata[]= {0}; strncpy (RecvData, (Char *) pcopydatastruct->lpdata, pcopydatastruct->cbdata); Setdlgitemtext (idc_edit_receive, (Char *) recvdata); Feedback (pWnd); } return Cdialog::oncopydata (pWnd, pcopydatastruct);}
Resources:
[1] MSDN
[2] http://blog.csdn.net/morewindows/article/details/6804157
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Complete interprocess communication with WM_COPYDATA messages