For practice, I set up a thread-specific, local mousehook on my Visual Basic application.
The problem is that the hit test always a 0 for the area of the screen where the mouse is currently located. And the Window class also retrieves a null string all the time.
Can someone point out the cause of the problem? Here's the code snippet.
I am suspecting there is some byte alignment issue with the MOUSEHOOKSTRUCT. The documentation is very lean and does not mention anything like that, though.
Okay, I figured it out. The modified/corrected code is attached.
The lParam is a pointer to the MOUSEHOOKSTRUCT and should've been passed by value to the CopyMemory API. That was the reason the data was not getting copied from the lParam parameter to the structure and hence all the values I saw were wierd.
Secondly, the Window class name would not get displayed for two reasons. The first reason being the data in the MOUSEHOOKSTRUCT was not present as mentioned above. Second, I made a silly mistake because I'd not slept well when I wrote that sample this early morning. Look at this line, I'd sent an unformatted BSTR to GetClassName.
Code:
LngRetVal = GetClassName(MouseData.hwnd, StrClassName, MAX_SIZE)
If LngRetVal > 0 Then
StrClassName = String(MAX_SIZE, vbNullChar)
LngPos = InStr(1, StrClassName, vbNullChar)
should've been
Code:
StrClassName = String(MAX_SIZE, vbNullChar)
LngRetVal = GetClassName(MouseData.hwnd, StrClassName, MAX_SIZE)
If LngRetVal > 0 Then
LngPos = InStr(1, StrClassName, vbNullChar)
The in-out/reference string should obviously have been formatted before calling GetClassName.