Keyboard hook... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Keyboard hook...

Hi,

this function is called:

void SetKBHook(void)
{
KH1 = SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)KHookProc,hInst,0L);
}

KH1 is 0, why? :(
-hInst is my app instance
-KHookProc is the correct HookProc

hhheelllppp...

cu,

illuminator
(Novus ordo seclorum)

Comments

  • pingpongpingpong Posts: 937Member
    What OS? WH_KEYBOARD_LL does not work in 9x.

    Also, is this in an EXE or DLL? Because passing an hInstance should be done only with system-wide hooks that reside in a DLL. For a local hook in an EXE use:
    KH1 = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KHookProc, NULL, GetCurrentThreadId());

    Which hooks the keyboard for the current process only.

    : Hi,
    :
    : this function is called:
    :
    : void SetKBHook(void)
    : {
    : KH1 = SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)KHookProc,hInst,0L);
    : }
    :
    : KH1 is 0, why? :(
    : -hInst is my app instance
    : -KHookProc is the correct HookProc
    :
    : hhheelllppp...
    :
    : cu,
    :
    : illuminator
    : (Novus ordo seclorum)
    :

  • illuminatorilluminator Posts: 15Member
    Thx !! :)

    cu,

    illuminator
    (Novus ordo seclorum)

  • illuminatorilluminator Posts: 15Member
    Hi

    1. Thx. Your code works. :)

    2. Hm, i tried to convert it to VB.NET, works also, but this structure causes huge problems:

    C:
    typedef struct tagKBDLLHOOKSTRUCT {
    DWORD vkCode;
    DWORD scanCode;
    DWORD flags;
    DWORD time;
    DWORD dwExtraInfo;
    } KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;

    VB.Net:
    Public Structure KBDLLHOOKSTRUCT
    Public vkCode As Integer
    Public scanCode As Integer
    Public flags As Integer
    Public time As Integer
    Public dwExtraInfo As Integer
    End Structure
    - Wrong :( Why?

    PS: you guys are great! ;)



    cu,

    illuminator
    (Novus ordo seclorum)

  • pingpongpingpong Posts: 937Member
    [b][red]This message was edited by pingpong at 2003-1-20 11:43:29[/red][/b][hr]
    /edit Yikes, got my VB mixed up with C#. Why dont you use C# by the way?

    You need the StructLayout attribute thingy..
    [code]
    Imports System.Runtime.InteropServices

    _
    Public Structure KBDLLHOOKSTRUCT
    Public vkCode As Integer
    Public scanCode As Integer
    Public flags As Integer
    Public time As Integer
    Public dwExtraInfo As Integer
    End Structure
    [/code]
    So .NET wont mess around with it and tries to optimize it. The pack=1 is standard Win32 API stuff.

    Dont know how are you going to make the LowLevelKeyboardProc work though, this might work:
    [code]
    _
    Public Shared Function LowLevelKeyboardProc(nCode As Integer, wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    [/code]
    If it doesnt, it means that Windows is trying to send a NULL in the lParam, dont know how you fix that in VB.NET, in C# you can declare it as int lParam then do the appropriate cast in a fixed block.

    Good luck.

    : Hi
    :
    : 1. Thx. Your code works. :)
    :
    : 2. Hm, i tried to convert it to VB.NET, works also, but this structure causes huge problems:
    :
    : C:
    : typedef struct tagKBDLLHOOKSTRUCT {
    : DWORD vkCode;
    : DWORD scanCode;
    : DWORD flags;
    : DWORD time;
    : DWORD dwExtraInfo;
    : } KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
    :
    : VB.Net:
    : Public Structure KBDLLHOOKSTRUCT
    : Public vkCode As Integer
    : Public scanCode As Integer
    : Public flags As Integer
    : Public time As Integer
    : Public dwExtraInfo As Integer
    : End Structure
    : - Wrong :( Why?
    :
    : PS: you guys are great! ;)
    :
    :
    :
    : cu,
    :
    : illuminator
    : (Novus ordo seclorum)
    :
    :



  • illuminatorilluminator Posts: 15Member
    Hi

    thx again! :)

    _
    Public Structure KBDLLHOOKSTRUCT
    Public vkCode As Integer
    Public scanCode As Integer
    Public flags As Integer
    Public time As Integer
    Public dwExtraInfo As Integer
    End Structure

    "Pack = 1" was not accepted, because "StructLayout" only expects one parameter.

    cu,

    illuminator
    (Novus ordo seclorum)

Sign In or Register to comment.