Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 573701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:45:07+00:00 2026-05-13T13:45:07+00:00

I’m attempting to get a KBDLLHOOKSTRUCT from a keyboard-hook’s lParam. private static IntPtr HookCallback(int

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:45:07+00:00Added an answer on May 13, 2026 at 1:45 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 303k
  • Answers 303k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ant copy task has the ability to replace those strings… May 13, 2026 at 8:35 pm
  • Editorial Team
    Editorial Team added an answer One way to optimise the binary-tree approach is to use… May 13, 2026 at 8:35 pm
  • Editorial Team
    Editorial Team added an answer Either of the first two are fine I'd say. Not… May 13, 2026 at 8:35 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.