在excel中使用C ++ 2010 dll [英] using C++ 2010 dll in excel
问题描述
标题:
// DateNTime.h
//避免需要.Def文件
#ifdef SwapFunDLL_EXPORTS
#define SwapFunDLL_API __declspec(dllexport)
#else
#define SwapFunDLL_API __declspec(dllimport)
#endif
命名空间DateNTime
{
类SwapFunDLL_API日期
{
double x,y;
public:
double datediff(double,double);
};
double Date :: datediff(double x,double y)
{
return x * y;
}
}
源文件:
#includeDateNTime.h
命名空间DateNTime
{
double returndates(double a,double b)
{
日期日期;
return Date.datediff(a,b);
}
}
Excel宏:
声明函数SwapFunDLL _
LibC:\Users\MIKE\Desktop\c ++ tial programs\SwapFunDLL\Debug\SwapFunDLL.dll _
(ByRef x As Double,ByRef y As Double)As Double
您的导入声明是错误的。两件事:语义和声明类型。如果你使用指针而不是Excel,你会说 ByRef
,但是如果你通过值传递使用,你会说 ByVal
。
将此函数声明为:
声明函数returndates LibC:\Users\MIKE\Desktop\\ \\ c ++ tial programs\SwapFunDLL\Debug\SwapFunDLL.dll_(ByVal x As Double,ByVal y As Double)As Double
使您的函数返回您正在导出的类的成员,静态成员。
也使你的函数静态,在任何情况下,你可以声明这个导出在.def文件(只是简单地:returdates这一切)。毕竟,这必须工作,除非你的C ++代码是OK的。
也在这里:
double returndates(double a,double b)
{
Date日期; //< - 更改它,即:日期d; d.datediff();是否编译?
return Date.datediff(a,b);
}
你可以更改代码,只是为了测试你是否正确地做这些测试信息表单:
double returndates(double a,double b)
{
return 117;
}
并在Excel中尝试。
Excel需要 __ stdcall
,所以在你的类中做一个函数声明,如下所示:
static double __stdcall returndates(double a,double b);
然后在.cpp文件中将其定义为:
SwapFunDLL_API double __stdcall DateNTime :: Date :: returndtes(double a,double b){
return 117;
}
所有这些更改都应该可以。
Ive built a simple dll in c++ using Visual C++ 2010 and am trying to use it in excel. The project name is "SwapFunDLL", the source files name is "swapmain" and the name of the header file containing the class and its function is "DateNTime". Ive attached them below. The code takes in two values and then uses the class function to multiply them and then returns the product (i realize the class and function arent necessary, im just doing this to learn). The function compiles with not errors however when the function is used in excel i get a value error. Could someone please help me see what im doing wrong thanks.
header:
// DateNTime.h
//Avoid need of .Def file
#ifdef SwapFunDLL_EXPORTS
#define SwapFunDLL_API __declspec(dllexport)
#else
#define SwapFunDLL_API __declspec(dllimport)
#endif
namespace DateNTime
{
class SwapFunDLL_API Date
{
double x,y;
public:
double datediff(double,double);
};
double Date::datediff(double x, double y)
{
return x*y;
}
}
Source File:
#include "DateNTime.h"
namespace DateNTime
{
double returndates(double a, double b)
{
Date Date;
return Date.datediff(a,b);
}
}
Excel Macro:
Declare Function SwapFunDLL _
Lib "C:\Users\MIKE\Desktop\c++ tial programs\SwapFunDLL\Debug\SwapFunDLL.dll" _
(ByRef x As Double, ByRef y As Double) As Double
your import declaration is wrong. Two things: the semantics and declaration of types. If you use pointers than in Excel you say ByRef
, but if you use by value passing you say ByVal
.
Declare this function as:
Declare Function returndates Lib "C:\Users\MIKE\Desktop\c++ tial programs\SwapFunDLL\Debug\SwapFunDLL.dll" _ (ByVal x As Double, ByVal y As Double) As Double
make your function returndates member of your class that is being exported, static member. also make your function static and in any case you can declare this export in .def file (just simply: "returdates" it's all). After all of it this has to work unless your C++ code is OK. also here:
double returndates(double a, double b)
{
Date Date; //<- change it, i.e: Date d; d.datediff(); does it compile?
return Date.datediff(a,b);
}
you can change the code just to test if you do everything correctly to such "test info" form:
double returndates(double a, double b)
{
return 117;
}
and try it in Excel.
Excel needs __stdcall
, so make a functions declarations inside your class like this:
static double __stdcall returndates(double a, double b);
then define it in .cpp file as:
SwapFunDLL_API double __stdcall DateNTime::Date::returndtes(double a, double b){
return 117;
}
after all these changes should be OK.
这篇关于在excel中使用C ++ 2010 dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!