【问题标题】:Excel error linking C++: "A value used in the formula is of the wrong data type"链接 C++ 的 Excel 错误:“公式中使用的值的数据类型错误”
【发布时间】:2016-08-24 03:13:07
【问题描述】:

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

来源.cpp:

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

定义.def:

LIBRARY "square" 
EXPORTS
square

然后在我的 .xlsm 文件 Modules 中,添加 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

同样的错误。

【问题讨论】:

  • 为什么要通过引用传递?按值传递就足够了。
  • @songyuanyao 同样的错误。请查看更新的帖子。
  • 您是否将 A1 单元格格式设置为双精度?也许 excel 将其解释为字符串。
  • no 64-bit VS。重要的是您的 dll 是否已在 32 位或 64 位模式下编译,而不是 VS 版本
  • @Ari0nhh 不,这不是问题。如果在 A1 中输入“10.5”,在 B1 中输入“=A1*A1”,就可以了。

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


【解决方案1】:

由于架构的差异,无法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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    相关资源
    最近更新 更多