0

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.

juju
  • 1
  • It may depend on how the C++ side is defined and coded. Also, how is GetData defined in C# exactly?. – Simon Mourier Sep 04 '17 at 07:49
  • I don't think, I have access to more information in C++, there's basicly just the dll. Concerning the C#-definition, this is what the object catalogue shows:void GetData(int bTime, int lFirstIndex, int lLastIndex, out object pBuffer, out int nValuesRead) – juju Sep 04 '17 at 07:53
  • If you don't need help, don't ask questions – Simon Mourier Sep 04 '17 at 08:07
  • I do need help, because as I said, I get an exception and I don't know why and how to avoid that. – juju Sep 04 '17 at 08:22
  • Just to clarify that: the method "GetData" is part of the COM-Object. Since I don't have the plain code of the C++-COM-Lib I can't deliver more details. At least I don't know how. That's why I pasted the function call in c++, so somebody might be able to conclude a severe difference between that and my C# approach. – juju Sep 04 '17 at 12:53
  • Could you just run the debugger and visualize the variant pBuffer to infer its type from the watch window ? – Malick Sep 04 '17 at 13:06
  • In C++ the vData (last line in my post) looks like this: vData 0x0489bd18 safearray of R8, [rank]=1 ---> type: tagVARIANT consisting of: safearray 0x0489bd18 safearray of R8, [rank]=1 ---> type: tagSAFEARRAY * vt ---> value:8192, type: unsigned short – juju Sep 04 '17 at 13:37
  • If I run the code in C#, the arguments still have the values I handed over, when the exception is thrown. So I can't see, what the function would have returned. – juju Sep 04 '17 at 13:49

0 Answers0