Windows process Communication--WM_COPYDATA messages

Source: Internet
Author: User
Tags sprintf

Wm_copydata messages, which are used in Win32 for inter-process data transfer.

struct tagcopydatastruct {  //    DWORD dwdata;     DWORD cbdata;     PVOID lpdata; } copydatastruct;

Where Dwdata is a 32-bit custom data, lpdata is a pointer to the data, cbdata the lpdata pointer to the size (in bytes) of the data.

It is generally recommended to send using the SendMessage function, which ensures that the sender is prevented from modifying or deleting the data before the recipient replicates the data;

LRESULT SendMessage (  hwnd hwnd,      //  Handle of destination window  UINT Msg,       // message to send  WPARAM WPARAM,  // First message parameter  LPARAM LPARAM   //  Second message parameter);

Over here:

HWnd: The form handle for the receiving process;

Msg:wm_copydata;
WParam: The form handle for the sending process;

LParam: Pointer to the copydatastruct data structure;

It is important to note that the transmitted CopyData data is sent to avoid being modified by other threads in the sending process, and secondly to ensure that the transmitted data does not contain pointers or references to objects that the receiving process cannot access. For example: Send the Process own hdc, HBITMAP and other things, they are not valid for the receiving process. In addition, the receive process form handle can be obtained through the FindWindow function:

Const Char " receiveprocess " ;  HWND Hrecvwindow  =:: FindWindow (NULL, szdlgtitle);

Here the "receiveprocess" is the name of the receiving process;

At the same time, in the process of receiving the data, first add the WM_COPYDATA message map in the message map table, then define the message map function, the format of the function is:

BOOL Ccancollectordlg::oncopydata (cwnd* pWnd, copydatastruct* pcopydatastruct) {    ...}

The main code is as follows: (here the sending process sends the current time to the receiving process)

Send process:

voidCprocesscommdlg::onbtnstart1 () {Const CharSzdlgtitle[] ="receiveprocess"; HWND Hsendwindow= This-GetSafeHwnd (); HWND Hrecvwindow=:: FindWindow (NULL, szdlgtitle);     Copydatastruct CopyData; Charszsendbuf[ -];     time_t TimeNow; Time (&TimeNow); sprintf (Szsendbuf,"%s", CTime (&timenow));//Note that the string returned by CTime () is followed by ' \ n 'Copydata.dwdata =0; Copydata.cbdata=strlen (SZSENDBUF); Szsendbuf[copydata.cbdata-1] =' /'; Copydata.lpdata=Szsendbuf; :: SendMessage (Hrecvwindow, Wm_copydata, (WPARAM) Hsendwindow, (LPARAM)&copydata); printf ("%s\n", SZSENDBUF); }

Receive process:

Begin_message_map (Ccancollectordlg, CDialog)//{{Afx_msg_map (CCANCOLLECTORDLG)On_wm_syscommand () on_wm_paint () On_wm_querydragicon () On_wm_copydata ( )//}}afx_msg_mapEnd_message_map () ... BOOL Ccancollectordlg::oncopydata (CWnd* PWnd, copydatastruct*pcopydatastruct) {    Charszbuffer[ -]; memset (Szbuffer,0,sizeof(szbuffer)); sprintf (Szbuffer,"dwdata:%d cbdata:%d\r\nlpdata:0x%08x =%s\r\n\r\n", Pcopydatastruct->dwdata, pcopydatastruct->cbdata, (PVOID) pcopydatastruct->lpdata, (Char*) pcopydatastruct->lpdata); returnCdialog::oncopydata (pWnd, pcopydatastruct);}

Windows process Communication--WM_COPYDATA messages

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.