PostMessage 参数从 32 位 C# 到 64 位 C++ [英] PostMessage params from 32-bit C# to 64-bit C++

查看:33
本文介绍了PostMessage 参数从 32 位 C# 到 64 位 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 wParam 从 32 位 C# 应用程序转变为 64 位 C++ 进程的过程中传递的指针内容时,我遇到了问题.

I am having problem with the contents of a pointer passed as the wParam from a 32-bit C# application is changing along the way to a 64-bit C++ process.

有两个进程32.exe(C#)和64.exe(C++).64.exe 作为 32.exe 的子进程启动.32.exe 为 64.exe 发布窗口消息,其中一个具有 wParam,它是指向 RECT 结构数组的指针.64.exe 和 32.exe 都有一个共同的 DLL(用 C++ 编写,但当然是针对不同平台编译的),称为 32.dll 和 64.dll.

There are two processes 32.exe (in C#) and 64.exe (in C++). 64.exe is started as a child process of 32.exe. 32.exe post window message for 64.exe, one of which has a wParam that is a pointer to an array of RECT structures. Both 64.exe and 32.exe have a common DLL (written in C++, but compiled for different platforms, of course), called 32.dll and 64.dll.

一个在 32.dll 中需要 RECT* 的函数是直接从 32.exe 调用的,并使用稍后发布的相同的 RECT*,这很有效.之后它会向 64.exe 发送一条消息,它调用相同的函数并将 wParam 转换为 RECT*:

A function expecting a RECT* in 32.dll is called directly from 32.exe with the same RECT* that is later posted, and this works well. Afterwards it posts a message to 64.exe, which calls the same function and casts the wParam to RECT*:

else if (WM_SetDisabledAreas == message)
{
    SetDisabledAreas((RECT*)wParam, (UINT)lParam);
}

留言内容如下:

    if (Is64Bit() && SubProcess64 != null)
    {
        Win32.PostMessage(SubProcess64.MainWindowHandle, WindowMessages.SetDisabledAreas,
            (uint)pointer.ToInt32(), length);
    }
    MessageBox.Show(pointer.ToString());
    DLL32.SetDisabledAreas(pointer, length);

通过调试,我验证了消息已收到,但是 wParam 地址与以前不同.这并不意外,但它现在指向的内存内容是未定义的(并且在尝试查看那里的内容时遇到了访问冲突).

By debugging I've validated that the message is received, however the wParam address is not the same as it was before. This is not unexpected, but the contents of the memory it now points to is undefined (and I get an access violation when attempting to see what is there).

这里发生了什么?

推荐答案

两个进程各有自己的地址空间,因此来自进程 32.exe 的指针在 64.exe 中无效.

Each of the two processes has an own address space, so that the pointer from process 32.exe is not valid in 64.exe.

然而,这与 32 位和 64 位完全没有关系.您只需使用您选择的进程间通信技术在两个进程之间传输数据.

However, this has nothing to do with 32bit vs 64bit at all. You just have to use an interprocess communication technique of your choice to transfer the data between the two processes.

例如,您可以使用 CreateFileMapping 创建共享内存的命名部分.

For example, you could use CreateFileMapping to create a named section of shared memory.

这篇关于PostMessage 参数从 32 位 C# 到 64 位 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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