I created a C++ DLL which take an empty array as VARIANT. I will run some sql query inside DLL and store output in empty array. When I tried this, excel crashes.
My VBA call :
Dim outArray(17) As Variant
bo = GetData_V(dbFilePath, id, inArray, outArray, counter)
and function is defined as
Declare Function GetData_V& Lib "xyz.dll" (ByVal path As String, ByRef inputArr() As String, ByRef output As Variant, ByRef id As Integer)
C++ Implementation :
CComSafeArray<VARIANT> out_sa(nCount);
HRESULT hr;
for (LONG i = lowerBound; i <= upperBound; i++)
{
CComVariant variant = CComVariant(outputCustom[i]);
hr = out_sa.SetAt(i,variant);
if (FAILED(hr))
{
return false;
}
}
CComVariant(out_sa).Detach(outputArray);
Where outputCustom is defined as
outputCustom = new char*[nCount+1];
and has string values.
When I try to run it, Excel crashes. I don't know how to return outputArray back to VBA.
Long(i.e. the&on the end ofGetData_V&) - are you doing so? Also, your declaration is defining the parameters to beByVal path As String, ByVal path As String, ByRef inputArr() As String, ByRef output As Variant, ByRef id As Integer(withpathrepeated?) but your call (i.e.dbFilePath, id, inArray, outArray, counter) doesn't seem to match that.