Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA AddressOf Crash Office App

I want to run a simple snippet, but evertime Access and Excel crash.

I'm running CallbackTest2, could you please help me. Thanks allot.

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" ( _
      ByVal lpPrevWndFunc As Long, _
      ByVal hwnd As Long, _
      ByVal msg As Long, _
      ByVal wParam As Long, _
      ByVal lParam As Long) _
      As Long

Sub CallbackTest2()
   CallWindowProc AddressOf CallbackMsg, 0, 0, 0, 0
End Sub

Public Function CallbackMsg _
      (msg As String, _
      ByVal nUnused1 As Long, _
      ByVal nUnused2 As Long, _
      ByVal nUnused3 As Long)

   MsgBox "CallBack Work :)" & msg
End Function
like image 291
Summer-Time Avatar asked Nov 11 '22 04:11

Summer-Time


1 Answers

Found the solution.

Things I change:

  • CallbackMsg from function to sub
  • String Parameter filled with Data VarPtr("Hallo Welt")

RUNNING VERSION:

Private Declare Function CallWindowProc _
                          Lib "user32.dll" Alias "CallWindowProcA" ( _
                              ByVal lpPrevWndFunc As Long, _
                              ByVal hwnd As Long, _
                              ByVal msg As Long, _
                              ByVal wParam As Long, _
                              ByVal lParam As Long) As Long

private Sub CallbackTest2()
   CallWindowProc AddressOf CallbackMsg, VarPtr("Hallo Welt"), 0, 0, 0
End Sub

Private Sub CallbackMsg( _
        msg As String, _
        ByVal nUnused1 As Long, _
        ByVal nUnused2 As Long, _
        ByVal nUnused3 As Long)
    MsgBox msg
End Sub
like image 114
Summer-Time Avatar answered Nov 15 '22 07:11

Summer-Time