Marshal.PtrToStructure throwing System.ArgumentException error

折月煮酒 提交于 2019-11-29 04:03:16

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.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!