Marshal.PtrToStructure throwing System.ArgumentException error
Asked Answered
G

1

13

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?

Guardi answered 17/1, 2010 at 3:33 Comment(3)
Can you post your KBDLLHOOKSTRUCT structure definition?Calamity
Yes, KBDLLHOOKSTRUCT is very likely declared incorrectly, perhaps missing either the LayoutKind.Explicit or LayoutKind.Sequential attribute.Indelicacy
In my case your KBDLLHOOKSTRUCT was declared as a struct. After having made it into a class that exception went away.Freiburg
H
37

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

Humdinger answered 17/1, 2010 at 9:8 Comment(1)
By the way, regarding the ref-parameter solution, I have a feeling I was the one who pointed you at Marshal.PtrToStructure in the first place. In retrospect the ref-parameter solution is probably the simplest and I should have suggested it in the first place. Feel free to administer avenging downvotes of just retribution if you feel I gave you a bum steer!Humdinger

© 2022 - 2024 — McMap. All rights reserved.