导入DLL函数VBA

时间:2018-03-19 08:36:46

标签: excel vba dll

我已根据this tutorial编写的C ++库编译了一个DLL。

dll的dumpbin如下:

  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)

现在我想在Excel-VBA项目中使用这样的函数:

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

但每当我想要运行test()时,我都会获得Bad DLL Calling convention; Error 49

我已经查看了以下(以及其他一些)资源,但无法解决我的问题:

你有什么建议吗?

非常感谢...

更新

这是头文件的代码:

#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);
    }
    //[...]
}

1 个答案:

答案 0 :(得分:0)

谢谢Hans Passant的帮助;

我已将项目属性更改为调用约定; 然后做了另一个dumpbin,我的函数的结果被命名为

_Add@16

然后只更改了VBA代码中的Alias ...