希望通过wm_copydata实现两个进程之间的通信,但是在进程1中的数据是分配在堆中,当把这些指针传递到进程2中的时候,指针无效了。那如何实现进程1的数据传递给进程2呢???

解决方案 »

  1.   

    SendMessage( 
      (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进去  而不是赋值一个指针接收者负责转换里面的数据
      

  2.   

    PVOID lpData;  
    这个不是一个指向数据块的指针吗???你分配数据块的时候是在 进程1中分配的啊。那就必须用进程1中的堆里的空间怎么办???
      

  3.   

    找到了  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自己分配了内存
      

  4.   

    你的msg是什么数据结构???我是不是只能用byte,不能自己构造数据结构??? 你在wm_copydata那端是怎么处理的了????我无论是用栈空间分配还是用堆空间分配的数据传过去,都是无效的地址不知道怎么办~~~
      

  5.   

    typedef _msg {
         int x;
         int y;
    }msg , *pMsg;
    注意cds.cbData = sizeof( msg );  很重要 
      

  6.   

    WM_COPYDATA消息是可以跨进程COPY数据的,windows会负责在目标进程中分配内存,并把数据COPY过去的。所以在目标进程里,COPYDATASTRUCT.lpData指针指向的位置就是copy过来的数据。
    比如
    进程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);就可以了。
      

  7.   

    晕,COPY楼上的代码忘记改了,应该是:进程1
    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);就可以了。
      

  8.   

    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到自己申请的内存区域 而不要直接去操作那段内存地址
      

  9.   

    wm_copydata 适用于2个独立的exe程序之间通信。
      

  10.   

    搞定了。原来不能传递string 类型或者流类型,必须转换为char型而且必须是栈空间地址。。不能传递指针。。悲剧发觉windows不怎么支持标准C++啊