VBA - PPT Keyboard Hook not working with Office 64-bit

B

Bourgui

Hi all,

I have created an addin for PowerPoint, in which I have setup a keyboard
hook to assign shortcuts for my functions.

The problem I have is with Office 2010, the 64-bit version. When I attempt
to register the hook using SetWindowsHookEx, I get a LastDLLError of 126,
which is apparently "Module not found". I'm guessing this has to do with the
handle to PPT that I am passing on to SetWindowsHookEx, but I'm not able to
go any further.

The code looks like:
Code:
Public Declare PtrSafe Function SetWindowsHookEx Lib "user32" Alias
"SetWindowsHookExA" (ByVal idHook As Long, ByVal lpFn As LongPtr, ByVal hmod
As LongPtr, ByVal dwThreadId As Long) As Long
Public Declare PtrSafe Function GetModuleHandle Lib "kernel32" Alias
"GetModuleHandleA" (ByVal lpModuleName As String) As Long
Public Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As Long

Private hWndPPT As Long

Sub SetHook()
Dim lThreadID As Long

If Not GetPPTHandle Then Exit Sub

If isHooked Then Exit Sub  'We should NEVER set the same hook twice, as
lThreadID = GetCurrentThreadId

'Set a local hook
HookHandle = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyHandler, hWndPPT,
lThreadID)

If HookHandle <> 0 Then isHooked = True
End Sub

Function GetPPTHandle() As Boolean
GetPPTHandle = True
hWndPPT = GetModuleHandle(vbNullString)
If IsNull(hWndPPT) Then GetPPTHandle = False
End Function

I have tried "casting" the module's handle to a LongPtr, since I reckon
that's what it wants, but no luck, SetWindowsHookEx always returns the error.

Any idea what I'm doing wrong? I have never worked with 64-bit before.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top