Marshal.PtrToStructure 抛出 System.ArgumentException 错误

发布于 2024-08-18 03:11:01 字数 895 浏览 8 评论 0原文

我正在尝试从键盘挂钩的 lParam 获取 KBDLLHOOKSTRUCT。

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {

        KBDLLHOOKSTRUCT kbd = new KBDLLHOOKSTRUCT();
        Marshal.PtrToStructure(lParam, kbd); // Throws System.ArguementException
        ...

PtrToStructure 都会抛出两个

A first chance exception of type 'System.ArgumentException' occurred in myprogram.exe

不幸的是,每次按下按键时, 错误。它还会阻止该方法的运行。

MSNDA 说: http://msdn.microsoft.com/en-us/library/4ca6d5z7.aspx

ArgumentException when:

The structureType parameter layout is not sequential or explicit.

-or-

The structureType parameter is a generic type.

我可以在这里做什么来让它工作? lParam 直接来自键盘挂钩,所以我希望它是正确的。这些错误中的任何一个在这里有意义吗?我能做些什么来修复它?

I'm attempting to get a KBDLLHOOKSTRUCT from a keyboard-hook's lParam.

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {

        KBDLLHOOKSTRUCT kbd = new KBDLLHOOKSTRUCT();
        Marshal.PtrToStructure(lParam, kbd); // Throws System.ArguementException
        ...

Unfortunately the PtrToStructure is throwing two

A first chance exception of type 'System.ArgumentException' occurred in myprogram.exe

errors every time a key is pressed. It also stops that method in its tracks.

MSNDA says:
http://msdn.microsoft.com/en-us/library/4ca6d5z7.aspx

ArgumentException when:

The structureType parameter layout is not sequential or explicit.

-or-

The structureType parameter is a generic type.

What can I do here to get it working? lParam is coming straight from the keyboard hook so I would expect it to be correct. Do either of those errors make sense here, and what can I do to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身仙ぐ女味 2024-08-25 03:11:01

这是一些适合我的代码:

public struct KBDLLHOOKSTRUCT
{
  public Int32 vkCode;
  public Int32 scanCode;
  public Int32 flags;
  public Int32 time;
  public IntPtr dwExtraInfo;
}

private static IntPtr HookCallback(
    int nCode, IntPtr wParam, IntPtr lParam)
{
  if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  {
    KBDLLHOOKSTRUCT kbd = (KBDLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
    Debug.WriteLine(kbd.vkCode);  // ***** your code here *****
  }
  return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

与您的代码的关键区别是我调用 Marshal.PtrToStructure(IntPtr, Type) 重载而不是 (IntPtr, object) 重载。我认为这就是你出了问题的地方。因为如果您使用结构调用 (IntPtr, object) 重载,则会收到以下错误:

System.ArgumentException:该结构不能是值类。

此错误的明显修复方法是将 KBDLLHOOKSTRUCT 更改为类(引用类型)而不是结构(值类型):

public class KBDLLHOOKSTRUCT    // not necessarily the right solution!

但是,这会导致 MSDN 表示的错误“结构类型参数布局不是顺序的或显式的”。 :

System.ArgumentException:指定的结构必须是 blittable 或具有布局信息。

我猜这就是您现在所处的位置,将 KBDLLHOOKSTRUCT 声明为类,并收到“无布局信息”错误。有两种方法可以解决这个问题。

首先,根据 Eric Law 的评论,您可以保持 Marshal.PtrToStructure 调用不变,将 KBDLLHOOKSTRUCT 保留为类,并将布局信息添加到 KBDLLHOOKSTRUCT:

[StructLayout(LayoutKind.Sequential)]
public class KBDLLHOOKSTRUCT { ... }

其次,根据我上面的示例代码,您可以将 KBDLLHOOKSTRUCT 更改为 struct 而不是 class,并将 Marshal.PtrToStructure 调用更改为 (IntPtr, Type) 重载:(

public struct KBDLLHOOKSTRUCT { ... }

KBDLLHOOKSTRUCT kbd = (KBDLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

在这种情况下,您仍然可以添加 [StructLayout(LayoutKind如果您愿意的话,可以将 .Sequential)] 属性添加到 KBDLLHOOKSTRUCT 结构中。它在技术上是多余的,但可以帮助代码的读者将 KBDLLHOOKSTRUCT 识别为布局敏感的互操作类型。)

这两种解决方案都适合我(诚然)简单)测试。在这两者中,我会推荐第二个,因为 Win32 / C 结构在 P/Invoke 场景中通常被声明为 struct ,并且如果没有其他情况,以 STRUCT 结尾的名称可能应该是一个结构,而不是比一个班!

最后,让我提一下另一种方法。

可以将 LowLevelKeyboardProc 声明为接收 ref KBDLLHOOKSTRUCT(其中 KBDLLHOOKSTRUCT 是一个 struct,而不是 class),而不是将 LowLevelKeyboardProc 声明为接收 IntPtr 作为其 lParam )。这还需要对 CallNextHookEx 进行更改,但最终结果是通过完全避免 Marshal 调用来简化 KBDLLHOOKSTRUCT 信息的使用。使用 ref 参数还意味着您可以写入结构(我从其他问题中知道这是您的目标),并且不需要在写入后将其封送回来:(

private delegate IntPtr LowLevelKeyboardProc(
    int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT kbd);

private static IntPtr HookCallback(
    int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT kbd)
{
  Debug.WriteLine(kbd.vkCode);  // look!  no marshalling!
  return CallNextHookEx(_hookID, nCode, wParam, ref kbd);
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
    IntPtr wParam, ref KBDLLHOOKSTRUCT kbd);

不过,我可能应该警告您,当我尝试修改 kbd.vkCode 时,它​​实际上并没有影响文本框中出现的内容等。我对低级键盘挂钩了解不够,不知道为什么不知道或者我需要做什么才能完成这项工作; 对不起。)

Here is some code that works for me:

public struct KBDLLHOOKSTRUCT
{
  public Int32 vkCode;
  public Int32 scanCode;
  public Int32 flags;
  public Int32 time;
  public IntPtr dwExtraInfo;
}

private static IntPtr HookCallback(
    int nCode, IntPtr wParam, IntPtr lParam)
{
  if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  {
    KBDLLHOOKSTRUCT kbd = (KBDLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
    Debug.WriteLine(kbd.vkCode);  // ***** your code here *****
  }
  return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

The crucial difference from your code is that I am calling the Marshal.PtrToStructure(IntPtr, Type) overload rather than the (IntPtr, object) overload. And I think this is where things have gone awry for you. Because if you call the (IntPtr, object) overload with a struct, you get the following error:

System.ArgumentException: The structure must not be a value class.

The obvious fix for this error is to change KBDLLHOOKSTRUCT to be a class (reference type) instead of a struct (value type):

public class KBDLLHOOKSTRUCT    // not necessarily the right solution!

However, this results in the error that MSDN means by "The structureType parameter layout is not sequential or explicit.":

System.ArgumentException: The specified structure must be blittable or have layout information.

I am guessing this is where you are right now, with KBDLLHOOKSTRUCT declared as a class, and getting the "no layout information" error. There are two ways to address this.

First, as per Eric Law's comment, you can keep your Marshal.PtrToStructure call as it is, keep KBDLLHOOKSTRUCT as a class, and add layout information to KBDLLHOOKSTRUCT:

[StructLayout(LayoutKind.Sequential)]
public class KBDLLHOOKSTRUCT { ... }

Second, as per my sample code above, you can change KBDLLHOOKSTRUCT to a struct instead of a class, and change your Marshal.PtrToStructure call to the (IntPtr, Type) overload:

public struct KBDLLHOOKSTRUCT { ... }

KBDLLHOOKSTRUCT kbd = (KBDLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

(In this case you can still add the [StructLayout(LayoutKind.Sequential)] attribute to the KBDLLHOOKSTRUCT struct if you like. It is technically redundant but may help readers of your code recognise KBDLLHOOKSTRUCT as a layout-sensitive interop type.)

Both of these solutions work for me in an (admittedly simple) test. Of the two, I would recommend the second, because Win32 / C structures are conventionally declared as struct in P/Invoke scenarios -- and if nothing else a name that ends in STRUCT should probably be a struct rather than a class!

Finally, let me mention an alternative approach.

Instead of declaring LowLevelKeyboardProc as receiving an IntPtr as its lParam, it is possible to declare it as receiving a ref KBDLLHOOKSTRUCT (where KBDLLHOOKSTRUCT is a struct, not a class). This also requires changes to CallNextHookEx, but the net result is to simplify the use of the KBDLLHOOKSTRUCT info by avoiding the Marshal call altogether. The use of a ref parameter also means you can write to the struct (which I know from other questions is your goal) and not need to marshal it back after writing:

private delegate IntPtr LowLevelKeyboardProc(
    int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT kbd);

private static IntPtr HookCallback(
    int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT kbd)
{
  Debug.WriteLine(kbd.vkCode);  // look!  no marshalling!
  return CallNextHookEx(_hookID, nCode, wParam, ref kbd);
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
    IntPtr wParam, ref KBDLLHOOKSTRUCT kbd);

(I should probably warn you, though, that when I tried modifying kbd.vkCode, it didn't actually affect what appeared in text boxes etc. I don't know enough about low-level keyboard hooks to know why not or what I would need to do to make this work; sorry.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文