通过PostMessage发送/接收字符串 [英] Sending/Receiving a string through PostMessage

查看:2568
本文介绍了通过PostMessage发送/接收字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然在线上已经有几个资源来解决这个粗糙的问题,但我仍然没有找到一个适合我的答案。

Although there are already a few resources online that address this rough topic, I still haven't found an answer that works for me.

我希望在我的 VB.net 进程和我的 C ++ 进程之间有充分的沟通。我想能够从C ++进程发送一个字符串到,但暂时我需要实现:

I desire to have full communication between my VB.net process and my C++ process. I would like to be able to send a string to and from the C++ process, but for the time being I need to achieve:

发送字符串到 C ++ 过程,然后处理。

Sending a string to the C++ process, and handling it.

我不确定的点,但我会尽量保持这个尽可能简单...

This creates a few points that I am uncertain on, but I'll try to keep this as simple as possible...

使用 VB中的以下函数声明;

Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
    ByVal hWnd As IntPtr, _
    ByVal Msg As UInteger, _
    ByVal wParam As IntPtr, _
    ByVal lParam As String _
) As Boolean

并发送如下信息:

PostMessage(hWnd, SM_PING, Nothing, "schlampe")


b $ b

使用以下用于在 C ++ 中捕获邮件的方法声明;

With the following method declaration for capturing the message in C++;

LRESULT CALLBACK newWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

为了测试我是否可以访问字符串使用;

And for a test of whether I can access the string using;

char buffer[50];
sprintf(buffer, "Received: %s", (char *)lParam);
MsgBox(buffer);



我忽略了许多我认为不必要的细节,但要求并给你。


I skimmed over a lot of the details that I believe to be unnecessary, but ask and it shall be given unto you.

我的问题是收到和处理的消息。但是C ++进程创建的消息框不包含我的测试消息(它读为:Recieved:)。

My problem is that the message is received and "handled"... but the message box created by the C++ process does not contain my test message (it reads: "Recieved: ").

因此,如何发送通过PostMessage / SendMessage从VB到C ++的字符串?

So, how can I send a string via PostMessage/SendMessage from VB to C++?

查看解决方案的接受答案...但此外, C ++):

See the accepted answer for the solution... but furthermore, here is how I receive the string (C++):

LRESULT CALLBACK newWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch(uMsg) {
        case WM_COPYDATA:
            MsgBox("Received a WM_COPYDATA message");
            COPYDATASTRUCT * pcds = (COPYDATASTRUCT *)lParam;
            LPCTSTR lpszString = (LPCTSTR)(pcds->lpData);
            MsgBox(lpszString);
            return 1L;
    }
    return CallWindowProc(instance->OriginalProcessor(), hwnd, uMsg, wParam, lParam);
}


最后,我使用IPC示例此处发送消息。这个例子使用C#发送消息,但是这个概念是我需要的(更不用说在公园里散步,把这样的代码转换成VB)。注意,在我的VB实现中,我不需要用空字符终止字符串。


And finally, I used the IPC example here to send the message. This example sends the message using C#, but the concept was all I needed (not to mention that it's a walk in the park to convert such code to VB). Note that in my VB implementation, I didn't need to terminate the string with a null character.

推荐答案

,您应该使用 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.

这篇关于通过PostMessage发送/接收字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆