Code:
Declare PtrSafe Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As LongPtr, ByVal hwnd As LongPtr, ByVal Msg As LongPtr, ByVal wParam As LongPtr, Optional ByVal lParam As LongPtr) As LongPtr
Declare PtrSafe Sub CopyMemoryX64 Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
Dim ClassA As New Class1
Dim Address1 As LongPtr
Address1 = GetObjSubAddressX64(ClassA, 1)
Dim A As LongPtr, B As LongPtr, c As LongPtr, Result As LongPtr
A = 11
B = 22
c = 33
c = CallWindowProc(ByVal Address1, ObjPtr(ClassA), ByVal A, ByVal B, VarPtr(Result))
MsgBox "Result=" & Result
Function GetObjSubAddressX64(obj As Object, ID As Long) As LongPtr
Dim FuncOffset As LongPtr, SubVoid As LongPtr, pVTable As LongPtr
FuncOffset = (ID - 1) * 8 + 7 * 8 '&h1c=28 (就是跳过7个地址,类是IDispatch)
CopyMemoryX64 pVTable, ByVal ObjPtr(obj), 8
SubVoid = pVTable + FuncOffset
CopyMemoryX64 GetObjSubAddressX64, ByVal SubVoid, 8
End Function
Code:
Public Function Add2(ByVal A As LongPtr, ByVal B As LongPtr) As LongPtr
MsgBox "excel class1 A=" & A & ",B=" & B
Add2 = A + B
End Function