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:

// Widget.h
class CWidget
{
    static HHOOK m_hHook;
    static CWidget *m_pThis;

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

    // Trampoline
    static LRESULT CALLBACK StaticKeyboardProc(int code, WPARAM wParam, LPARAM lParam)
    {
        ASSERT(m_pThis != NULL);
        m_pThis->KeyboardProc(code, wParam, lParam);
    }

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

    /* etc. */
};

You need to define the static member:

// Widget.cpp
CWidget *CWidget::m_pThis = NULL;

Need Your Help

wsHttpBinding, just allow certain accounts to access a service

.net wcf .net-4.5 wcf-binding wcf-security

I have a simple WCF Service hosted on IIS 8 that uses wsHttpBinding. I want to be able to control wich users (domain accounts) have access to the service. How can I do that? Perhaps there are several

What's the best way to divide large files in Python for multiprocessing?

python concurrency multiprocessing bioinformatics

I run across a lot of "embarrassingly parallel" projects I'd like to parallelize with the multiprocessing module. However, they often involve reading in huge files (greater than 2gb), processing them