1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
// C++ Declaration
WINOLEAUTAPI VariantClear(__inout VARIANTARG * pvarg);
//C++ Function
__declspec(dllexport) DWORD WINAPI XLLRequest(DWORD src, DWORD dst, DWORD, DWORD) {
BSTR *request((BSTR*)src);
VARIANT *result((VARIANT*)dst);
::VariantClear(result);
}
// VBA Declaration
Public Declare PtrSafe Function GetProcAddress Lib "Kernel32.dll" (ByVal module As LongPtr, ByVal procName As String) As LongPtr
Public Declare PtrSafe Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal func As LongPtr, ByVal par1 As LongPtr, ByVal par2 As LongPtr, ByVal par3 As LongPtr, ByVal par4 As LongPtr) As LongPtr
// VBA
Function Check()
Static Func_Address As LongPtr
Dim My_DLL_Address As LongPtr
My_DLL_Address = LoadLibrary(C:\temp\" & "My.xll") ' not hardcoded in real life.
' returns: 140717666926592
Func_Address = GetProcAddress(My_DLL_Address, "XXLRequest")
' returns: 140717667776880
' Both variants are killing Excel.
Get_XLLReqst = CallWindowProc(Func_Address, VarPtr("NAMES"), VarPtr(result), 0, 0)
'or
Get_XLLReqst = CallWindowProc(Func_Address, 0, 0, 0, 0)
End function
|