So I am working on an Excel Project that is going to load a C++ dll using VBA. What I'd like to do is to be able to pass an Excel range with no specific type (data can be numerical or categorical) to the C++ dll (The best way I can describe my Excel range is of the type variant
).
So the steps probably involve:
I am thinking of using excel variant and C++ variant. But it's not clear for me how to use the C++ variant as I couldn't find any good documentations on it.
Another suggestion I received was to ues COM programming.
My Questions:
UPDATE:
Provided you only want to pass data to the dll (and not pointers to actual Excel objects such as Range
), you have two fundamental options:
You have huge data sets and want to avoid copying as much as possible.
In this case you might want to pass that same Variant
array you get by calling Range.Value
. In order to do that, you will have to write a little TLB to reference from VB, in which you would describe your exported C++ function as expecting a SAFEARRAY(VARIANT)*
. This is because the Declare
operator will not let you actually pass a SAFEARRAY*.
The function will look like this:
LONG __stdcall ReturnArrLowerBound(SAFEARRAY** ppArr)
{
if (ppArr == NULL) return -1;
SAFEARRAY* pArr = (*ppArr);
LONG res = 0;
SafeArrayGetLBound(pArr, 1, &res);
return res;
}
And the TLB descripion will look like that:
[
uuid(A686B138-D8CE-462e-AEF2-75DA4DBF1C75)
]
library foo
{
[
dllname("TestSafearray.dll")
]
module vb
{
[entry("ReturnArrLowerBound")]
LONG __stdcall ReturnArrLowerBound(SAFEARRAY(VARIANT)* ppArr);
}
}
And your C++ project will obviously include a def file:
LIBRARY "TestSafearray"
EXPORTS
ReturnArrLowerBound
Your data sets are reasonably sized and you don't mind a little bit of copying.
Then make your C++ function to accept a mere int[]
and Declare it in VB as accepting arr() as Long
. On VB side, allocate an array on Long
s and copy the elements into it from the Range.Value
array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With