Trying to set up a thread specifc hook in MFC
-
jeudi 23 août 2012 21:10
I am trying to capture some keyboard messages in a child window that's attached to a dialog.
I am getting an error trying to install the hook.
Error C2440: 'reinterpret_cast' : cannot convert from 'LRESULT (__stdcall CMyClass::* )(int,WPARAM,LPARAM)' to 'HOOKPROC'
below is my code
In .h
HHOOK m_hHook;
LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam, LPARAM lParam);
In .cpp
m_hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)(&CMyClass::GetMsgProc), AfxGetInstanceHandle, NULL);
LRESULT CALLBACK CMyClass:GetMsgProc(int code, WPARAM wParam, LPARAM lParam)
{
// implementation
}
can anyone help me point out what I am doing wrong?
Toutes les réponses
-
jeudi 23 août 2012 22:06
On 8/23/2012 5:10 PM, tigersuen wrote:
I am trying to capture some keyboard messages in a child window that's attached to a dialog.
I am getting an error trying to install the hook.
Error C2440: 'reinterpret_cast' : cannot convert from 'LRESULT (__stdcall CMyClass::* )(int,WPARAM,LPARAM)' to 'HOOKPROC'The hook procedure must be a standalone non-member function, or else a static member function. It cannot be a non-static member function of any class.
Igor Tandetnik
- Marqué comme réponse tigersuen jeudi 23 août 2012 22:41
-
jeudi 23 août 2012 22:31
You are right, so I tried to add static to my function
static LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam, LPARAM lParam);
Now I get this error
error C2664: 'SetWindowsHookExA': cannot convert parameter 3 from 'HINSTANCE (__stdcall *)(void)' to 'HINSTANCE'
-
jeudi 23 août 2012 22:37
On 8/23/2012 6:31 PM, tigersuen wrote:
Now I get this error
error C2664: 'SetWindowsHookExA': cannot convert parameter 3 from 'HINSTANCE (__stdcall *)(void)' to 'HINSTANCE'The third parameter should be AfxGetInstanceHandle(). You are missing a pair of parentheses. You want to actually call the function and pass its return value, but instead you are trying to pass the address of the function.
Igor Tandetnik
-
jeudi 23 août 2012 22:41
You are absolutely right.
I am very sorry for this careless mistake.

