0

I have compiled a DLL from a C++ Library I have written according to this tutorial.

The dumpbin for the dll is as follows:

  Section contains the following exports for HelloDLL.dll
    00000000 characteristics
    FFFFFFFF time date stamp
        0.00 version
           1 ordinal base
           6 number of functions
           6 number of names

    ordinal hint RVA      name

          1    0 00011032 ?Add@Functions@MathLibrary@@SANNN@Z = @ILT+45(?Add@Functions@MathLibrary@@SANNN@Z)
          2    1 00011037 ?AddMultiply@Functions@MathLibrary@@SANNN@Z = @ILT+50(?AddMultiply@Functions@MathLibrary@@SANNN@Z)
          3    2 000112EE ?Multiply@Functions@MathLibrary@@SANNN@Z = @ILT+745(?Multiply@Functions@MathLibrary@@SANNN@Z)
          4    3 000110F5 Add = @ILT+240(_Add)
          5    4 00011073 AddMultiply = @ILT+110(_AddMultiply)
          6    5 0001105F Multiply = @ILT+90(_Multiply)

Now I want to use the Functions in an Excel-VBA Project like this:

Declare Function LIB_AddMultiply Lib "C:\Users\xxxx\source\repos\HelloDLL\Debug\HelloDLL.dll" Alias "AddMultiply" (ByVal a As Double, ByVal b As Double) As Double

Public Sub test()
    Dim a As Double
    Dim b As Double
    a = 3
    b = 4
    Dim c As Double
    c = LIB_AddMultiply(a, b)
    MsgBox ("hi " + c)
End Sub

But whenever I want to run the test() I get a Bad DLL Calling convention; Error 49.

I already looked at the following (and some other) resources, but couldn't resolve my problem:

Do you have any advice?

Thanks a lot...

UPDATE:

This is the code for the header file:

#pragma once
#ifdef MATHLIBRARY_EXPORTS  
#define MATHLIBRARY_API __declspec(dllexport)   
#else  
#define MATHLIBRARY_API __declspec(dllexport)   
#endif  

namespace MathLibrary
{
    class Functions
    {
    public:
        static MATHLIBRARY_API double Add(double a, double b);
        //[...]
    };

    extern "C" MATHLIBRARY_API double Add(double a, double b)
    {
        return MathLibrary::Functions::Add(a, b);
    }
    //[...]
}
Fabian Schneider
  • 799
  • 1
  • 13
  • 40
  • 2
    These functions are being compiled as `__cdecl`, the default for C++ code. Not compatible with VBA. The easiest way to do this for a DLL that you designed to be used from VBA is to change the default. Project > Properties > Configuration combobox = All > C/C++ > Advanced > "Calling convention" = stdcall. – Hans Passant Mar 19 '18 at 10:25

1 Answers1

0

Thank you Hans Passant for your help;

I have changed the project properties to the calling convention; then did another dumpbin with the result of my Functions being named as

_Add@16

and then just changed the Alias in the VBA code...

Fabian Schneider
  • 799
  • 1
  • 13
  • 40