Skip to content

Instantly share code, notes, and snippets.

@rmdavy
Created December 16, 2020 20:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rmdavy/6fec713505b8a28fce693c0fd79aca74 to your computer and use it in GitHub Desktop.
Save rmdavy/6fec713505b8a28fce693c0fd79aca74 to your computer and use it in GitHub Desktop.
x64FunctionPointerExample
Declare PtrSafe Function DispCallFunc Lib "OleAut32.dll" (ByVal pvInstance As LongPtr, ByVal offsetinVft As LongPtr, ByVal CallConv As Long, ByVal retTYP As Integer, ByVal paCNT As Long, ByRef paTypes As Integer, ByRef paValues As LongPtr, ByRef retVAR As Variant) As Long
Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtr
Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, ByVal lpProcName As String) As LongPtr
Const CC_STDCALL = 4
Private VType(0 To 63) As Integer, VPtr(0 To 63) As LongPtr
Sub SayHello()
Dim RetVal As LongPtr
Dim Result() As String
RetVal = stdCallA("kernel32", "LoadLibraryA", vbLongLong, "OleAut32.dll")
Debug.Print "Value1 " + Str(RetVal)
Debug.Print "Value2 " + Str(LoadLibrary("OleAut32.dll"))
End Sub
Public Function stdCallA(sDll As String, sFunc As String, ByVal RetType As VbVarType, ParamArray P() As Variant)
Dim i As Integer, V(), HRes As LongPtr
ReDim V(0)
V = P
For i = 0 To UBound(V)
If VarType(P(i)) = vbString Then P(i) = StrConv(P(i), vbFromUnicode): V(i) = StrPtr(P(i))
VType(i) = VarType(V(i))
VPtr(i) = VarPtr(V(i))
Next i
'MsgBox GetProcAddress(LoadLibrary(sDll), sFunc)
HRes = DispCallFunc(0, GetProcAddress(LoadLibrary(sDll), sFunc), CC_STDCALL, RetType, i, VType(0), VPtr(0), stdCallA)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment