C#: Reading SafeArray wrapped in Variant from C++ COM

550 views Asked by At

I'm struggling with using a COM-Object in C#. I referenced the dll, and I'm able to call all the functions that return basic data types. But one function that returns a larger data object always throws a SEHException. The function is defined as follows:

GetData (long bTime, long lFirstIndex, long lLastIndex, VARIANT *pBuffer, long *nValuesRead)

pBuffer is a safearray of doubles.

I tried some approaches like:

double[] ar = new double[nSample];
object buffer = (object) ar;

or

Array ar = Array.CreateInstance(typeof(double), nSample);
object buffer = (object) ar;

as well as

object buffer = null;

All of those compile, but also throw the exception.

m4.GetData(0, idxFirst, idxLast, out buffer, out nSampleRead); 

However, I have a working C++-Example, so the COM-Lib should be able to run properly.

CComSafeArray<double> *pData;
CComSafeArrayBound bound;
bound.SetCount(nValues); // nValues has been claculated above
bound.SetLowerBound(0);
pData = new CComSafeArray<double>(&bound,1); 
// Wrap safearrays by a VARIANT
VARIANT vData;
vData.parray = *pData->GetSafeArrayPtr();
vData.vt = VT_ARRAY;
pData->Detach();
// Get the data form the signal
m4.GetData(FALSE, idx1, idx2, &vData, &n);

I'm just unable to translate this to C#. So I would highly appreciate, if somebody could give me a hint, how to do this.

0

There are 0 answers