Passing Saferyray custom types from C ++ to C #

how can one use Safearraypass an array of user types (a class containing only properties) from c ++ to c #? Does type use the VT_RECORDcorrect way to do this?

I try as follows, but it returns an error when trying to populate a safearray the reference to the array of classes gets the managed code as NULL. SafeArrayPutElement

In a managed world, I have something like the following:

[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; } 
}

Uncontrollable 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 I can use Safearrayto transfer other arrays BSTRwithout problems between two applications. So this is something characteristic of the passage of the structure.

Stefano

+5
1

, , , , VT_DISPATCH? , , VT_RECORD, ( ), [StructLayout(LayoutKind.Sequential)].

. , , , DISP_E_BADINDEX? indices ? ? ( , SafeArrayPutElement , , ?)

+3

All Articles