C#:一个编组"指针,指向一个int数组"从SendMessage函数()的lParam
我试图利用的NativeWindow继承的类从我管理的COM服务器继承非托管状态栏窗口,正在运行到墙上试图使如何正确封送的lParam的内容感。
I'm trying to subclass an unmanaged statusbar window from my managed COM server using a class inherited from NativeWindow, and am running into a wall trying to make sense of how to properly marshal the contents of an lParam.
http://msdn.microsoft.com/ EN-US /库/ bb760757%28VS.85%29.aspx 说,这lParam的内容的类型为(LPARAM)(LPINT)aWidths
,而这个变量的内容实际上是一个指针到一个整数数组。
http://msdn.microsoft.com/en-us/library/bb760757%28VS.85%29.aspx says that the contents of this lParam is of type (LPARAM)(LPINT) aWidths
, and that the contents of this variable is actually a "pointer to an integer array."
我不能想出一个办法来正确地封送这个。我们的目标是读取lParam的,我们的价值添加到数组,然后通过发送新邮件 base.wndProc(参考M)
。
I can't figure out a way to marshal this correctly. The goal is to read the lParam, add our value to the array, and then send the new message via base.wndProc(ref m)
.
这会是很好,如果我能 INT []数组=(INT [])M。*的lParam
或诸如此类,但生活却并不如此简单(我没有得到使用不安全code)。我笨拙地试图强迫编组给我通过 Marshal.PtrToStructure)的东西(
但我知道这是从一开始就注定为C-数组不是一个结构我试图使这个结构显然是不blittable。
It'd be nice if I could just int[] array = (int[])m.*lParam
or somesuch, but life isn't so simple (and I don't get to use unsafe code). I've clumsily tried to force the marshaller to give me something via Marshal.PtrToStructure()
but knew this was doomed from the start as the C-array isn't a struct and the struct I tried to make is obviously not blittable.
现在我们是让原来的通话经历,然后让更多的WinAPI的调用来获取数组,格式化,然后重新发送它之前的状态栏可以粉刷。这是很好的工作,但不是不够好。
Right now we are letting the original call go through and then making additional WinAPI calls to get the array, format it, and then resend it before the statusbar can repaint. This is working well, but not well enough.
任何想法?
谢谢!
汤姆
PS-我有很多的麻烦所著的Grokking lParams是如何在C#中使用的文件是相当混乱: - /
PS- I've had a lot of trouble grokking how lParams are used in C#, the documentation is quite confusing :-/
继DTB的评论,你可以从这个借用一些code的 SO进入。
Following the "dtb"'s comment, you can borrow some code from this SO entry.
您必须提供的LPARAM是一个指向数组的第一个元素。然后,所有你需要做的是:
The LPARAM you must supply is a pointer to the first element of the array. Then all you have to do is:
int[] parts = new int[]{ 1, 2, 3, 4 };
int nParts = parts.Length;
IntPtr pointer = Marshal.AllocHGlobal(nParts * Marshal.SizeOf(typeof(int)));
for (int i = 0; i < nParts; i++) {
Marshal.WriteInt32(pointer, i * Marshal.SizeOf(typeof(int), parts[i]));
}
// Call SendMessage with WPARAM = nParts and LPARAM = Pointer
Marshal.FreeHGlobal(pointer);