简体   繁体   English

通过 Excel / VBA 调用的 DLL 中的 C++ 函数在传递双参数时生成异常

[英]C++ function in DLL called through Excel / VBA generates exception when passing double argument

I'm trying to use a C / C++ static function in Excel / VBA through a DLL.我正在尝试通过 DLL 在 Excel/VBA 中使用 C/C++ 静态函数。

I'm getting an exception when debugging in VS17, and I suspect it's an issue with the way the argument is passed (it's a double)在 VS17 中调试时出现异常,我怀疑这是参数传递方式的问题(它是双重的)

Exception thrown at 0x00007FFA28BBA14F (kernel32.dll) in EXCEL.EXE: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.在 EXCEL.EXE 中的 0x00007FFA28BBA14F (kernel32.dll) 处引发异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF。

here's my C code:这是我的 C 代码:

test.h测试.h

extern "C" __declspec(dllexport) double get_sum_cpp(double x);

test.cpp测试.cpp

double WINAPI get_sum_cpp(double x)
{
   double res = x + x;
   return res;
}

declaration in VBA: VBA中的声明:

Declare PtrSafe Function get_sum_cpp Lib "C:\Users\bbi\source\repos\Test\x64\Debug\Test.dll" (ByVal my_var As Double) As Double

test code in VBA: VBA中的测试代码:

Sub testSum()

    Dim A As Double
    Dim Asum As Double

    A = 5

    Asum = get_sum_cpp(A)

end sub

I'm running 64 bits excel, and the dll is compiled in debug mode 64 bits.我正在运行 64 位 excel,并且 dll 以 64 位调试模式编译。

I have many more issues with the overall development (for example any function with more than one argument will crash excel entirely), but this is the smallest "unit test" I could get too.我在整体开发方面还有很多问题(例如,任何具有多个参数的函数都会使 excel 完全崩溃),但这也是我能得到的最小的“单元测试”。

I feel it's an issue with the way the VBA double argument is passed to the DLL function, (stack misalignment ?), but I can't figure out how to set it right.我觉得将 VBA 双参数传递给 DLL 函数的方式存在问题(堆栈未对齐?),但我不知道如何正确设置它。 When debugging in VS17, the exception occurs before I reach the line "double res = x + x", so I suspect it's happening at the function declaration, so when the double argument is passed - so an issue with casting - again maybe stack misalignment ?在 VS17 中调试时,异常发生在我到达“double res = x + x”行之前,所以我怀疑它发生在函数声明中,所以当传递 double 参数时 - 所以是转换问题 - 再次可能是堆栈未对齐?

my exports seem OK - checked with dumpbin / EXPORTS.我的出口似乎没问题 - 使用 dumpbin / EXPORTS 检查。 The function is found and eventually returns.该函数被找到并最终返回。

Any idea ?任何想法 ?

I have successfully ran your code exactly without any problem on my computer.我已经成功地在我的计算机上完全运行了您的代码,没有任何问题。

My VBA script:我的 VBA 脚本:

Private Declare PtrSafe Function get_sum_cpp Lib "D:\Codes\VC\TestVBA\x64\Debug\Dll2.dll" (ByVal my_var As Double) As Double

Sub testSum()

    Dim A As Double
    Dim Asum As Double

    A = 5

    Asum = get_sum_cpp(A)

    MsgBox "Val is: " & Asum
End Sub

and your C++ code.和你的 C++ 代码。 I have tested both with your code and using __declspec(dllexport) and definition file.我已经使用您的代码和使用__declspec(dllexport)和定义文件进行了测试。 Both works.两者都有效。 I also have tested with/without DLL runtime library.我还测试了有/没有 DLL 运行时库。 Still works.仍然有效。

I have used VS2019, Excel 2019. If they are not same version, make sure you are using /MT not /MTd for non-DLL runtime libraries though.我使用过 VS2019、Excel 2019。如果它们的版本不同,请确保您使用的是/MT而不是/MTd用于非 DLL 运行时库。

I had the same exceptions and the same question.我有同样的例外和同样的问题。 I came to conclusion that it's normal behaivior for Excel causing no issues or leaks (you can ignore these messages in debug window),happening when parameters are passed as value (byval).我得出的结论是 Excel 的正常行为不会导致问题或泄漏(您可以在调试窗口中忽略这些消息),当参数作为值(byval)传递时发生。 It's a bit strange, but it seems Excel (or Kernel32) tries to test / access the memory address taken from variable value.这有点奇怪,但似乎 Excel(或 Kernel32)试图测试/访问从变量值获取的内存地址。 In my case I passed Boolean byval and Excel tried to access FFFFFFFF address for True value and 00000000 for False...在我的情况下,我传递了布尔值,Excel 尝试访问 FFFFFFFF 地址以获得 True 值和 00000000 获得 False ......

This exception disappears when parameter is passed by reference (which is default option for VBA)...当通过引用传递参数时,此异常消失(这是 VBA 的默认选项)...

I hope it helps.我希望它有所帮助。

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

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