我創建從https://msdn.microsoft.com/de-de/library/ms235636.aspx的MathFuncsDll.dll
沒有問題。 我可以在不同的C++項目中使用數學函數也沒有問題。在Excel中使用C++ DLL VBA
現在我想在Excel VBA中使用這些功能。我想:
Public Declare Function DLL_Import_Add Lib "C:\Users\User\Desktop\MathFuncsDll.dll" Alias "Add" (ByVal a As Double, ByVal b As Double) As Double
Sub test()
MsgBox DLL_Import_Add(2.1, 3.3)
End Sub
但是我收到一個Run-time error '453'
的DLL入口點無法找到。
我怎樣才能解決這個問題?
MathFuncsDll.h:
// MathFuncsDll.h
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
namespace MathFuncs
{
// This class is exported from the MathFuncsDll.dll
class MyMathFuncs
{
public:
// Returns a + b
static MATHFUNCSDLL_API double Add(double a, double b);
// Returns a - b
static MATHFUNCSDLL_API double Subtract(double a, double b);
// Returns a * b
static MATHFUNCSDLL_API double Multiply(double a, double b);
// Returns a/b
// Throws const std::invalid_argument& if b is 0
static MATHFUNCSDLL_API double Divide(double a, double b);
};
}
MathFuncsDll.cpp:
// MathFuncsDll.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "MathFuncsDll.h"
#include <stdexcept>
using namespace std;
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}
double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}
double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}
double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return a/b;
}
}
這就是它!我試過'Add @ MyMathFuncs @ MathFuncs @@ SANNN @ Z',但我在開頭忘了'?'。我也在C中做了這個,並且用DEF文件在DLL中定義了函數的名稱,並且在那裏寫了'name_in_dll = name_in_c'。應該在你的答案中翻轉? – TimFinnegan
@TimFinnegan哎呀,謝謝 –