The use of SendMessage and Wm_copydata

Source: Internet
Author: User

SendMessage two parameters, one is wparam (UINT), the other is lparam (LONG), which is an API function. is the same in VS and CB.

Passing strings, if this is the process, Pseudocode is as follows:
String str = "Test string";
:: SendMessage (handle, custom message, str. Length, LPARAM (Str.c_str ()));
Or
TCHAR sz[] = "Test string";
:: SendMessage (handle, custom message, _tcslen (SZ), LPARAM (SZ));

Using WM_COPYDATA message Communication

For small amounts of data, communication can be easily implemented with Wm_copydata. Because SendMessage () is blocked, only the receiver responds to the message, SendMessage () can return, or it blocks. Therefore, for a large number of data, using SendMessage () is easy to cause the window to suspend animation.
Methods of interprocess communication through WM_COPYDATA messages

In Win32, the primary purpose of the WM_COPYDATA message is to allow read-only data to be passed between processes. The SDK documentation recommends that users use the SendMessage () function, which does not return until the data is replicated, so that it is not possible for the sender to delete and modify the data. The prototype of this function is as follows:

SendMessage (Wm_copydata,wparam,lparam)

Where wparam is set to a window handle that contains data, lparam points to a COPYDATASTRUCT structure defined as:

typedef struct tagcopydatastruct{

DWORD Dwdata;

DWORD cbdata;

PVOID Lpdata;

}copydatastruct;

Where Dwdata is the custom data, cbdata is the data size, lpdata is the pointer to the data. It is to be noted that the WM_COPYDATA message guarantees that the data sent is copied from the original process to the target process. However, wm_copydata messages cannot send things like HDC, HBITMAP, and so on, which are not valid for the target process. The target process does not get the data to do anything in the original process because they belong to different processes.

As with other process communication methods, in order to achieve data communication between processes, in the program that sends the datum, first to find the window handle pwnd that receives the data process, you can use the Cwnd::findwindow (null,_ T ("datarecv")) function to get, where the string " Datarecv "is the name of the program that receives the data. The data is then sent using the SendMessage () function.

In the program that receives the data, first add the WM_COPYDATA message mapping in the message map table, and then define the message mapping function whose function is in the form:

BOOL Cdatarecvdlg::oncopydata (cwnd* pwnd, copydatastruct* pcopydatastruct)

{

Adding user-defined program code

...

}

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.