c++ - 链接 C++ 的 Excel 错误 : "A value used in the formula is of the wrong data type"

标签 c++ excel dll x86-64 32bit-64bit

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

源.cpp:

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

定义.def:

LIBRARY "square" 
EXPORTS
square

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

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。

错误提示:“公式中使用的值的数据类型错误”。

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

我的环境是

  • Windows 7 64 位
  • Visual Studio 2016 社区(所以它是 32 位)
  • Excel 2016 64 位

回复评论,如果我把引用传递改成值传递,还是报错。

我将 Source.cpp 更改为

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

并将 module1 更改为

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但除非您有没有 64 位版本的遗留 DLL,否则您应该编译为 64 位项目。和 64-bit Office doesn't support 32-bit plugin at all所以你需要一个 64 位 DLL。

关于c++ - 链接 C++ 的 Excel 错误 : "A value used in the formula is of the wrong data type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39113813/

相关文章:

excel - Excel 中比较两个单元格并返回较小者的函数

c - 执行需要 ext lib 的 C 脚本

ruby-on-rails - 尝试为 Windows XP 使用 Heroku 时找不到 msvcrt-ruby18.dll

c++ - 模幂(模算术中的幂)

c++将dll注入(inject)cmd.exe监控命令

EXCEL VBA - 遍历列中的单元格,如果不为空,则将单元格值打印到另一列

c# - 无法从 C# 调用用 C++ 编写的 dll 函数

c++ - 拆分类 C++

c++ - 将 double 转换为 unsigned long C++

vba - 需要更正代码以将工作簿合并到单个工作簿中 - 消除保存提示