简体   繁体   English

无法使用Marshal.PtrToStringAuto(IntPtr)将C ++ char []编组为C#字符串

[英]Cannot Marshal C++ char[] to C# string using Marshal.PtrToStringAuto(IntPtr)

I have been struggling with this problem for a few a while now and I one of my best friends (google) has yielded no help for me. 我已经为这个问题苦苦挣扎了好一阵子了,我最好的朋友之一(谷歌)对我没有任何帮助。 Currently, I am working on a C# application (WinForms) that needs to take and use data from the LParam of a message sent by a C++ application. 当前,我正在开发一个C#应用程序(WinForms),该应用程序需要从C ++应用程序发送的消息的LParam中获取和使用数据。 Currently, my C++ test application checks if a handle is available for the Message Receiver (C# application) and if the handle is valid, sends a message. 当前,我的C ++测试应用程序检查是否有消息接收器(C#应用程序)可用的句柄,以及该句柄是否有效,然后发送一条消息。 The code for the C++ application is below: C ++应用程序的代码如下:

int _tmain(int argc, _TCHAR* argv[])
{
    HWND hScreenSaver = ::FindWindow(NULL,TEXT("MessageReceiver")); //MessageReceiveris the window name of the C# application
    if (hScreenSaver == NULL)
    {
        std::cout << "Handle Invalid!" << std::endl;
        return exit();
    }

    char testMessage[13] = "Test Message";
    ::SendMessageA(hScreenSaver, (UINT)101296, 0, (LPARAM)&testMessage);
    return exit();
}

int exit()
{
    system("Pause");
    return 0;
}

I believe that the C++ application is working properly (builds and runs with no errors). 我相信C ++应用程序可以正常运行(生成并运行时没有错误)。 I thought I would post it just to show how the message is being sent. 我以为我会发布它只是为了显示消息的发送方式。 A quick note is that the message number has no importance and I randomly chose it - Just in case if anyone was wondering. 快速说明一下,消息编号并不重要,我随机选择了它-以防万一有人怀疑。

My C# test application, which receives the message, is posted below: 我收到消息的C#测试应用程序发布在下面:

protected override void WndProc(ref Message m)
{
    if(m.Msg == 101296)
    {
        if(m.LParam == IntPtr.Zero)
        {
            textBox1.Text = "Nothing Sent in LParam";
        }
        else
        {
            textBox1.Text = Marshal.PtrToStringAuto(m.LParam); //This is causing the error detailed below
        }              
    }
    base.WndProc(ref m);
}

When running these two applications at the same time, the C++ application successfully sends the C# application a message and the C# application processes the message until the stated point. 当同时运行这两个应用程序时,C ++应用程序会成功向C#应用程序发送一条消息,并且C#应用程序将处理该消息,直到达到指定点为止。 The error message is: 错误消息是:

A first chance exception of type 'System.AccessViolationException' occurred in mscorlib.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I have tried to also use Marshal.Copy with no luck. 我也尝试过使用Marshal.Copy,但运气不好。 I have seen similar posts on StackOverflow about marshaling but I have not been able to resolve my issue with any solutions. 我在StackOverflow上看到过类似封送处理的类似文章,但是我无法通过任何解决方案解决问题。 Most other solutions detail how (which i believe that I am reproducing) but no other solutions detail this error that is being cause in this way. 大多数其他解决方案都详细说明了方式(我相信我正在复制),但是没有其他解决方案详细说明了以这种方式引起的此错误。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Two different applications have different address spaces, so the address in one application may be meaningless in another application. 两个不同的应用程序具有不同的地址空间,因此一个应用程序中的地址在另一应用程序中可能毫无意义。 There are different ways to exchange info between processes. 在进程之间交换信息有不同的方法。 One of them is sending WM_COPYDATA. 其中之一正在发送WM_COPYDATA。 See for example here 例如看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM