找到了 COPYDATASTRUCT cds; cds.dwData = 0; cds.cbData = sizeof( msg ); cds.lpData = &msg; 然后 ::SendMessage( m_hWndRecv, WM_COPYDATA,(WPARAM) m_hWnd, (LPARAM) &cds ); MSDN里面有一句话When you send a WM_COPYDATA message, SendMessage allocates a block of memory cbData bytes in size and copies the data from the caller's address space to this block. It then sends the message to the destination window. When the receiving window procedure processes this message, the lParam parameter is a pointer to a COPYDATASTRUCT structure that exists in the address space of the receiving process. The lpData member is a pointer to the copied block of memory, and the address reflects the memory location in the receiving process's address space 大意就是SendMessage自己分配了内存
MSDN还说了一点 The receiving application should consider the data read-only. The lParam parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by lParam. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer. 也就是说接受进程认为此时数据是只读的 因此 收到消息时 将信息copy到自己申请的内存区域 而不要直接去操作那段内存地址
(HWND) hWnd, // handle to destination window
WM_COPYDATA, // message to send
(WPARAM) wParam, // handle to window (HWND)
(LPARAM) lParam // data (PCOPYDATASTRUCT)
);typedef struct tagCOPYDATASTRUCT {
ULONG_PTR dwData;
DWORD cbData;
PVOID lpData;
} COPYDATASTRUCT, *PCOPYDATASTRUCT; SendMessage 的时候 LPARAM是一个COPYDATASTRUCT结构的指针
里面的PVOID就是数据了 需要将数据都COPY进去 而不是赋值一个指针接收者负责转换里面的数据
这个不是一个指向数据块的指针吗???你分配数据块的时候是在 进程1中分配的啊。那就必须用进程1中的堆里的空间怎么办???
cds.dwData = 0;
cds.cbData = sizeof( msg );
cds.lpData = &msg;
然后
::SendMessage( m_hWndRecv, WM_COPYDATA,(WPARAM) m_hWnd, (LPARAM) &cds );
MSDN里面有一句话When you send a WM_COPYDATA message, SendMessage allocates a block of memory cbData bytes in size and copies the data from the caller's address space to this block. It then sends the message to the destination window. When the receiving window procedure processes this message, the lParam parameter is a pointer to a COPYDATASTRUCT structure that exists in the address space of the receiving process. The lpData member is a pointer to the copied block of memory, and the address reflects the memory location in the receiving process's address space
大意就是SendMessage自己分配了内存
int x;
int y;
}msg , *pMsg;
注意cds.cbData = sizeof( msg ); 很重要
比如
进程1
COPYDATASTRUCT cds;
cds.dwData = 0;
cds.cbData = sizeof( msg );
cds.lpData = "12345";
SendMessage( m_hWndRecv, WM_COPYDATA,(WPARAM) m_hWnd, (LPARAM) &cds );进程2的消息处理函数中直接
MessageBox(NULL,cds.lpData,NULL,0);就可以了。
COPYDATASTRUCT cds;
cds.dwData = 0;
cds.cbData = 6;
cds.lpData = "12345";
SendMessage( m_hWndRecv, WM_COPYDATA,(WPARAM) m_hWnd, (LPARAM) &cds );进程2的消息处理函数中直接
MessageBox(NULL,cds.lpData,NULL,0);就可以了。
The receiving application should consider the data read-only. The lParam parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by lParam. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer. 也就是说接受进程认为此时数据是只读的 因此 收到消息时 将信息copy到自己申请的内存区域 而不要直接去操作那段内存地址