3

I use this program in winx86 without any error but when i try use it in win x64 my problems started. I use ptrsafe in my code to let me run in win 7 64bit, I'll add that this module have more code but for limitations of this site i had delete that. if need to now that lines of my code tell me to put them here. please help me and tell me Why my code produce an error:

'UDT for passing data through the hook
Private Type MSGBOX_HOOK_PARAMS
   hwndOwner   As Long
   hHook       As Long
End Type

Private MSGHOOK As MSGBOX_HOOK_PARAMS
#If VBA7 Then

    Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
       Alias "SetWindowsHookExA" _
      (ByVal idHook As Long, _
       ByVal lpfn As Long, _
       ByVal hmod As Long, _
       ByVal dwThreadId As Long) As Long
 #Else

    Private Declare Function SetWindowsHookEx Lib "user32" _
       Alias "SetWindowsHookExA" _
      (ByVal idHook As Long, _
       ByVal lpfn As Long, _
       ByVal hmod As Long, _
       ByVal dwThreadId As Long) As Long
#End If

Public Function MsgBoxHookProc(ByVal uMsg As Long, _
                               ByVal wParam As Long, _
                               ByVal lParam As Long) As Long

   If uMsg = HCBT_ACTIVATE Then

      SetDlgItemText wParam, vbYes, "بلـي"
      SetDlgItemText wParam, vbNo, "خـير"
      SetDlgItemText wParam, vbIgnore, "انصـراف"
      SetDlgItemText wParam, vbOK, "تـــاييد"

      UnhookWindowsHookEx MSGHOOK.hHook

   End If

   MsgBoxHookProc = False

End Function

Public Function MsgBoxFa(Prompt, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Tiltle = "", Optional HelpFile, Optional Context) As Long

  'Wrapper function for the MessageBox API
Dim hwndThreadOwner As Long
Dim frmCurrentForm As Form
Set frmCurrentForm = Screen.ActiveForm

hwndThreadOwner = frmCurrentForm.hwnd

   Dim hInstance As Long

   Dim hThreadId As Long
   Dim hwndOwner As Long
   hwndOwner = GetDesktopWindow()
   hInstance = GetWindowLong(hwndThreadOwner, GWL_HINSTANCE)
   hThreadId = GetCurrentThreadId()

   With MSGHOOK
      .hwndOwner = hwndOwner
'in next line the error produced******************************
      .hHook = SetWindowsHookEx(WH_CBT, _
                                AddressOf MsgBoxHookProc, _
                                hInstance, hThreadId)
   End With


 MsgBoxFa = MessageBox(hwndThreadOwner, Prompt, Tiltle, Buttons)

End Function
Erik A
  • 31,639
  • 12
  • 42
  • 67
GR_mahdi
  • 31
  • 1
  • 3
  • 2
    Using *PtrSafe* requires using pointer-safe variable types. You didn't. lpfn, hmod, wParam, lParam are LongPtr, not Long. – Hans Passant Jan 12 '14 at 14:15
  • 2
    A late answer here but... Hans, are you *sure* that wParam and lParam require LongPtr? The window handles and the function pointer are obvious LongPtr parameters: lParam looks like a bitmask of integer flags which should be dclared as a Long; and wParam is a matter of guesswork. – Nigel Heffernan Jan 22 '15 at 11:44
  • 1
    @NigelHeffernan There is no guess work. `CBTProc` is [declared](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644977(v=vs.85)#syntax) with [`WPARAM`](https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types#WPARAM) and [`LPARAM`](https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types#LPARAM) which [are](https://stackoverflow.com/a/63801528/11683) `LongPtr`. – GSerg Dec 07 '21 at 20:44

0 Answers0