SetWindowsHookEx - LowLevelMouseProc
Hello there,
first of all: My english is not as good as it should be. So be patient if there is any confusing stuff I'm writing ;-)
As mentioned in title bar I`ve got some problems implementing a LowLevelMouseProc callback function with SetWindowsHookEx.
The LowLevelMouseProc function is implemented in a DLL-file which I'm loading with LoadLibrary in my main cpp-file.
The Source code is mentioned below. The Problem is: Every time I call the function GetFrame the hook is installed successfully, but the mouse pointer is hanging (about 10 seconds) and after that my program crashes.
Relating to MSDN documentation there is nothing I missed / typed in wrong.
Any Idea?
Code:
// DLL
LOWLEVELMI_API
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if( nCode == HC_ACTION )
{
MSLLData = *(reinterpret_cast<MSLLHOOKSTRUCT*>( lParam ));
switch( wParam )
{
case WM_LBUTTONDOWN:
{
*(reinterpret_cast<PULONGLONG>(&MSFrame)) =
(static_cast<ULONGLONG>(MSLLData.pt.x) << 32) + MSLLData.pt.y;
MSStat = MSStat | bFlagDown;
}
break;
case WM_LBUTTONUP:
{
*(reinterpret_cast<PULONGLONG>(reinterpret_cast<PBYTE>(&MSFrame) + 8)) =
(static_cast<ULONGLONG>(MSLLData.pt.x) << 32) + MSLLData.pt.y;
MSStat = MSStat | bFlagUp;
}
break;
default:
return CallNextHookEx(miHook, nCode, wParam, lParam);
}
}
return CallNextHookEx(miHook, nCode, wParam, lParam);
}
// main.cpp
MSFRAME __cdecl GetFrame(void)
{
MSFRAME MSFrame = {0};
LPCSTR lpDLLPath = "..\\LowLevelMI\\Release\\LowLevelMI.dll";
HMODULE hDLL = LoadLibrary(lpDLLPath);
if( hDLL != NULL )
{
typedef LRESULT (*LPLOWLEVELMOUSEPROC)(int, WPARAM, LPARAM);
typedef MSFRAME (*LPGETMOUSEINPUT) (void);
LPGETMOUSEINPUT lpGetMouseInput = reinterpret_cast<LPGETMOUSEINPUT>(GetProcAddress(hDLL, "GetMouseInput"));
HHOOK miHook = SetWindowsHookEx(
WH_MOUSE_LL,
reinterpret_cast<HOOKPROC>(GetProcAddress(hDLL, "LowLevelMouseProc")),
hDLL,
0
);
if( miHook != NULL )
{
lpGetMouseInput();
}
else
{
CHAR szError[50] = {0};
sprintf_s<50>( szError, "unable to set windows hook: %d", GetLastError() );
throw std::exception(szError);
}
UnhookWindowsHookEx(miHook);
FreeLibrary(hDLL);
}
else
throw std::exception("LowLevelMI.dll was not found");
return MSFrame;
}