我需要创建一个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(摘自Microsoft网页):
// 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"
,所以我假设调用约定是固定的...
更新
我尝试过的更多事情...
Trial.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
。
将test
VBA声明更改为
Public Declare Function test Lib "C:\development\MathLibrary\Release\Trial.dll" (ByRef p As String) As Boolean
并以bb = test(s)
调用(line7)-导致Excel崩溃
test
VBA声明更改为 Public Declare Function test Lib "C:\development\MathLibrary\Release\Trial.dll" (ByRef p As Long) As Boolean
并以bb = test(StrPtr(s))
调用(line7)-给出:“错误的DLL调用约定”
似乎没有任何作用。有人为这种设置提供有效的示例吗?
答案 0 :(得分:1)
相当于C ++整数的VBA长。
VBA中的Integer
是2字节的数据类型,VBA中的Long
是4字节的数据类型。由于传递的字节数与预期的字节数不匹配,因此您会得到错误的DLL调用约定
请注意,VBA不支持无符号类型,因此您的输出将被解释为带符号的4字节整数(长整数)。
答案 1 :(得分:0)
您将需要使其成为COM库。在Windows注册表中注册它们之后,即可将它们导入VBA代码中。