VC + + in two programs to pass a constant value such as String method: The use of the WM_COPYDATA message

Source: Internet
Author: User

Message function:
Sharing data between processes (internally by creating a memory-mapped file)

Message Description:
Data structure/type to use:
typedef struct TAGCOPYDATASTRUCT {
ULONG_PTR Dwdata;
DWORD cbdata;
PVOID lpdata;
} copydatastruct, *pcopydatastruct;

Structure parameter Description:
Dwdata (ULONG) holds a value that can be used as a marker, etc.
Lpdata (void*) The starting address of the data to be sent (can be null)
Cbdata (DWORD) length of data to be sent
Parameters of the message:
HWND: Handle to the window that receives the data
WParam: The window handle that transmits the data (null also doesn't matter)
Address of the LPARAM:COPYDATASTRUCT type variable
Examples of Use:

    copydatastruct CDs;     Char " Girls Don't Cry " ;     0 ;     = msg;     = strlen (msg) +1// string Please remember to add ' plus ', otherwise it's wrong, here is the ANSI string    SendMessage (FindWindow ( " Nbsg_class " 0, (LPARAM) &cds);

Note: If you are sending a string of type CString, you can do so

CString str = _t (" girl does not cry "); char* msg = str. GetBuffer (0);

So that it can be converted into a char* type.

--------------------------------------------
A possible processing of the message by the receiving end:

BOOL Cxuanzedlg::oncopydata (cwnd* pWnd, copydatastruct* pcopydatastruct) {    //  TODO:  Add the message handler code here and/or call the default value    copydatastruct* PCDS = (copydatastruct*) pcopydatastruct;    CString Str ((Char*) pcds->lpdata);    Str. Format (_t ("%s  flag:%d"), str, pcds->dwdata);    AfxMessageBox (str);     return Cdialogex::oncopydata (pWnd, pcopydatastruct);}


Description
Passing data using the WM_COPYDATA message is actually the data that will be passed to the shared memory, and the address is passed to the other process.

In this message, there is a flag dwdata this data is a ULONG_PTR type, an unsigned long integer, which can be used as a flag for the data. Such different flags can be used for different uses of the data passed!

VC + + in two programs to pass a constant value such as String method: Using the WM_COPYDATA message of the

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.