3.4 use wm_copydata for Message Communication
For a small amount of data, wm_copydata can be used for convenient communication. Because sendmessage () is blocked, only sendmessage () can be returned if the receiver responds to the message. Otherwise, it will be blocked all the time. Therefore, for a large amount of data, the use of sendmessage () can easily lead to false window death.
3.4.1 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 data before the data is copied, so that the sender cannot delete or modify 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 send HDC, hbitmap, and so on. They are invalid for target processes. The target process cannot get the data to do anything in the original process, because they belong to different processes.
Like other processes, to implement data communication between processes, in the data sending program, you must first find the window handle pwnd for receiving data processes. You can use cwnd :: findwindow (null, _ T ("datarecv") function. The string "datarecv" is the program name for receiving data. Then, use the sendmessage () function to send 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
...
}
3.4.2 instances for inter-process communication through the wm_copydata message
Unlike the preceding custom message, the wm_copydata message is a message provided by Win32. Compared with custom messages, wm_copydata messages can 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 the MFC classwizard tool or manually add the void cdatasenddlg: onsendcopydata () function. The 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 the ing for receiving the wm_copydata message. You can use the classwizard tool to add the message or manually add the message, but you need to modify the following three items manually: ① add on_wm_copydata () in the message ing table; ② Add the member function bool cdatarecvdlg: oncopydata (); ③ Add 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.