Función de Access vbs problemática con versión 64 bits

Resuelto

Hola,

Tengo un código VBA que utilizo para modificar los botones de mi cuadro de diálogo. Funcionaba a la perfección hasta la introducción de la versión de 64 bits. He añadido las inscripciones "PtrSafe", pero el operador "AddressOf" sigue siendo problemático. En internet recomiendan cambiar el tipo de dato Long por LongPtr, pero no tengo ningún tipo Long. No sé qué hacer..

El error obtenido dice "Compile error : Type mismatch" y se encuentra en la línea

msgHook = SetWindowsHookEx(5, AddressOf CaptionBoutons, hInstance, GetCurrentThreadId())


Aquí está mi código en mi Módulo:

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 

Et voici l'appel de la fonction:

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

Merci beaucoup! :) 

1 respuesta

  • Votos
  • Fecha
  • Fecha descendente
  1. hola,

    el tipo Long está oculto en los sufijos: en VBA, el & después de un nombre (idHook&, lpfn&, msgHook&…) significa As Long. en 64 bits, AddressOf devuelve un LongPtr, de ahí el tipo de coincidencia en lpfn&.

    hay que pasar a LongPtr todo lo que sea handle o puntero, el resto permanece en 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

    y en el resto del módulo: Dim Rep%, hInstance As LongPtr en MsgBoxPerso, y la función de retorno en

    Private Function CaptionBoutons(ByVal nCode As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr Dim hWndChild As LongPtr

    con CaptionBoutons = 0 al final en lugar de False.

    de paso, el Exit Sub justo después de la llamada a MsgBoxPerso hace que el Select Case nunca se ejecute.

    1. ¡Un enorme gracias, funciona muy bien!