Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

c++ - Excel VBA, Can't Find DLL Entry Point from a DLL file

I am attempting to use Excel VBA's ability to access and use functions from DLL files.

example:

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

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

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

When the VBA in Excel tries to access the created .dll file from this tutorial, it produces a runtime error (453): 'Can't find DLL entry point Add in "pathfile.dll".


I am a novice when it comes to the CC++ language. I have spent over 6 hours of:

  • trying to make tweaks to the vanilla tutorial
  • starting over
  • googling for help, and similar issues
  • making tweaks to the statements within VBA

And yet I feel further from a solution.

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


Any help would be much appreciated :)


Edit

Code Files (as requested):

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm going to post this in a solution, as it doesn't fit in an comment.

The "inconsistent dll linkage" warning: I copied your exact code from the question as it is at this point (it might change in the future), and placed it in a newly created VStudio 2015 project:

  • Configuration type: Dynamic Library (.dll)
  • Using precompiled headers (although, I usually don't do it)

The project compiled with no warnings, if I define MATHLIBRARY_EXPORTS either:

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

The only thing that I can imagine for you to still get the warning when building yours, is because you are defining the macro for the wrong configuration.
Example: you are building your project for Debug - x86, but you define the macro for Release - x86 (or Debug - x64).
You must check (it would be better select All Platfroms and All Configurations, and only define the macro once) that build configurations and settings configurations match, like in the image below:

VStudio project settings

But anyway, this warning is benign, the .dll is still built, and the symbols exported.

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

Can't find DLL entry point Add in "pathfile.dll"

as I specified on one of my comments, I don't think that Excel is able to import C++ style exports because of [MS.Docs]: Decorated Names (C++ name mangling). While it searches for Add, your .dll exports the following symbols as shown in the (Dependency Walker) image below (you can play with the highlighted button and see how Dependency Walker is able to demangle those names):

enter image description here

Those (gibberish) names you should import from Excel, but I doubt that's possible. As a workaround you could either:

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

[SO]: Linker error when calling a C function from C++ code in different VS2010 project (@CristiFati's answer) contains all the details (pay attention on extern "C": [MS.Docs]: extern (C++)).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...