Dll 通过 excel 从 c++ 代码访问
Dll access by excel from c++ code
我正在尝试使用Visual Studio 2015从C++代码创建DLL。
我有一个 DLL_Tutorial.h 文件:
#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
extern "C"
{
DECLDIR int Add( int a, int b );
}
#endif
\
\ 然后我创建了一个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:UsershasnaDesktopProjet VBA-C++projet5Debugprojet5.dll" (byval a As integer,byval b As integer) As integer
然后在 excel 工作表中,我输入 2 个值(例如 6 和 4),我调用 add 函数,但它给了我:#VALEUR!
问题出在哪里?你能帮我解决这个问题吗
谢谢
在 DLL 头文件中,您似乎没有在导出的 API 前面加上 __declspec(dllexport) 前缀。通常定义它,以便 API 可以在构建 DLL 时使用 __declspec(dllexport),在外部项目中使用标头时可以使用 __declspec(dllimport)。最好的办法是使用 VS 2015 项目模板创建 DLL,它包含正确的标头和导出定义,因此您只需要专注于编写 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 Via C/C++ 一书也是关于编写 DLL 的非常好的资源。现在关于 VB 导入,我不确定,因为我不熟悉通过 VB 导入 API。
相关文章:
- 为什么示例代码访问IUnknown中已删除的内存
- 如何使用 gcc 内联汇编器代码访问成员变量
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 使用MySQL C 连接器从C 代码访问MEMSQL
- 无法从C 代码访问C共享库方法
- 是否可以将匿名 lambda 函数绑定到对象以允许 lambda 中的代码访问对象的成员?
- 从本地代码访问apk中的压缩文件(从zip中读取zip)
- Unity 3D:从 C/C++ 代码访问 Unity 3D API
- Xcode c++ 无法通过代码访问文件
- 使用终止进程终止"mstsc.exe"进程时出现错误代码(5)访问被拒绝
- QT 5.4,无法从代码访问资源
- 为什么此代码访问向量超出其范围
- 使本机代码访问java方法和数据成员
- Dll 通过 excel 从 c++ 代码访问
- 通过静态变量从本机代码访问有状态托管对象
- 从标准C++代码访问iPhone的沙盒文档文件夹
- 从代码访问iOS设置
- <canvas> 从 NaCl 代码访问 HTML5
- 从 QT 中的C++代码访问 QML 元素
- 从非托管C++代码访问 WPF