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.
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
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.
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.