SetWindowsHookEx, KeyboardProc and Non-static members

I am creating a keyboard hook, wherein KeyboardProc is a static member of a class CWidget.

class CWidget
{
   static LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam );

};

I want to call the non-static members of CWidget inside the CWidget::KeyboardProc.

What is the best way to do it?

KeyboardProc does not have any 32 bit DWORD where I can pass the 'this' pointer.

Answers


Given that you probably only want one keyboard hook installed at a time, just add a static pThis member to your class:

class CWidget
{
    static HHOOK m_hHook;
    static CWidget *m_pThis;

public:
    /* NOT static */
    bool SetKeyboardHook()
    {
        m_pThis = this;
        m_hHook = ::SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, /* etc */);
    }

    // Trampoline
    static LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
    { return m_pThis->KeyboardProc(code, wParam, lParam); }

    LRESULT KeyboardProc(int code, WPARAM wParam, LPARAM lParam);

    /* etc. */
};

Need Your Help

Updating Class/IL in Assembly

c# .net

If I have an assembly which I would like to update a few files in what tool do I need in order to do this?

What's the difference between RealGetWindowClass and GetClassName? (or what is a “Window type” versus a “Window class name”?)

windows user-interface winapi

There is a difference in the description on MSDN, however, I do not understand what the text means what the difference is.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.