Using WM_COPYDATA messages for interprocess communication

Source: Internet
Author: User

  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) &copydata);

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)&copydata); 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.