現在有兩個進程A,B,怎樣可以將A裏面的變量a,b傳給B進程呢??

解决方案 »

  1.   

    WM_COPYDATA
    An application sends the WM_COPYDATA message to pass data to another application. To send this message, call the SendMessage function with the following parameters (do not call the PostMessage function). SendMessage( 
      (HWND) hWnd,              // handle to destination window 
      WM_COPYDATA,              // message to send
      (WPARAM) wParam,          // handle to window (HWND)
      (LPARAM) lParam           // data (PCOPYDATASTRUCT)
    );
      

  2.   

    COPYDATASTRUCT
    The COPYDATASTRUCT structure contains data to be passed to another application by the WM_COPYDATA message. typedef struct tagCOPYDATASTRUCT { 
        ULONG_PTR dwData; 
        DWORD     cbData; 
        PVOID     lpData; 
    } COPYDATASTRUCT, *PCOPYDATASTRUCT; 
    Members
    dwData 
    Specifies data to be passed to the receiving application. 
    cbData 
    Specifies the size, in bytes, of the data pointed to by the lpData member. 
    lpData 
    Pointer to data to be passed to the receiving application. This member can be NULL. 
      

  3.   

    http://www.codeproject.com/threads/ipc_wmcopy.aspThe exchange of data is performed by finding the other application (using FindWindow) and sending a WM_COPYDATA message to that window:    CString strWindowTitle = _T("Window Name");
        CString strDataToSend  = _T("This is a message to send"); LRESULT copyDataResult;
    CWnd *pOtherWnd = CWnd::FindWindow(NULL, strWindowTitle); if (pOtherWnd)
    {
    COPYDATASTRUCT cpd;
    cpd.dwData = 0;
    cpd.cbData = strDataToSend.GetLength();
    cpd.lpData = (void*)strDataToSend.GetBuffer(cpd.cbData);
    copyDataResult = pOtherWnd->SendMessage(WM_COPYDATA,
                                                    (WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(),
                                                    (LPARAM)&cpd);
    strDataToSend.ReleaseBuffer();
    // copyDataResult has value returned by other app }
    else
    {
    AfxMessageBox("Unable to find other app.");
    }
    }The other app should handle the WM_COPYDATA message in the following mannerBEGIN_MESSAGE_MAP(CMyWnd, CWnd)
    //{{AFX_MSG_MAP(CMyWnd)
    ...
    ON_WM_COPYDATA()
    ...
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()...BOOL CMyWnd::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
    {
    CString strRecievedText = (LPCSTR) (pCopyDataStruct->lpData); return CMyWnd::OnCopyData(pWnd, pCopyDataStruct);
    }
      

  4.   

    数据量少的话,直接WM_DATACOPY
    数据量大,就考虑filemapping
      

  5.   

    CString strWindowTitle = _T("Window Name");利用 WM_DATACOPY,
    通过找到另一个进程的窗口句柄,来实现消息传递
      

  6.   

    共享变量
    虚拟内存文件
    socket通信
    还有一些老的方法,一般不用了
      

  7.   

    不管是 自定义消息还是 WM—COPYDATA, 偶都觉得不妥,这两种情况如果遇到系统忙的时候就会失去应有的效果,不信你在你的程序A的主线程里面搞个批量I/O操作,然后程序B给A发一下消息试试~
    最保险的,用RPC吧,都什么时代了,还搞消息传递呢,老土不说,还显得咱没有水平
      

  8.   

    TO  rfa(实况狐狸)
    有什麽好的建議呢??請教了
      

  9.   

    用 R P C 呀,我都说过了,RPC = Remote Process Call,远过程调用的意思,这里的remote是广义范围上的,包括两种情况:跨进程,和跨计算机(其实也是跨越了进程)。
    MS搞它出来就是为了解决这些问题的,放心用吧。不过只能是在NT内核(包括2000和以后版本)下的哟~,至于Win32内核(9x & me)的呢....啊....系统都这么老,那方法也只能使用老的咯~ :)呵呵。
      

  10.   

    你可以发送消息啊sendmessage,用什么消息可以自己定义,如#define WM_NEW WM_USER+100
      

  11.   

    如果进程间需要用到我们自己创建的消息时需要用WM_COPYDATA消息。
    typedef struct tagCOPYDATASTRUCT 
    {
    DWORD dwData; 
    DWORD cbData; 
    PVOID lpData; 
    } COPYDATASTRUCT;
    COPYDATASTRUCT cds;
    SendMessage(hwndReceiver, WM_COPYDATA, (WPARAM)hwndSender, (LPARAM)cds);
    当进程间准备发送数据时,要先初始化COPYDATASTRUCT结构。dwData留给我们使用,cbData指定要发的字节数,lpData指向数据的第一个字节(在发送者的地址空间中)。
    当SendMessage发送WM_COPYDATA时,它分配一块大小为cbData字节的内存,并把数据从发送者的地址空间中拷贝到给内存块中。然后它将消息发送给目标窗口。但接受的窗口过程处理该消息时,lParam参数指向一个存在于接受线程地址空间的COPYDATASTRUCT结构。该结构的lpData指向被拷贝的内存块,地址也改为指向接受进程地址空间的一块内存。
    使用WM_COPYDATA时需要注意以下几件事:
    1.总是要发送这个消息,不应投递。因为窗口要在接受的窗口过程处理完消息后要释放拷贝的内存。如果投递消息,系统就不知道WM_COPYDATA是否处理完,也就无法释放拷贝的内存块。
    2.系统要花一时间来将数据拷贝到其他进程的地址空间。也就是在SendMessage的调用返回之前,不能让另一线程修改内存块的内容。
    3.WM_COPYDATA可以用于WIN32和WIN16的应用程序之间发送数据。