结构体
1 2 3 4 |
struct Info{ TCHAR g_strFileType[MAX_PATH]; bool bStop; }; |
这是发送端的进程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
HWND hWndRcv=::FindWindow(NULL,_T("RECV COPY DATA")); if(hWndRcv==NULL) { ::AfxMessageBox(_T("Not Found Window")); ((CButton*)(GetDlgItem( IDC_CHECK1 )))->SetCheck(0) ; //IDC_CHECK is ID for control return; } UpdateData(true); Info *m_info; m_info = new Info(); _tcscpy(m_info->g_strFileType,m_Filetype.GetBuffer(m_Filetype.GetLength()*2+2)); int checkState1 = IsDlgButtonChecked(IDC_CHECK1); if (BST_CHECKED==checkState1) { m_info->bStop = true; } else { m_info->bStop = false; } COPYDATASTRUCT cpd; cpd.dwData=1; cpd.cbData=sizeof(Info); cpd.lpData=(PVOID)m_info; ::SendMessage(hWndRcv,WM_COPYDATA,(WPARAM)this->m_hWnd,(LPARAM)&cpd); UpdateData(FALSE); ::AfxMessageBox(_T("send sucess!")); |
接受的进程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam) { switch(message) { case WM_DESTROY: PostQuitMessage(0); case WM_COPYDATA: OutputDebugString(_T("CopyData")); // 当收到WM_COPYDATA消息时,从lParam中提取出 // COPYDATASTRUCT PCOPYDATASTRUCT lpcds = (PCOPYDATASTRUCT)lParam; if (lpcds->dwData==1) { Info *info = (Info*)(lpcds->lpData); OutputDebugString(info->g_strFileType); _tcscpy(g_info.g_strFileType,info->g_strFileType); g_info.bStop = info->bStop; } return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } |
转载请注明:exchen's blog » 发送WM_COPYDATA消息实现进程间的通信