我试图使用Excel VBA的能力来访问和使用DLL文件中的函数。
例:
Private Declare Function funcName Lib _
"<filePath\File.dll>" _
(ByRef a As Double, ByRef b As Double) As Double
按照Mircosoft's tutorial关于如何创建DLL文件的指示,当我尝试构建项目时,对于声明的3个函数,导致3个警告(C4273):
'MathLibrary::Functions::Add': inconsistent dll linkage,
'MathLibrary::Functions::Multiply': inconsistent dll linkage,
'MathLibrary::Functions::AddMultiply': inconsistent dll linkage
当Excel中的VBA尝试从本教程访问创建的.dll文件时,它会产生运行时错误(453):'无法找到DLL入口点添加到“path \ file.dll”。
对于C \ C ++语言,我是新手。我花了6个多小时:
然而我从一个解决方案中感受到了更多。
我在64位Windows上运行32位Excel。
任何帮助将非常感激 :)
编辑
代码文件(根据要求):
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模块
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
我将在解决方案中发布此内容,因为它不适合评论。
“不一致的dll链接”警告:我现在从问题中复制了您的确切代码(可能会在将来更改),并将其放入新创建的VStudio 2015项目中:
如果我定义MATHLIBRARY_EXPORTS
,项目编译没有警告:
在构建你的时候,我唯一可以想象你仍然得到警告,是因为你正在为错误的配置定义宏。 示例:您正在为Debug - x86构建项目,但是您为Release - x86(或Debug - x64)定义了宏。 您必须检查(最好选择所有平台和所有配置,并且只定义宏一次)构建配置和设置配置匹配,如下图所示:
#define MATHLIBRARY_EXPORTS
但无论如何,这个警告是良性的,.dll仍然是构建的,并且符号被导出。
更进一步,在VBA模块中声明函数名称Add(plain)。基于错误消息:
找不到DLL入口点在“path \ file.dll”中添加
正如我在其中一条评论中指出的那样,由于#include "MathLibrary.h"
(C ++名称修改),我认为Excel无法导入C ++样式导出。当它搜索Add时,您的.dll会导出以下符号,如下面的(Dependency Walker)图像所示(您可以使用突出显示的按钮进行播放,看看Dependency Walker如何解析这些名称):
你应该从Excel导入的那些(乱码)名称,但我怀疑这是可能的。作为一种解决方法,您可以:
[MSDN]: Decorated Names包含所有细节(注意:[SO]: Linker error when calling a C function from C++ code in different VS2010 project (@CristiFati's answer))。