I am having problems getting a simple Excel VBA addin working. I am using the simple example at https://www.vitoshacademy.com/c-adding-c-function-to-vbaexcel/ to get started, but can't get even this simple example working. SimpleMath.cpp, defMe.def, and VBA code is below. I have added to the SimpleMath.cpp properties: Linker: Input: Module Definition File: defMe.def. In VS2019, the C++ code successfully compiles and produces SimpleMath.dll as expected in the Debug folder. But I have tried many alternatives, but can't get the VBA code in Excel to work. As the many web variations of the example appear old, I am wondering if VS2019/C++ requires something not covered in the old examples?
Guidance and suggestions will be appreciated.
//SimpleMath.cpp
int __stdcall SimpleMath(int & x)
{
int result = 0;
for (int a = 0; a <= x; a++) {
result += a;
}
return result;
}
// defMe.def C++ code
LIBRARY "SimpleMath"
EXPORTS
SimpleMath
// VBA Code in Excel Module
Declare Function SimpleMath Lib "C:\VS2019Projects\SimpleMath\Debug\SimpleMath.dll" (ByRef x As Long) As Long
Sub TestMe()
Dim n As Long: n = 5
dim result as long
result = SimpleMath(n)
End Sub