I have this static callback function in MyClass, and I try to call another static function from it. There is a problem however, one of the arguments that Register() takes is a non-static class variable.
I thought of using the “this” keyword to overcome this problem but it seems I am unable to (‘this’ : can only be referenced inside non-static member functions). Here is my code:
class MyClass
{
...
static LRESULT CALLBACK klHkProc(int nCode, WPARAM wParam, LPARAM lParam);
static BOOL Register(DWORD vKey,KEYBLOCK* ptrKEYBLOCK);
KEYBLOCK *kb;
...
}
LRESULT CALLBACK MyClass::klHkProc(int nCode, WPARAM wParam, LPARAM lParam)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
if (wParam == WM_KEYDOWN)
{
MyClass::Register(p->vkCode,this->kb);
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
Any suggestions?
Given what you wrote it is hard to answer this without more information.
However, assuming that all instances of
MyClassshould be processed, I suggest adding astatic std::list<MyClass *>into which you place each instance’sthispointer in the constructor forMyClassand then in the destructor ofMyClassto remove thethisfrom this list.Then in your static klHkProc() you would iterate over the static list of all instances of
MyClassand for each one callMyClass::Register()with thekbof each such registered instance ofMyClass.Here is a rough outline of the code to do this: