C#:编组“指向 int 数组的指针";来自 SendMessage() lParam [英] C#: Marshalling a "pointer to an int array" from a SendMessage() lParam

查看:28
本文介绍了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/library/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(ref 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[] array = (int[])m.*lParam 或类似的东西就好了,但生活并不那么简单(我不明白使用不安全的代码).我笨拙地试图强迫编组器通过 Marshal.PtrToStructure() 给我一些东西,但我知道这从一开始就注定了,因为 C 数组不是一个结构,而我尝试过的结构make 显然不是 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- 我在理解如何在 C# 中使用 lParams 时遇到了很多麻烦,文档非常混乱 :-/

PS- I've had a lot of trouble grokking how lParams are used in C#, the documentation is quite confusing :-/

推荐答案

按照dtb"的评论,你可以从这个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);

这篇关于C#:编组“指向 int 数组的指针";来自 SendMessage() lParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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