wm_copydata 进城通信

1. 俩个对话框,通过findwindow 获取窗口进程就行通信

1.1 服务端发送信息:

void CsendmessageDlg::OnSendCopydat()
{
    UpdateData();                                       // 更新数据
    CWnd *pWnd_resv=CWnd::FindWindow(NULL,_T("vm_copydata_test"));    //通过findwindow 查找DataRecv进程的窗口的句柄
    //HWND pWnd_resv= this->GetXHandle(L"vm_copydata_test.exe"); // 通过进程名自 获取DataRecv进程的主窗口句柄 

    if(pWnd_resv==NULL)
    {
        AfxMessageBox(L"Unable to find DataRecv.");
        return;
    }

    COPYDATASTRUCT cpd;                     // 给COPYDATASTRUCT结构赋值

    char *ch = "1234";
    cpd.dwData = 0;  //这个我也没弄懂 啥意思

    cpd.cbData =strlen(ch)+1; //防止乱码,因为strlen只计算字符串的个数,不计算最后的\0,所以这里加1
    cpd.lpData = (void*)ch;  //内容
    //AfxMessageBox();
    /** * *pWnd_resv,目的窗口的句柄, * WM_COPYDATA, 传递的值类型是 * (WPARAM)::GetForegroundWindow(),获取当前的主窗口 * (LPARAM)&cpd 前面定义的结构体 */
    ::SendMessage(*pWnd_resv, WM_COPYDATA, (WPARAM)::GetForegroundWindow(), (LPARAM)&cpd);

}

1.2 客户端接收信息 :mfc 客户端接收信息 是通过重写wm_copydata 方法 就是下面的这个方法:具体的可以百度一下 copydata

BOOL Cvm_copydata_testDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
    char *s = (char *)pCopyDataStruct->lpData;
    this->m_rec_message=s;
    //::AfxMessageBox(this->m_rec_message);

    UpdateData(FALSE);

    return CDialogEx::OnCopyData(pWnd, pCopyDataStruct);
}

2. 通过进程名获取进程的主窗口 就行通信

2.1 服务器发送信息

typedef struct tagWNDINFO  
{  
    DWORD dwProcId;  
    HWND hWnd;  
} WNDINFO, *LPWNDINFO;  
BOOL CALLBACK MyEnumProc(HWND hWnd,LPARAM lParam)  
{  
    DWORD dwProcId;  
    GetWindowThreadProcessId(hWnd, &dwProcId); //获得hWnd对应的线程ID 
LPWNDINFO pInfo = (LPWNDINFO)lParam;  
    if(dwProcId == pInfo->dwProcId) //判断是否是我们需要寻找的ID 
    {  
        pInfo->hWnd = hWnd;  
        return FALSE;  
    }  

    return TRUE;  
}  
HWND GetProcessMainWnd(DWORD dwProcId)  
{  
    WNDINFO wi;  

    wi.dwProcId = dwProcId; //被查询窗口的进程ID 
    wi.hWnd = NULL;  

    EnumWindows(MyEnumProc,(LPARAM)&wi); //枚举窗口 

    return wi.hWnd; //返回dwProcId对应的窗口句柄 
}   
HWND GetXHandle(LPCTSTR strProcessName)//procName 被调用进程名称
{
    if(NULL == strProcessName)
    {
        return false;
    }

    HANDLE handle32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

    if(INVALID_HANDLE_VALUE == handle32Snapshot)
    {
        return FALSE;
    }

    PROCESSENTRY32 pEntry;
    pEntry.dwSize = sizeof(PROCESSENTRY32);

    if(Process32First(handle32Snapshot,&pEntry))
    {
        bool b_Found = false;

        if(!_tcsicmp(pEntry.szExeFile,strProcessName))
        {
            b_Found = true;
        }

        while((!b_Found) && Process32Next(handle32Snapshot,&pEntry))
        {
            if(!_tcsicmp(pEntry.szExeFile,strProcessName))
            {
                b_Found = true;
            }
        }

        if(b_Found)
        {
            CloseHandle(handle32Snapshot);
            HANDLE handle  = OpenProcess(PROCESS_TERMINATE,FALSE,pEntry.th32ProcessID);
            //::MessageBox(::GetForegroundWindow(),"this->GetProcessMainWnd(pEntry.th32ProcessID);", TEXT("测试"), MB_ICONWARNING|MB_OK); 
            return this->GetProcessMainWnd(pEntry.th32ProcessID);

        }

    }
    DWORD d = GetLastError();
    CloseHandle(handle32Snapshot);

    return NULL;
}
void OnSendCopydat()
{
    UpdateData();                                       // 更新数据
    //CWnd *pWnd_resv=CWnd::FindWindow(NULL,_T("vm_copydata_test")); //通过findwindow 查找DataRecv进程的窗口的句柄
    HWND pWnd_resv= this->GetXHandle(L"vm_copydata_test.exe");  // 通过进程名自 获取DataRecv进程的主窗口句柄 

    if(pWnd_resv==NULL)
    {
        AfxMessageBox(L"Unable to find DataRecv.");
        return;
    }

    COPYDATASTRUCT cpd;                     // 给COPYDATASTRUCT结构赋值

    char *ch = "1234";
    cpd.dwData = 0;  //这个我也没弄懂 啥意思

    cpd.cbData =strlen(ch)+1; //防止乱码,因为strlen只计算字符串的个数,不计算最后的\0,所以这里加1
    cpd.lpData = (void*)ch;  //内容
    //AfxMessageBox();
    /** * *pWnd_resv,目的窗口的句柄, * WM_COPYDATA, 传递的值类型是 * (WPARAM)::GetForegroundWindow(),获取当前的主窗口 * (LPARAM)&cpd 前面定义的结构体 */
    ::SendMessage(pWnd_resv, WM_COPYDATA, (WPARAM)::GetForegroundWindow(), (LPARAM)&cpd);

}

2.2 客户管接收信息,和上面的一样。

3 通过进程进程通信,而不是窗口句柄。(因为有的程序没有窗口或者是在锁屏的状态没有办法获取正在运行程序的窗口,只能获取程序的进程和进程id

你可能感兴趣的:(进城通信,wm-copydat)