通过excel从c ++代码访问

我正在尝试使用Visual Studio 2015从C ++代码创build一个DLL。

我有一个DLL_Tutorial.h文件:

#ifndef _DLL_TUTORIAL_H_ #define _DLL_TUTORIAL_H_ #include <iostream> extern "C" { DECLDIR int Add( int a, int b ); } #endif \\ 

\然后我创build了一个DLL_Tutorial.cpp

 #include <iostream> #include "DLL_Tutorial.h" #define DLL_EXPORT extern "C" { __declspec(dllexport) int Add(int a, int b) { return(a + b); } } 

我有一个Dll文件

我想在VBA中调用我的函数,并将其应用于Excel表格

所以在VBA中我做到了:

 Public Declare Function Add _ Lib "C:\Users\hasna\Desktop\Projet VBA-C++\projet5\Debug\projet5.dll" (byval a As integer,byval b As integer) As integer 

然后在Excel工作表中,我input2个值(例如6和4)我打电话添加function,但它给了我:#VALEUR!

哪里有问题 ? 你能帮我解决这个问题吗?

谢谢

在您的DLL头文件中,似乎没有用__declspec(dllexport)为导出的API添加前缀。 通常将其定义为使API在构buildDLL时可以使用__declspec(dllexport),而在外部项目中使用该头时可以使用__declspec(dllimport)。 最好的方法是使用VS 2015项目模板创buildDLL,它包含正确的头文件和导出定义,因此您只需要专注于编写API。 以下是VS 2015项目的一个实例:

示例* .h:

 #pragma once // The following ifdef block is the standard way of creating macros which make exporting // from a DLL simpler. All files within this DLL are compiled with the DLLAPI_EXPORTS // symbol defined on the command line. This symbol should not be defined on any project // that uses this DLL. This way any other project whose source files include this file see // DLLAPI_API functions as being imported from a DLL, whereas this DLL sees symbols // defined with this macro as being exported. #ifdef DLLAPI_EXPORTS #define DLLAPI_API __declspec(dllexport) #else #define DLLAPI_API __declspec(dllimport) #endif // This is an example of an exported function. DLLAPI_API int fnDLLAPI(void); 

示例* .cpp:

 // DLLAPI.cpp : Defines the exported functions for the DLL application. // #include "DLLAPI.h" // This is an example of an exported function. DLLAPI_API int fnDLLAPI(void) { return 42; } 

Windows通过C / C + +书籍也是一个非常好的写入DLL资源。 现在关于VB导入,我不知道,因为我不熟悉通过VB导入API。