Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When hooking Window's message loop, what's KBDLLHOOKSTRUCT's dwExtraInfo for

Tags:

c++

c#

winapi

hook

I tried hooking a wnd proc of a window, when suddenly a wild variable appeared...

Ok, let me describe my problem a little bit more. I P/Invoked some functions to access C++ WinApi from C#. I wanted to copy some keystrokes from one window to another (aka dual boxing in WoW and other MMOs) The P/Invoking works just fine and I'm happy, but I'm confused what the variable dwExtraInfo is for. The documentation on this field just repeats what the name itself says already...

Here is the definition: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644967(v=vs.85).aspx

Or for the lazy ones (I'm one of them too :P):

[StructLayout(LayoutKind.Sequential)]
public class KBDLLHOOKSTRUCT
{
    public uint vkCode;
    public uint scanCode;
    public KBDLLHOOKSTRUCTFlags flags;
    public uint time;
    public UIntPtr dwExtraInfo;
}

When I want to send this data to a game (SendMessage with WPARAM set to WM_KEYDOWN and WM_KEYUP), I set vkCode to WPARAM and build my LPARAM from this structure. Is dwExtraInfo just the amount of keystrokes in one message?

For example, I hold down the w-key for about 10 seconds, does it store the amount of keystrokes in one intervall before the next message will be sent?

This variable really confuses me...

like image 591
bash0r Avatar asked Sep 12 '25 05:09

bash0r


2 Answers

It represents "extra" information that the developer can use when working with a LowLevelKeyboardProc, for instance, to indicate a certain or special type of keyboard event (like an artificially generated keystroke). In a LowLevelKeyboardProc, the lParam is a pointer to a KBDLLHOOKSTRUCT that holds the dwExtraInfo.

Here is a good example of it in use: https://web.archive.org/web/20170710091853/http://globalmousekeyhook.codeplex.com/discussions/286784

While dwExtraInfo is meant to be a pointer, in that example they just set it to an arbitrary value 111.

like image 54
edtheprogrammerguy Avatar answered Sep 14 '25 18:09

edtheprogrammerguy


It is the exact same value as you see used in keybd_event(). Or the KEYBDINPUT structure used by SendInput(). Or what you get from GetMessageExtraInfo(). Which describes it:

Extra message information is an application- or driver-defined value associated with the current thread's message queue.

So as long as you don't add any extra info to a keyboard message that you generate with keybd_event() or SendInput(), or the driver doesn't add anything (the default keyboard driver doesn't), then this field is of no interest to you.

like image 22
Hans Passant Avatar answered Sep 14 '25 17:09

Hans Passant