进程间发送数据的问题

来源:互联网 发布:电子商务php 编辑:程序博客网 时间:2024/05/17 09:40

向其他进程的窗口发送数据,如果只是发送基本类型的数据,用不着使用WM_COPYDATA消息,因为Jeffrey Richter said in programming application for Microsoft windows: " Fortunately, most messages don't require this type of processing—it takes place only when an application sends interprocess messages. Special processing such as this has to be performed for any message whose wParam or lParam parameters represent a pointer to a data structure. " 我个人是这样理解的,WM_COPYDATA是个进程间消息通信,"It's a shame it's not used more frequently"(said by Jeffrey Richter),在wparam lparam是个结构的地址时如字符串等,WM_COPYDATA是使用的绝佳场合。若进程的消息通信,wparam,lparam只是些基本的数据类型的数据,是用不着WM_COPYDATA,因为他们不是地址,是与进程自身无关的数据,可以直接Send 或Post自己定义的消息。例如: CWnd * pWnd;  pWnd=CWnd::FindWindow(NULL,_T("Shell"));  if(pWnd!=NULL)  pWnd->PostMessage(WM_USER+120,(WPARAM)(int)nData,0);

使用WM_COPYDATA的注意事项:

 1.Always send this message; never post it. You can't post a WM_COPYDATA message because the system must free the memory-mapped file after the receiving window procedure has processed the message. If you post the message, the system doesn't know when the WM_COPYDATA message is processed, and therefore it can't free the copied block of memory.

 2.It takes some time for the system to make a copy of the data in the other process's address space. This means that you shouldn't have another thread that modifies the contents of the memory block running in the sending application until the call to SendMessage returns.

3.The WM_COPYDATA message allows a 16-bit application to communicate with a 32-bit application and vice versa. It also allows a 32-bit application to talk to a 64-bit application and vice versa. This is an incredibly easy way to have newer applications talk to older applications. Also note that WM_COPYDATA is fully supported on Windows 2000 and Windows 98.

原创粉丝点击