如何将数组从C ++ DLL传递到VBA? [英] How to pass array to VBA from a C++ DLL?
问题描述
我有一个C ++ DLL;我试图在VBA中调用导出的函数,该函数将返回一个数组.为了避免必须将SAFEARRAY作为返回类型来处理,我选择了通过引用将数组传递到函数中,并让函数修改此数组的值.但是,即使我使用 ByRef
传入数组,VBA实现也不会返回修改后的值.
标题:
命名空间myProject{类FileOperator{上市:静态__declspec(dllexport)void __stdcall testArray(long * docArray);};}
实施:
#include"stdafx.h"#include"myProject.h"使用命名空间std;命名空间myProject{无效__stdcall FileOperator :: testArray(long * docArray){docArray [0] = 303;docArray [1] = 909;}}
控制台应用程序测试:
#include"stdafx.h"#include< iostream>#include"../myProject/myProject.h"使用命名空间std;int main(){long docArray [2] = {0};myProject :: FileOperator :: testArray(docArray);cout<<docArray [0]<<"\ n"<<docArray [1];}
VBA测试:
私有声明子testArray库"C:\ pathToDLL \ myProject.dll" _别名?testArray @ FileOperator @ myProject @@ SGXPAJ @ Z" _(ByRef docArray()一样长)公开子测试()昏暗docArray(1)一样长docArray(0)= 0docArray(1)= 0testArray docArray对于每个x docArray调试打印x下一个结束子
C ++控制台应用程序输出:
303909
VBA应用输出:
00
为什么 testArray()
函数退出后为什么不保存数组更改? ByRef
关键字不应该实现这一点吗?
ByRef docArray()
在VB(A)内部调用时具有预期的意义-它允许您替换存储在变量与另一个数组一起使用(以及在不替换整个数组的情况下更改传递的数组的元素).
对于已声明的函数,请使用 ByRef Long DocArray
,然后将 docArray(LBound(docArray))
用作参数(指向第一个元素的指针)./p>
I have a C++ DLL; I am trying to call an exported function in VBA that will return an array. To avoid having to deal with SAFEARRAY as a return type, I've chosen to pass an array into the function by reference and have the function modify this array's values. But the VBA implementation doesn't return the modified values, even though I'm using ByRef
to pass in the array.
Header:
namespace myProject
{
class FileOperator
{
public:
static __declspec(dllexport) void __stdcall testArray(long* docArray);
};
}
Implementation:
#include "stdafx.h"
#include "myProject.h"
using namespace std;
namespace myProject
{
void __stdcall FileOperator::testArray(long* docArray)
{
docArray[0]=303;
docArray[1]=909;
}
}
Console app test:
#include "stdafx.h"
#include <iostream>
#include "../myProject/myProject.h"
using namespace std;
int main()
{
long docArray[2]={0};
myProject::FileOperator::testArray(docArray);
cout << docArray[0] << "\n" << docArray[1];
}
VBA test:
Private Declare Sub testArray Lib "C:\pathToDLL\myProject.dll" _
Alias "?testArray@FileOperator@myProject@@SGXPAJ@Z" _
(ByRef docArray() As Long)
Public Sub test()
Dim docArray(1) As Long
docArray(0) = 0
docArray(1) = 0
testArray docArray
For Each x In docArray
Debug.Print x
Next
End Sub
The C++ console app outputs:
303
909
The VBA app outputs:
0
0
Why aren't the array changes being saved once the testArray()
function exits? Shouldn't the ByRef
keyword make this possible?
ByRef docArray()
makes the expected sense when calling inside VB(A) - it allows you to replace the array stored in the variable with another array (as well as change the elements of the passed array without replacing the whole array).
For declared functions, use ByRef docArray As Long
, and then pass docArray(LBound(docArray))
as the argument (the pointer to the first element).
这篇关于如何将数组从C ++ DLL传递到VBA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!