gpt4 book ai didi

c++ - 从 Excel VBA 使用 C++ dll 时出现 "Bad DLL calling convention"- 如何解决?

转载 作者:行者123 更新时间:2023-11-30 04:56:44 29 4
gpt4 key购买 nike

我需要创建一个 C++ dll,它具有一个函数,该函数返回要在 Excel (2010) VBA 代码中使用的字符串。

我已阅读以下帖子:Using C++ DLL in Excel VBA - 以及 Microsoft 关于 C++ dll 创建和 VBA 使用的教程(Walkthrough: Create and use your own Dynamic Link Library (C++)Access DLLs in Excel)并遵循该过程。

代码如下:

C++ dll(取自微软网页):

// MathLibrary.h - Contains declarations of math functions
#pragma once

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

// The Fibonacci recurrence relation describes a sequence F
// where F(n) is { n = 0, a
// { n = 1, b
// { n > 1, F(n-2) + F(n-1)
// for some initial integral values a and b.
// If the sequence is initialized F(0) = 1, F(1) = 1,
// then this relation produces the well-known Fibonacci
// sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

// Initialize a Fibonacci relation sequence
// such that F(0) = a, F(1) = b.
// This function must be called before any other function.
extern "C" MATHLIBRARY_API void fibonacci_init(
const unsigned int a, const unsigned int b);

// Produce the next value in the sequence.
// Returns true on success and updates current value and index;
// false on overflow, leaves current value and index unchanged.
extern "C" MATHLIBRARY_API bool fibonacci_next();

// Get the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned int fibonacci_current();

// Get the position of the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned fibonacci_index();

VBA 代码(我的,遵循 Microsoft 文档):

Public Declare Sub fibonacci_init Lib "C:\development\MathLibrary\Release\MathLibrary.dll" (ByVal a As Integer, ByVal a As Integer)
Public Declare Function fibonacci_next Lib "C:\development\MathLibrary\Release\MathLibrary.dll" () As Boolean
Public Declare Function fibonacci_current Lib "C:\development\MathLibrary\Release\MathLibrary.dll" () As Integer

Public Function run_dll()
Dim b As Integer

Call fibonacci_init(1, 1)
b = fibonacci_current()
End Function

当我在 VBA 中运行 run_dll 函数时,出现异常:Call fibonacci_init(1,1) 行出现“Bad DLL calling convention”。

这里有什么问题?我已将 C++ 函数声明为 extern "C" 所以我假设调用约定是固定的...

更新

我尝试过的更多东西...

  1. 我按照评论/答案中的提示从头开始创建了一个新的 dll:

试用.cpp:

const char* str = "abcdefg";

extern "C" __declspec(dllexport) int size()
{
return strlen(str);
}

extern "C" __declspec(dllexport) bool test(char* pReturn)
{
int nSize = strlen(str);
lstrcpynA(pReturn, str, nSize);

return true;
}

使用以下 VBA:

Public Declare Function size Lib "C:\development\MathLibrary\Release\Trial.dll" () As Long
Public Declare Function test Lib "C:\development\MathLibrary\Release\Trial.dll" (ByVal p As Long) As Boolean

(1) Public Function run_dll()
(2) Dim bb As Boolean
(3) Dim sz As Integer
(4) Dim s As String
(5) sz = size()
(6) s = Space(sz)
(7) bb = test(StrPtr(s))
(8) End Function

第 5 行工作正常 - sz 收到 7。但是第 7 行给出“Bad DLL calling convention”。

  1. 尝试使用 WINAPI 声明 C++ 函数,正如我在一篇文章中提到的那样,得到:Can't find DLL entry point size in C:\development\MathLibrary\Release\Trial.dll

  2. test VBA 声明更改为

公共(public)声明函数测试库 "C:\development\MathLibrary\Release\Trial.dll"(ByRef p As String) As Boolean

并调用 (line7) 作为 bb = test(s) - 导致 Excel 崩溃

  1. test VBA 声明更改为

Public Declare Function test Lib "C:\development\MathLibrary\Release\Trial.dll"(ByRef p As Long) As Boolean

并将(第 7 行)调用为 bb = test(StrPtr(s)) - 给出:“Bad DLL calling convention”

似乎没有任何效果。有人有此类设置的工作示例吗?

最佳答案

C++ Integer 的等价物是 VBA long。

VBA 中的 Integer 是 2 字节数据类型,VBA 中的 Long 是 4 字节数据类型。由于传递的字节数与预期的字节数不匹配,您会得到一个错误的 DLL 调用约定

请注意,VBA 不支持无符号类型,因此您的输出将被解释为带符号的 4 字节整数(长整型)。

关于c++ - 从 Excel VBA 使用 C++ dll 时出现 "Bad DLL calling convention"- 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52361982/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com