I have a problem hooking mouse event in C++Builder, it's showing an error:
[bcc32 Error] MainUnit.cpp(24): E2034 Cannot convert 'long * (__stdcall * (_closure )(int,unsigned int,long))(int,unsigned int,long)' to 'long (__stdcall *)(int,unsigned int,long)' Full parser context MainUnit.cpp(22): parsing: void TInputHookMain::SetHook()`.
And:
[bcc32 Error] MainUnit.cpp(24): E2342 Type mismatch in parameter 'lpfn' (wanted 'long (__stdcall *)(int,unsigned int,long)', got 'void') Full parser context MainUnit.cpp(22): parsing: void TInputHookMain::SetHook()`.
Here is my code in my main unit:
HHOOK mouseHook;
void TInputHookMain::ReleaseHook()
{
UnhookWindowsHookEx(mouseHook);
}
void TInputHookMain::SetHook()
{
if ( !(mouseHook = SetWindowsHookExW(WH_MOUSE_LL, MouseInputHook, NULL, 0)) )
{
ShowMessage("Failed To Install Mouse Hook!");
}
}
LRESULT __stdcall TInputHookMain::MouseInputHook(int nCode, WPARAM MsgID, LPARAM Data)
{
if ( nCode >= 0 )
{
switch ( MsgID ) {
case WM_LBUTTONDOWN: ShowMessage("Left Mouse Button Clicked!");
break;
case WM_RBUTTONDOWN: ShowMessage("Right Mouse Button Clicked!");
break;
}
}
return CallNextHookEx(mouseHook, nCode, MsgID, Data);
}
Sadly, I cannot find where the problem is coming from. I tried to follow a C++ Tutorial step-by-step, but no luck so far.
MouseInputHook()
should be a global function, or a static class method. So, add the static
keyword to the declaration of MouseInputHook()
in your class header file.
The error message means: a _closure
function doesn't match the required callback function prototype. _closure
means "a pointer to a non-static class method" in the C++Builder compiler. A non-static class method has a hidden this
parameter, which makes it incompatible with the function prototype that SetWindowsHookExW()
requires.