Hi,
how can one use a Safearray
to pass an array of custom types (a class containing only properties) from C++ to C#? Is using the VT_RECORD
type the right way to do it?
I am trying in the following way, but the reference to the array of classes gets to the managed code as a NULL.SafeArrayPutElement
returns an error when trying to fill the safearray
I have something like the following in the managed world:
[ComVisible(true)]
public interface IStatistics
{
double Mean { get; set; }
double StdDev { get; set; }
}
[Serializable]
[ComVisible(true)]
public class Statistics : IStatistics
{
public Mean { get; set; }
public double StdDev { get; set; }
}
Unmanaged world:
HRESULT hr = CoInitialize(NULL);
...
SAFEARRAY *pEquationsStatistics;
// common dimensions for all arrays
SAFEARRAYBOUND dimensions[1];
dimensions[0].cElements = 2;
dimensions[0].lLbound = 0;
pEquationsStatistics = SafeArrayCreate(VT_RECORD, 1, dimensions);
...
for (long i = 0; i < dimensions[0].cElements; i++)
{
long indices[1];
indices[0] = 0;
...
// Equation statistics
IStatisticsPtr pIStatistics(__uuidof(Statistics));
pIStatistics->PutMean(1.0); // so far so good
result = SafeArrayPutElement(pEquationsStatistics, indices, pIStatistics);
...
indices[0]++;
}
Please note that the I am able use the SafeArray
to pass other arrays of BSTR
with no problems between the two applications. So this is something peculiar to passing a structure.
Stefano