Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

c# - Marshal.PtrToStructure throwing System.ArgumentException error

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...