Fonction Access vbs problématique avec version 64bit

Résolu

Bonjour,

J'ai un code vba qui me sert à modifier les boutons de ma boîte de dialogue. Ça fonctionnait à la perfection jusqu'à l'implantation de la version 64 bits. J'ai ajouté les inscriptions "PtrSafe", mais l'opérateur "AddressOf" reste problématique. Sur internet, on dit de changer le type de donnée Long pour LongPrt, mais je n'ai aucun type de donnée Long.. Je ne sais pas quoi faire..

L'erreur obtenue dit "Compile error : Type mismatch" et elle se trouve sur la ligne 

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


Voici mon code dans mon 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

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! :) 
Windows / Chrome 156.0.0.0

1 réponse

  • Votes
  • Date
  • Date décroissante
  1. bonjour,

    le long est caché dans les suffixes : en vba, le & après un nom (idHook&, lpfn&, msgHook&…) veut dire As Long. en 64 bits, AddressOf renvoie un LongPtr, d'où le type mismatch sur lpfn&.

    il faut passer en LongPtr tout ce qui est handle ou pointeur, le reste reste 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

    et dans le reste du module : Dim Rep%, hInstance As LongPtr dans MsgBoxPerso, et la fonction de rappel en

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

    avec CaptionBoutons = 0 à la fin au lieu de False.

    au passage, le Exit Sub juste après l'appel de MsgBoxPerso fait que le Select Case ne s'exécute jamais.

    1. Un énorme merci, ça fonctionne très bien!