【发布时间】:2019-02-21 00:55:16
【问题描述】:
我需要创建一个 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) 行上的“错误的 DLL 调用约定”。
这里有什么问题?我已经将 C++ 函数声明为extern "C",所以我假设调用约定是固定的......
更新
我尝试过的更多东西......
- 我按照 cmets/answers 中的提示从头开始创建了一个新的 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 行给出“错误的 DLL 调用约定”。
尝试使用
WINAPI声明 C++ 函数,正如我在一篇文章中提到的那样,给出:Can't find DLL entry point size in C:\development\MathLibrary\Release\Trial.dll。将
testVBA 声明更改为
Public Declare Function test Lib "C:\development\MathLibrary\Release\Trial.dll" (ByRef p As String) As Boolean
并以bb = test(s) 调用 (line7) - 导致 Excel 崩溃
- 将
testVBA 声明更改为
Public Declare Function test Lib "C:\development\MathLibrary\Release\Trial.dll" (ByRef p As Long) As Boolean
并以bb = test(StrPtr(s)) 的形式调用 (line7) - 给出:“错误的 DLL 调用约定”
似乎没有任何工作。有人有这种设置的工作示例吗?
【问题讨论】:
-
@AlanBirtles:这篇文章谈到了 MSAccess VBA - 我没有关注那里的细节......我没有“VBA 编译器模块”
-
最佳答案是关于 excel 的,其他几个也是如此
-
@AlanBirtles:它谈到了重新编译 Excel 加载项......而不是 C++ dll
-
是的,对您的 excel 代码进行更改以使其重新编译,因为它有 24 个赞成票,并带有与您相同的错误消息,我想它很有可能会起作用