Problematic Access Function vbs with 64-bit version
SolvedHello,
I have a VBA code that I use to modify the buttons of my dialogue box. It worked perfectly until the 64-bit version was implemented. I added the "PtrSafe" declarations, but the "AddressOf" operator remains problematic. On the internet, people say to change the data type from Long to LongPtr, but I don't have any Long data type.. I don't know what to do..
The error obtained says "Compile error: Type mismatch" and it points to the line
msgHook = SetWindowsHookEx(5, AddressOf CaptionBoutons, hInstance, GetCurrentThreadId())
Here is my code in my Module :
Option Explicit Private Declare PtrSafe Function SetWindowsHookEx& Lib "USER32" Alias "SetWindowsHookExA" _ (ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal dwThreadId&) Private Declare PtrSafe Function GetCurrentThreadId& Lib "kernel32" () Private Declare PtrSafe Function CallNextHookEx& Lib "USER32" _ (ByVal hHook&, ByVal CodeNo&, ByVal wParam&, ByVal lParam&) Private Declare PtrSafe Function GetWindow& Lib "USER32" (ByVal hWnd&, ByVal wCmd&) Private Declare PtrSafe Function SetWindowText& Lib "USER32" Alias "SetWindowTextA" _ (ByVal hWnd&, ByVal lpString$) Private Declare PtrSafe Function UnhookWindowsHookEx& Lib "USER32" (ByVal hHook&) Private msgHook& Private TitreBtn$(1 To 2) Function MsgBoxPerso(Prompt$, Optional Title$, Optional Icon&, Optional Caption1$ = "Oui", _ Optional Caption2$ = "Non", Optional Cancel As Boolean = False) As Byte Dim Rep%, hInstance& TitreBtn(1) = Caption1 TitreBtn(2) = Caption2 msgHook = SetWindowsHookEx(5, AddressOf CaptionBoutons, hInstance, GetCurrentThreadId()) Rep = MsgBox(Prompt, Icon + IIf(Cancel, vbYesNoCancel, vbYesNo), Title) If Rep > 5 Then MsgBoxPerso = Rep - 5 Else MsgBoxPerso = 0 Erase TitreBtn End Function Private Function CaptionBoutons&(ByVal nCode&, ByVal wParam&, ByVal lParam&) Dim hWndChild& If nCode < 0 Then CaptionBoutons = CallNextHookEx(msgHook, nCode, wParam, lParam) Exit Function End If If nCode = 5 Then hWndChild = GetWindow(wParam, 5) Call SetWindowText(hWndChild, TitreBtn(1)) hWndChild = GetWindow(hWndChild, 2) Call SetWindowText(hWndChild, TitreBtn(2)) UnhookWindowsHookEx msgHook End If CaptionBoutons = False End Function
And here is the function call:
Dim MonMessage As String Dim Rep As Byte MonMessage = "Veuillez sélectionner le type de support désiré pour cet avis." Rep = MsgBoxPerso(MonMessage, "Sélection du type de support", vbQuestion, "Papier", "Courriel", True) Exit Sub Select Case Rep Case 0 'Annuler Exit Sub Case 1 'Papier Msgbox "Papier" Case 2 'Courriel Msgbox "Courriel" End Select
Thanks a lot! :)
1 answer
-
Hello,
The long is hidden in the suffixes: in VBA, the & after a name (idHook&, lpfn&, msgHook&...) means As Long. In 64 bits, AddressOf returns a LongPtr, hence the type mismatch on lpfn&.
All handles or pointers must be passed as LongPtr, while the rest remains as Long:
Private 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 LongPtr Private Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As Long Private Declare PtrSafe Function CallNextHookEx Lib "user32" _ (ByVal hHook As LongPtr, ByVal nCode As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal wCmd As Long) As LongPtr Private Declare PtrSafe Function SetWindowText Lib "user32" Alias "SetWindowTextA" _ (ByVal hWnd As LongPtr, ByVal lpString As String) As Long Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As LongPtr) As Long Private msgHook As LongPtr
And in the rest of the module: Dim Rep%, hInstance As LongPtr in MsgBoxPerso, and the callback function as
Private Function CaptionBoutons(ByVal nCode As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr Dim hWndChild As LongPtr
with CaptionBoutons = 0 at the end instead of False.
By the way, the Exit Sub right after the call to MsgBoxPerso means the Select Case never executes.