Reprint: http://www.cnblogs.com/renyuan/p/5037536.html
VC + + in two programs to pass a constant value such as String method: The use of the WM_COPYDATA message
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 msg[] = "The girl does not cry"; Cds.dwdata = 0; Cds.lpdata = msg; Cds.cbdata = strlen (msg) +1; String please remember to add ' plus ', or else it would be wrong, here is the ANSI string SendMessage (FindWindow ("Nbsg_class", NULL), Wm_copydata, 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 transfer a constant value such as String method: Using the WM_COPYDATA message (reprint)