Windows Process Communication (1)-use wm_copydata

Source: Internet
Author: User

From: http://blog.sina.com.cn/s/blog_543ef0f4010008cx.html

How to Implement inter-process communication through the wm_copydata message

In Win32, The wm_copydata message is designed to allow the transfer of read-only data between processes.We recommend that you use the sendmessage () function in the SDK documentation.

The receiver does not return the data before the data replication is completed, so that the sender cannot delete or modify the data.. The prototype of this function is as follows:

Sendmessage (wm_copydata, wparam, lparam)

Wparam is set to the window handle containing data, and lparam points to a copydatastruct structure, which is defined:

typedef struct tagCOPYDATASTRUCT{
DWORD dwData;
DWORD cbData;
PVOID lpData; 
}COPYDATASTRUCT;

Dwdata is the custom data, cbdata is the data size, and lpdata is the pointer to the data.

Note that the wm_copydata message ensures that the data sent is copied from the original process to the target process. However, wm_copydata messages cannot be sent.

HDC, hbitmap, and other things are ineffective for target processes. The target process cannot get the data to do anything in the original process, because they belong

Different processes.

Like other process communication methods, to implement data communication between processes, in the data sending program, you must first find the window handle pwnd for receiving data processes.

Use cwnd ::Findwindow(Null, _ T ("datarecv") function, where the string "datarecv" is the name of the program that receives data. Then use

The sendmessage () function sends data. For more information, see the following example.

In the program that receives data, add the wm_copydata message ing in the message ing table, and then define the message ing function. The format of the function is:

BOOL CDataRecvDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
// Add custom program code
}

Instances for inter-process communication through wm_copydata messages

Unlike the preceding custom message, the wm_copydata message is a message provided by Win32. Compared with custom messages, wm_copydata messages can

To transmit a large data block. Here, we still use two dialog box programs to implement wm_copydata message communication.

The following are the sending functions of the data sending program and the receiving functions of the Data receiving program. In the dialog box class cdatasenddlg for sending data, use MFC

The classwizard tool or manual method adds the void cdatasenddlg: onsendcopydata () function. The specific code is as follows:

void CDataSendDlg::OnSendCopydata()
{
Updatedata (); // update data
Cwnd * pwnd = cwnd: findwindow (null, _ T ("datarecv"); // find the datarecv Process
 if(pWnd==NULL){
  AfxMessageBox("Unable to find DataRecv.");
  return;
 }
Copydatastruct CPD; // assign a value to the copydatastruct Structure
 cpd.dwData = 0;
 cpd.cbData = m_strCopyData.GetLength();
 cpd.lpData = (void*)m_strCopyData.GetBuffer(cpd.cbData);
Pwnd-> sendmessage (wm_copydata, null, (lparam) & CPD); // send
}


After you use MFC Appwizard (exe) to create a dialog box program for receiving data, a dialog box class cdatarecvdlg is generated. In this class, you must first define

You can use the classwizard tool to add the ing of wm_copydata messages. You can also manually add the ing, but you need to manually add three places:

① Add on_wm_copydata () in the message ing table ();

② Added the member function bool cdatarecvdlg: oncopydata ();

③ Added the definition of the wm_copydata message ing function in the cdatarecvdlg class.

The ing of wm_copydata messages is as follows:

BEGIN_MESSAGE_MAP(CDataRecvDlg, CDialog)
//{{AFX_MSG_MAP(CDataRecvDlg)
ON_WM_COPYDATA()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
The cdatarecvdlg: oncopydata () function is defined as follows:
BOOL CDataRecvDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
 m_strCopyData=(LPSTR)pCopyDataStruct->lpData;
// Obtain the string of the actual length
 m_strCopyData=m_strCopyData.Left(pCopyDataStruct->cbData);
// Update data
 UpdateData(FALSE);
 return CDialog::OnCopyData(pWnd, pCopyDataStruct);
}

M_strcopydata is the received string, and pcopydatastruct is the copydatastruct structure pointer. Note that the m_strcopydata String Length directly obtained by pcopydatastruct may not be the actual length of the sent string. You need to use the string length given when sending the string for further determination, the length is obtained by pcopydatastruct-> cbdata.

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.