使用WM_COPYDATA的消息通信,保证发送数据从原进程到目标进程,允许在进程间传递少量的只读数据,使用sendmessage()函数,接收方在数据完成复制前不能返回。
//函数原型
LRESULT SendMessage( HWND hWnd, //接受消息的窗口句柄 UINT Msg, //要发送的消息,这里是WM_COPYDATA WPARAM wParam, LPARAM lParam ); //copydata的结构体
typedef struct tagCOPYDATASTRUCT { DWORD dwData; //发送的数据 DWORD cbData; //发送数据的大小 PVOID lpData; //指针,指向要发送的数据 } COPYDATASTRUCT;
1 //发送进程的主程序 2 //send the data 3 void DataSend(HWND hDlg) 4 { 5 TCHAR buf[128] ={0} ; 6 COPYDATASTRUCT cds; 7 GetDlgItemText(hDlg, IDC_EDIT, buf, sizeof(buf)); //gain the string from the EDIT control 8 9 cds.dwData = 1; 10 cds.cbData = _tcslen(buf)*2; 11 cds.lpData = (PVOID)buf; 12 // hwnd为接收消息的目标窗口句柄 13 //(WPARAM)hWnd可以是NULL或者发送消息的窗口句柄 14 //COPYDATASTRUCT结构体指针 15 SendMessage(FindWindow(NULL, TEXT("copydata")), WM_COPYDATA, NULL,(LPARAM)&cds); 16 }
1 //接收进程的主程序 2 case WM_COPYDATA: 3 { 4 COPYDATASTRUCT *pcds = (COPYDATASTRUCT *)lParam; 5 memcpy(databuf, pcds->lpData, pcds->cbData); //copy the data to buffer 6 //send the receiving data to the list and display 7 SendDlgItemMessage(hWnd, IDC_RPTLIST, LB_ADDSTRING,0,(LPARAM)(LPCTSTR)databuf); 8 } 9 break;
时间: 2024-10-08 20:06:26