PostMessage 的字节数组封送处理
我正在尝试将一些 C++ 代码移植到 C#,我需要做的事情之一是使用 PostMessage
将字节数组传递到另一个进程的窗口。我正在尝试将源代码获取到另一个程序,以便我可以准确地看到它所期望的内容,但与此同时,原始 C++ 代码如下所示:
unsigned long result[5] = {0};
//Put some data in the array
unsigned int res = result[0];
Text winName = "window name";
HWND hWnd = FindWindow(winName.getConstPtr(), NULL);
BOOL result = PostMessage(hWnd, WM_COMMAND, 10, res);
这是我现在拥有的:
[DllImport("User32.dll", SetLastError = true, EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll", SetLastError = true, EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public int dwData;
public int cbData;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
public byte[] lpData;
}
public const int WM_COPYDATA = 0x4A;
public static int sendWindowsByteMessage(IntPtr hWnd, int wParam, byte[] data)
{
int result = 0;
if (hWnd != IntPtr.Zero)
{
int len = data.Length;
COPYDATASTRUCT cds;
cds.dwData = wParam;
cds.lpData = data;
cds.cbData = len;
result = SendMessage(hWnd, WM_COPYDATA, wParam, ref cds);
}
return result;
}
//*****//
IntPtr hWnd = MessageHelper.FindWindow(null, windowName);
if (hWnd != IntPtr.Zero)
{
int result = MessageHelper.sendWindowsByteMessage(hWnd, wParam, lParam);
if (result == 0)
{
int errCode = Marshal.GetLastWin32Error();
}
}
请注意,我必须从在 C++ 中使用 PostMessage
在 C# 中使用 SendMessage
。
所以现在发生的情况是,我得到的结果和 errCode 都为 0,我相信这意味着消息未得到处理 - 事实上,查看其他应用程序,我没有看到预期的响应。我已经验证了 hWnd != IntPtr.Zero
,所以我认为消息被发布到正确的窗口,但消息数据是错误的。有什么想法我做错了吗?
更新
在尝试了评论中的建议后,我仍然没有任何运气。这是我目前得到的:
[DllImport("User32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}
public struct BYTEARRDATA
{
public byte[] data;
}
public static IntPtr IntPtrAlloc<T>(T param)
{
IntPtr retval = Marshal.AllocHGlobal(Marshal.SizeOf(param));
Marshal.StructureToPtr(param, retval, false);
return (retval);
}
public static void IntPtrFree(IntPtr preAllocated)
{
//Ignores errors if preAllocated is IntPtr.Zero!
if (IntPtr.Zero != preAllocated)
{
Marshal.FreeHGlobal(preAllocated);
preAllocated = IntPtr.Zero;
}
}
BYTEARRDATA d;
d.data = data;
IntPtr buffer = IntPtrAlloc(d);
COPYDATASTRUCT cds;
cds.dwData = new IntPtr(wParam);
cds.lpData = buffer;
cds.cbData = Marshal.SizeOf(d);
IntPtr copyDataBuff = IntPtrAlloc(cds);
IntPtr r = SendMessage(hWnd, WM_COPYDATA, IntPtr.Zero, copyDataBuff);
if (r != IntPtr.Zero)
{
result = r.ToInt32();
}
IntPtrFree(copyDataBuff);
copyDataBuff = IntPtr.Zero;
IntPtrFree(buffer);
buffer = IntPtr.Zero;
这是一个 64 位进程试图联系 32 位进程,所以那里可能有一些东西,但我不确定是什么。
I'm trying to port some C++ code to C#, and one of the things that I need to do is use PostMessage
to pass a byte array to another process' window. I'm trying to get the source code to the other program so I can see exactly what it's expecting, but in the meantime, here's what the original C++ code looks like:
unsigned long result[5] = {0};
//Put some data in the array
unsigned int res = result[0];
Text winName = "window name";
HWND hWnd = FindWindow(winName.getConstPtr(), NULL);
BOOL result = PostMessage(hWnd, WM_COMMAND, 10, res);
And here's what I have now:
[DllImport("User32.dll", SetLastError = true, EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll", SetLastError = true, EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public int dwData;
public int cbData;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
public byte[] lpData;
}
public const int WM_COPYDATA = 0x4A;
public static int sendWindowsByteMessage(IntPtr hWnd, int wParam, byte[] data)
{
int result = 0;
if (hWnd != IntPtr.Zero)
{
int len = data.Length;
COPYDATASTRUCT cds;
cds.dwData = wParam;
cds.lpData = data;
cds.cbData = len;
result = SendMessage(hWnd, WM_COPYDATA, wParam, ref cds);
}
return result;
}
//*****//
IntPtr hWnd = MessageHelper.FindWindow(null, windowName);
if (hWnd != IntPtr.Zero)
{
int result = MessageHelper.sendWindowsByteMessage(hWnd, wParam, lParam);
if (result == 0)
{
int errCode = Marshal.GetLastWin32Error();
}
}
Note that I had to switch from using PostMessage
in C++ to SendMessage
in C#.
So what happens now is that I'm getting both result and errCode to be 0, which I believe means that the message was not processed - and indeed looking at the other application, I'm not seeing the expected response. I have verified that hWnd != IntPtr.Zero
, so I think that the message is being posted to the correct window, but the message data is wrong. Any ideas what I'm doing wrong?
Update
I'm still not having any luck after trying the suggestions in the comments. Here's what I've currently got:
[DllImport("User32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}
public struct BYTEARRDATA
{
public byte[] data;
}
public static IntPtr IntPtrAlloc<T>(T param)
{
IntPtr retval = Marshal.AllocHGlobal(Marshal.SizeOf(param));
Marshal.StructureToPtr(param, retval, false);
return (retval);
}
public static void IntPtrFree(IntPtr preAllocated)
{
//Ignores errors if preAllocated is IntPtr.Zero!
if (IntPtr.Zero != preAllocated)
{
Marshal.FreeHGlobal(preAllocated);
preAllocated = IntPtr.Zero;
}
}
BYTEARRDATA d;
d.data = data;
IntPtr buffer = IntPtrAlloc(d);
COPYDATASTRUCT cds;
cds.dwData = new IntPtr(wParam);
cds.lpData = buffer;
cds.cbData = Marshal.SizeOf(d);
IntPtr copyDataBuff = IntPtrAlloc(cds);
IntPtr r = SendMessage(hWnd, WM_COPYDATA, IntPtr.Zero, copyDataBuff);
if (r != IntPtr.Zero)
{
result = r.ToInt32();
}
IntPtrFree(copyDataBuff);
copyDataBuff = IntPtr.Zero;
IntPtrFree(buffer);
buffer = IntPtr.Zero;
This is a 64 bit process trying to contact a 32 bit process, so there may be something there, but I'm not sure what.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是 COPYDATASTRUCT 应该包含一个指针作为最后一个成员,并且您将传递整个数组。
看一下 pinvoke.net 上的示例:http://www.pinvoke。 net/default.aspx/Structures/COPYDATASTRUCT.html
注释后的更多信息:
给出这些定义:
我可以创建两个 .NET 程序来测试
WM_COPYDATA
。下面是接收方的窗口过程:以及使用
SendMessage
调用它的代码:当发送方和接收方都是 32 位进程以及 64 位进程时,这将按预期工作。如果两个进程的“位数”不匹配,它将不起作用。
有多种原因导致此功能不适用于 32/64 或 64/32。想象一下您的 64 位程序想要将此消息发送到 32 位程序。 64 位程序传递的 lParam 值的长度为 8 个字节。但32位程序只能看到其中的4个字节。所以该程序将不知道从哪里获取数据!
即使这有效,
COPYDATASTRUCT
结构的大小也是不同的。在 32 位程序中,它包含两个指针和一个 DWORD,总大小为 12 个字节。在 64 位程序中,COPYDATASTRUCT
的长度为 20 字节:两个指针各占 8 字节,以及一个 4 字节长度值。换个方向你也会遇到类似的问题。
我严重怀疑您是否能让
WM_COPYDATA
适用于 32/64 或 64/32。The problem is that COPYDATASTRUCT is supposed to contain a pointer as the last member, and you're passing the entire array.
Take a look at the example on pinvoke.net: http://www.pinvoke.net/default.aspx/Structures/COPYDATASTRUCT.html
More info after comments:
Given these definitions:
I can create two .NET programs to test
WM_COPYDATA
. Here's the window procedure for the receiver:And the code that calls it using
SendMessage
:This works as expected when both the sender and receiver are 32 bit processes and when they're 64 bit processes. It will not work if the two processes' "bitness" does not match.
There are several reasons why this won't work for 32/64 or 64/32. Imagine that your 64 bit program wants to send this message to a 32 bit program. The
lParam
value passed by the 64 bit program is going to be 8 bytes long. But the 32 bit program only sees 4 bytes of it. So that program won't know where to get the data from!Even if that worked, the size of the
COPYDATASTRUCT
structure is different. In 32 bit programs, it contains two pointers and a DWORD, for a total size of 12 bytes. In 64 bit programs,COPYDATASTRUCT
is 20 bytes long: two pointers at 8 bytes each, and a 4-byte length value.You have similar problems going the other way.
I seriously doubt that you'll get
WM_COPYDATA
to work for 32/64 or for 64/32.这适用于 32 位发送器到 64 位接收器、64 位发送器到 32 位接收器。也可以从 32 到 32、64 到 64 工作。您甚至不需要声明 COPYDATASTRUCT。很简单:
This will work on 32bit sender to 64bit receiver, 64bit sender to 32bit receiver. Also work from 32 to 32, and 64 to 64. You don't even need to declare COPYDATASTRUCT. Very simple:
这可能是 32 位与 64 位的问题吗?
尝试将 COPYDATASTRUCT 的 dwData 成员设置为 IntPtr 而不是 int。
有关相关问题,请参阅此线程:
http://www.vistax64.com/net-general/156538-apparent-marshalling-lated-problem-x64-but-works-x86.html
查看 COPYDATASTRUCT 的原始定义:
http://msdn.microsoft.com/en-us/library/ms649010(VS.85).aspx
以下是 ULONG_PTR 在 x64 上的含义:
http://msdn.microsoft.com/en-us/library/aa384255(VS.85).aspx
要存储 64 位指针值,请使用 ULONG_PTR。使用 32 位编译器编译时,ULONG_PTR 值为 32 位;使用 64 位编译器编译时,ULONG_PTR 值为 64 位。
Could it be a 32 versus 64 bit problem?
Try setting COPYDATASTRUCT's dwData member to an IntPtr instead of an int.
See this thread for a related problem:
http://www.vistax64.com/net-general/156538-apparent-marshalling-related-problem-x64-but-works-x86.html
See the original definition of COPYDATASTRUCT:
http://msdn.microsoft.com/en-us/library/ms649010(VS.85).aspx
Here's the meaning of ULONG_PTR on x64:
http://msdn.microsoft.com/en-us/library/aa384255(VS.85).aspx
To store a 64-bit pointer value, use ULONG_PTR. A ULONG_PTR value is 32 bits when compiled with a 32-bit compiler and 64 bits when compiled with a 64-bit compiler.
在您的
IntPtrAlloc
函数中,SizeOf(param)
为您提供了什么?我认为这将是对数组的引用的大小,而不是数组内容的大小。因此Windows会将.NET数组引用复制到其他进程中,这是完全没有意义的。固定数组,并使用
Marshal.UnsafeAddrOfPinnedArrayElement
获取lpData
的正确值。In your
IntPtrAlloc
function, what's theSizeOf(param)
giving you? I think it's going to be the size of a reference to an array, not the size of the array content. And so Windows will copy a .NET array reference into the other process, which is completely meaningless.Pin the array, and use
Marshal.UnsafeAddrOfPinnedArrayElement
to get the proper value oflpData
.