Serious problems with windows callbacks?

I might have gotten up too early this morning but I seem to be having a serious issue with SetWindowsHookEx and a callback.

I’m following the msdn articles as I normally do, the mouse hook is getting established but in my callback for the hook all the parameters being passed to the callback seem to be shifted one along.

nCode As Int32, wParam As UInteger, lParam As Integer

nCode contains wParam’s values, wParam contains lParam’s values and lParam contains some random garbage.

It acts the same on all versions of Xojo that I’ve tried from the latest beta all the way back to 2015r1.

Could you try this little demo and let me know what simple or stupid mistake I’m making as I can’t see it for the life of me.

https://www.dropbox.com/s/0jrkkyyqzznjwke/TestMouseTrapBroken.xojo_binary_project?dl=1

Thanks in advance.

Doesn’t the hook just require wParam & lParam?

Not according to:

https://msdn.microsoft.com/en-us/library/ms644986(v=VS.85).aspx

Ooops, sorry. My MouseHooks use Aaron Ballmans RB classes from a long time ago and they handle the callback and pass it to my function so the first parameter wasn’t in the declaration. This does work in Xojo 2014r2 - not tried any newer versions.

If it helps, here is some of the code that we are using:

[code]Protected Function InstallMouseHook(attachTo as Win32MouseHookHandler) As Boolean
#If TargetWin32 Then
Declare Function SetWindowsHookExA Lib “User32” (hookType As Integer, proc As Ptr, instance As Integer, threadID As Integer) As Integer
Declare Function GetCurrentThreadId Lib “Kernel32” As Integer

If attachTo = Nil Then
   Return False
End If

If mMouseAttached_ <> Nil Then 
  Return False
End If

mMouseAttached_ = attachTo

Const WH_MOUSE = 7
mMouseHookHandle_ = SetWindowsHookExA(WH_MOUSE, AddressOf MouseHookCallbackFunction, 0, GetCurrentThreadId)

Return mMouseHookHandle_ <> 0

#EndIf
End Function

Private Function MouseHookCallbackFunction(nCode as Integer, wParam as Integer, lParam as Integer) As Integer
#If TargetWin32 Then
Declare Function CallNextHookEx Lib “User32” (hookHandle As Integer, code As Integer, wParam As Integer, lParam As Integer) As Integer

Dim result As Integer
result = 0

If nCode >= 0 And mMouseAttached_ <> Nil Then
  result = mMouseAttached_.MouseProc(wParam, lParam)
End If

If result = 0 Then
  Return CallNextHookEx(mMouseHookHandle_, nCode, wParam, lParam)
Else
  Return result
End If

#EndIf
End Function
[/code]

Did you check nCode >=0? Because if nCode < 0 you are supposed to pass ther message on:

The parameters are corrupting, so I cant check nCode as its actually containing wParam’s info.

I’ve found the problem.

MouseButtonHook

needs to be a Shared Method of the Window or in a Module.

Thanks Kevin, the fact that you had working code spurred me on to double check everything.

At a guess your MouseHookCallbackFunction is in a Module which is why your version worked and mine doesn’t even though we have pretty much the same code.

[quote=407708:@JulianS]I’ve found the problem.

MouseButtonHook

needs to be a Shared Method of the Window or in a Module.

Thanks Kevin, the fact that you had working code spurred me on to double check everything.

At a guess your MouseHookCallbackFunction is in a Module which is why your version worked and mine doesn’t even though we have pretty much the same code.[/quote]

Glad you got it working. For a minute I was worried that Xojo had broken something.
The Install, Callback & Remove methods are in a module (with callback being private). The Install method accepts an interface that implements the MouseProc method. mMouseAttached_ is a private property within the module that stores the object passed into Install until the Remove method is called.
If you didn’t already know, you can only have one callback for each type of hook which can make them harder to deal with.