Wednesday, 12 April 2017

Excel VBA C++ .dll

First post here. I'm trying to follow the numerous tutorials out there for compiling a C++ dll as an Excel function and running it as an Excel function, but can't seem to get it to work. I'm running Windows 10 64-bit, Visual Studio Community 2017, and Excel 2016 64-bit.



I've followed the tutorials out there. I started a new project as a "Windows Desktop Wizard", and created an empty project as a DLL. This is my code below.
There are only these two files, in the "Source Files" folder under my project.




funct.cpp:



double __stdcall squareFxn(double x) {
return x * x;
}


This is my Def file, defFile.def:



LIBRARY "square"

EXPORTS
squareFxn


In the project properties, I made sure to set the Platform to x64 in the configuration manager, and added "defFile.def" without quotes to Linker/Input/Module Definition File.



It compiled fine. I put the following into VBA:



Declare PtrSafe Function squareFxn Lib "C:\MyPath\square.dll" (ByVal x As Double) As Double



When I debugged in Visual Studio, and ran in an Excel cell:



=squareFxn(5)


Visual studio is saying that x is an extremely small number (6.234E-310) and the function is returning zero.



The other problem I'm having is when I try to do this as a reference, with the appropriate changes (squareFxn(double &x) in funct.cpp and ByRef instead of ByVal in the VBA function declaration), I'm getting a read access error. I tried running Excel as an administrator, to no avail.




Does anybody know what's going on here? Any help would be greatly appreciated!!

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...