I'm trying to use a C / C++ static function in Excel / VBA through a DLL.
I'm getting an exception when debugging in VS17, and I suspect it's an issue with the way the argument is passed (it's a double)
Exception thrown at 0x00007FFA28BBA14F (kernel32.dll) in EXCEL.EXE: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
here's my C code:
test.h
extern "C" __declspec(dllexport) double get_sum_cpp(double x);
test.cpp
double WINAPI get_sum_cpp(double x)
{
double res = x + x;
return res;
}
declaration in VBA:
Declare PtrSafe Function get_sum_cpp Lib "C:\Users\bbi\source\repos\Test\x64\Debug\Test.dll" (ByVal my_var As Double) As Double
test code in VBA:
Sub testSum()
Dim A As Double
Dim Asum As Double
A = 5
Asum = get_sum_cpp(A)
end sub
I'm running 64 bits excel, and the dll is compiled in debug mode 64 bits.
I have many more issues with the overall development (for example any function with more than one argument will crash excel entirely), but this is the smallest "unit test" I could get too.
I feel it's an issue with the way the VBA double argument is passed to the DLL function, (stack misalignment ?), but I can't figure out how to set it right. When debugging in VS17, the exception occurs before I reach the line "double res = x + x", so I suspect it's happening at the function declaration, so when the double argument is passed - so an issue with casting - again maybe stack misalignment ?
my exports seem OK - checked with dumpbin / EXPORTS. The function is found and eventually returns.
Any idea ?