简体   繁体   English

从Excel VBA / Python调用时C ++ dll返回不同的结果

[英]C++ dll returns different results when called from Excel VBA/Python

Suppose my VC100 Dll project has the following: 假设我的VC100 Dll项目具有以下内容:

Foo.h oo

struct Foo {
    std::vector v;
    ...

    Foo(const double* x, const long& n, ...)
        : v(x, x + n),
          ... {; }

    double bar() {...} // Does something to this->v, returns a double
};

myproject.cpp myproject.cpp

#include "Foo.h"    

double fun(double* x, long n, ...) {

    Foo f(x, n, ...);

    const double r(f.bar());

    std::memcpy(x, f.v.data(), n * sizeof(double));

    return r;
}

inline double __stdcall xfun(double* x, long n, ...) {
    return fun(x, n, ...);
}

def.def def.def

library "myproject"
EXPORTS
fun
xfun

mywb.xlsm (VBA) mywb.xlsm(VBA)

Declare Function xl_fun Lib "myproject.dll" Alias "xfun" _
    (ByRef x As Double, ByVal n As Integer, ...) As Double

xl_fun() is then called from Excel, and fun() is called from Python 2.7` after. 然后从Excel调用xl_fun() ,然后从Python 2.7`调用fun()

My question is, xl_fun() will return with its first argument updated to the contents of Foo.v , just as Python will. 我的问题是, xl_fun()将返回其第一个参数更新为Foo.v的内容,就像Python一样。

However, when fun() is called from Python 2.7 , the first argument update, and the return value is different. 但是,从Python 2.7调用fun()时,第一个参数更新,并且返回值不同。

Without going into too much detail, this update and return are better numbers than the Excel ones. 无需赘述,此更新和返回的数字比Excel的数字更好

All exceptions are handled by the time f.bar() returns. f.bar()返回时将处理所有异常。

I've stepped through and confirmed the inputs passed to Foo 's constructor are identical in both cases. 我已逐步完成并确认传递给Foo的构造函数的输入在两种情况下都是相同的。 I've also confirmed Foo.v 's initial state is identical in both cases. 我还确认了Foo.v的初始状态在两种情况下都是相同的。 How can the same DLL produce different results given the same inputs? 给定相同的输入,相同的DLL如何产生不同的结果?

Any help is much appreciated. 任何帮助深表感谢。

Excel sets FPU flags for extended precision (80-bits) while most other run-time environments don't. Excel设置FPU标志以提高精度(80位),而其他大多数运行时环境则没有。 This may play a role in the differences you observe. 这可能在您观察到的差异中起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM