Gold mine of Visual C++ tricks!
WM_COPYDATA can be used as simple IPC mechanism.
![]()
Sending and receiving data between processes are one of the greatest headaches that we face. For that many mechanisms are there – such as COM, Sockets, but a bit complicated… There is comparatively simple mechanism for doing the same. i.e. WM_COPYDATA window message.
![]()
For IPC communication between two processes, you just need a hidden window created in each and by using the window handle in target process you can send data to it. For sending data via WM_COPYDATA, the following are requirements.
WPARAM – Handle of window in target process.
LPARAM – Pointer to COPYDATASTRUCT which contains data to be exported.
Please see the following code block about how to use it.
void CMyDialog::SendData( HWND hTargetWindow_i,
void* pData_i,
DWORD dwSize_i )
{
// structure holding data information.
COPYDATASTRUCT stCopyData = { 0 };
stCopyData.lpData = pData_i;
stCopyData.dwData = dwSize_i;
// Send the data.
SendMessage( WM_COPYDATA,
(UINT) hTargetWindow_i,
(ULONG) &stCopyData );
}
In the receiving Window, if you are using MFC, you need to handle the WM_COPYDATA message by adding message handle using ON_WM_COPYDATA() and handler function with proto -
afx_msg BOOL OnCopyData( CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct );
![]()
Targeted Audience _ intermediate.
| Print article | This entry was posted by Jijo Raj on April 12, 2008 at 5:47 pm, and is filed under Codeproject, MFC. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |










about 2 years ago
I think the following sentence should be corrected…
“There is comparatively simple mechanism in MFC for doing the same. i.e. WM_COPYDATA window message”
Because WM_COPYDATA has no relation to MFC. It is an independant windows message.
about 2 years ago
Yes. You are right. What happened is, first i found – ON_WM_COPYDATA() from MSDN while digging MFC and then i reached to WM_COPYDATA. Thats why the term MFC stick in my mind. I’ll modify it. thanks for pointing it out.
about 1 year ago
Maybe i’m missing something:
This doesn’t release you from other communication mechanisms, as you still need to somehow communicate the target window handle across processes. Don’t you?