Excel VBA, cannot find dll entry point from dll file

I am trying to use the features of Excel VBA to access and use functions from DLL files.

Example:

Private Declare Function funcName Lib _ "<filePath\File.dll>" _ (ByRef a As Double, ByRef b As Double) As Double 

Following the instructions of the Mircosoft tutorial on how to create a DLL file, it leads to 3 warnings ( C4273 ) when I try to build a project for three declared functions:

 'MathLibrary::Functions::Add': inconsistent dll linkage, 'MathLibrary::Functions::Multiply': inconsistent dll linkage, 'MathLibrary::Functions::AddMultiply': inconsistent dll linkage 

When VBA in Excel tries to access the generated DLL file from this tutorial, it generates a runtime error ( 453 ): 'Cannot find the entry point of the DLL. Add to "path \ .dll file".


I start when it comes to C \ C ++. I spent more than 6 hours:

  • trying to do tricks in a vanilla textbook.
  • beginning with
  • googling for reference and similar problems
  • amending statements in VBA

And yet I feel farther from the decision.

I am running 32-bit Excel on 64-bit Windows.


Any help would be greatly appreciated :)


Edit

Code files (on request):

MathLibrary.cpp

 // MathLibrary.cpp : Defines the exported functions for the DLL application. // Compile by using: cl /EHsc /DMATHLIBRARY_EXPORTS /LD MathLibrary.cpp #include "stdafx.h" #include "MathLibrary.h" namespace MathLibrary { double Functions::Add(double a, double b) { return a + b; } double Functions::Multiply(double a, double b) { return a * b; } double Functions::AddMultiply(double a, double b) { return a + (a * b); } } 

MathLibrary.h

 // MathLibrary.h - Contains declaration of Function class #pragma once #ifdef MATHLIBRARY_EXPORTS #define MATHLIBRARY_API __declspec(dllexport) #else #define MATHLIBRARY_API __declspec(dllimport) #endif namespace MathLibrary { // This class is exported from the MathLibrary.dll class Functions { public: // Returns a + b static MATHLIBRARY_API double Add(double a, double b); // Returns a * b static MATHLIBRARY_API double Multiply(double a, double b); // Returns a + (a * b) static MATHLIBRARY_API double AddMultiply(double a, double b); }; } 

stdafx.h

 // stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #pragma once #include "targetver.h" #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Windows Header Files: #include <windows.h> // TODO: reference additional headers your program requires here 

targetver.h

 #pragma once // Including SDKDDKVer.h defines the highest available Windows platform. // If you wish to build your application for a previous Windows platform, // include WinSDKVer.h and // set the _WIN32_WINNT macro to the platform you wish to support // before including SDKDDKVer.h. #include <SDKDDKVer.h> 

VBA module

 Private Declare Function Add Lib _ "c:\<Path>\MathLibrary.dll" _ (ByRef a As Double, ByRef b As Double) As Double Sub useAddXL() MsgBox Add(1, 2) End Sub 
+1
source share
1 answer

I am going to post this in the solution, as it does not fit in the comments.

Warning "Inconsistent DLL Linkage": I copied your exact code from the question as it is at the moment (it may change in the future), and put it in the newly created VStudio 2015 project:

  • Type of configuration: dynamic library (.dll)
  • Using precompiled headers (although usually I don't)

The project compiled without warning if I define MATHLIBRARY_EXPORTS either:

  • In the file MathLibrary.cpp #define MATHLIBRARY_EXPORTS ( before #include "MathLibrary.h" )
  • As a project setting: adding it to Project Properties β†’ Configuration Properties β†’ C / C ++ β†’ Preprocessor β†’ Preprocessor Definitions (next to other macros separated by semicolons ( ; ))

The only thing I can imagine that you still receive a warning when creating your own is that you define a macro for the wrong configuration .
Example: you are building your project for Debug - x86, but you are defining a macro for Release - x86 (or Debug - x64).
You should check (it would be better to select All Platfroms and All Configurations and define the macro only once) so that the configuration configurations of the configurations match , as in the figure below:

VStudio project settings

But in any case, this warning is benign, the .dll is still built, and the characters are exported.

Next, in your VBA module, you declare the name of the Add (plain) function. Based on the error message:

Cannot find DLL entry point Add to "path \ .dll file"

as I pointed out in one of my comments, I don’t think Excel can import the export of C ++ styles because of [MS.Docs]: decorated names (C ++ name distortion). While searching for Add, your .dll will export the following characters, as shown in the image (Dependency Walker) below (you can play with the button highlighted and see how Dependency Walker can parse these names):

enter image description here

You should import these (crazy) names from Excel, but I doubt that it is possible. As a workaround, you can:

  • Remove C ++ functions (class and namespace), define and export 3 simple functions
  • Write 3 C functions (wrappers over 3 methods) and export functions, not methods

[SO]: a linker error when calling a C function from C ++ code in another VS2010 project (@CristiFati answer) contains all the details (note extern "C" : [MS.Docs]: extern (C ++) ).

+1
source

All Articles