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?
In Excel I get "Run-Time error '453': Can't fine DLL entry point SimpleMath in c:\VS19Projects\CPP\SimpleMath\Debug\SimpleMath.dll".
I have used dumpbin /HEADERS SimpleMath.dll in attempt to view dll entry point. No entry point is returned. This would seem to indicate that my use of the *.def is incorrect.
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
I finally got this example Excel VBA accessible MSVC Dll working with code below. I had problems doing this in Visual Studio (not up MSBuild learning curve yet). So I did this using VS code with command line compiling for smdll.cpp and smapp.cpp as per comments in below code. I included both C++ test code (smapp.cpp) and VBA code below. Debug compile switches can be added [/LDd (smdll.cpp) & /Zi (smapp.cpp)] for the C++ test code. I used the VS2019 "Developer Command Prompt" to execute: "...>dumpbin /EXPORTS smdll.dll" to determine the VBA Alias (_SimpleMath@4).
Guidance on how to "get_up_the_curve" with VS2019 MSBuild to develop in VS2019 will be appreciated? I was able to get Build and Debug scripts working in VS Code. I was not able to get the *.def file method to work. Guidance on this will also be appreciated.
//smdll.cpp
// cl /EHsc /LD smdll.cpp
#include "smdll.h"
extern "C" {__declspec(dllexport) long __stdcall SimpleMath(long x)
{ long result = 0;
for (long a = 0; a <= x; a++) result += a;
return result;
}
}
// smdll.h
extern "C" {__declspec(dllexport) long __stdcall SimpleMath(long x);}
// smapp.cpp
// cl /EHsc smapp.cpp smdll.lib
#include <iostream>
#include "smdll.h"
using namespace std;
void main () {
const long x = 5;
long result = SimpleMath(x);
cout << "Ans: " << result << endl;
}
'Excel VBA subroutine
Option Explicit
'used "...>dumpbin /EXPORTS smdll.dll VS 2019 command to get below Alias for smapp
Declare Function smapp Lib "<Path_to>\SimpleMath\smdll.dll" Alias "_SimpleMath@4" (ByVal a As Long) As Long
Sub test()
Dim x, result As Long
x = 5
result = smapp(x)
End Sub