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.