且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过PostMessage发送/接收字符串

更新时间:2023-02-12 15:18:13

,您应该使用 WM_COPYDATA 在进程之间传输字符串数据。如果使用自定义消息ID,则字符串数据不会在两个不同的进程地址空间之间编组。

When using Windows messages, you should use WM_COPYDATA to transfer string data between processes. If you use custom message IDs then the string data will not be marshalled between the two distinct process address spaces.

这就是为什么你当前的代码失败。接收进程在 lParam 中传递一个指向调用进程的地址空间中的内存的指针。当然在其他进程中是没有意义的。

And this is why your current code fails. The receiving process is passed in lParam a pointer to memory in the address space of the calling processes. And of course that is meaningless in the other process.

虽然还有其他方法可以在Windows消息进程之间编组数据,但是 WM_COPYDATA 是最简单的。如果你的要求变得更加复杂,那么你可能需要考虑一个比Windows消息更全面的IPC方法。

Whilst there are other ways to marshal data like this between processes with Windows messages, WM_COPYDATA is by far the simplest. If your requirement becomes much more complex then you may need to consider a more comprehensive IPC approach than Windows messages.