Excel 链接 C++ 时出错:"A value used in the formula is of the wrong data type"

Excel error linking C++: "A value used in the formula is of the wrong data type"

我写了一个简单的 C++ 程序

Source.cpp:

double __stdcall square(double& x)
{
    return x*x;
}

Define.def:

LIBRARY "square" 
EXPORTS
square

然后在我的 .xlsm 文件模块中,添加模块 1 导入:

Declare PtrSafe Function square _
Lib "C:\Users\user\Documents\AthosCode\Marek Kolman Square\Square\Debug\square.dll" _
(ByRef x As Double) As Double

并在 Sheet1 中,将 10 放入 A1,将 =square(A1) 放入 B1。

错误说:"A value used in the formula is of the wrong data type"。

有什么问题,我该如何解决?

我的环境是


回复评论,如果我把引用传递改成值传递,还是一样的错误

我把Source.cpp改成了

double __stdcall square(double x)
{
    return x*x;
}

并将模块 1 更改为

Declare PtrSafe Function square _
Lib "C:\Users\user\Documents\AthosCode\Marek Kolman Square\Square\Debug\square.dll" _
(ByVal x As Double) As Double

同样的错误。

由于架构不同,无法load a 32-bit DLL in a 64-bit process,反之亦然

可以access 32-bit DLLs from 64-bit code but unless you have legacy DLLs without a 64-bit version, you should compile as a 64-bit project. And 64-bit Office doesn't support 32-bit plugin at all,所以您需要一个 64 位 DLL。