Marshal SafeArray as array<T>

  • Thread starter Thread starter Christian Schmidt
  • Start date Start date
C

Christian Schmidt

Hi all,
I need to implement an unmanaged function that gets a SafeArray and
hands it over to a managed function having the managed array-type.
Using MarshalAs I can call unmanaged functions having SafeArray from
managed code. But here I need this vice-versa...

I assume it is doable with MarshalAs magic, but how?

Thanks
Christian

example:

ref class Funcs {
static double mysum(array<double>^ arr) {
double sum = 0;
for each(double x in arr) sum += x;
return sum;
}
};

extern "C" {
double WINAPI mysum(SAFEARRAY** arr) {
// ERROR - need some MarshalAs magic
return Funcs::mysum(arr);
}
}
 
Christian Schmidt said:
Hi all,
I need to implement an unmanaged function that gets a SafeArray and hands
it over to a managed function having the managed array-type.
Using MarshalAs I can call unmanaged functions having SafeArray from
managed code. But here I need this vice-versa...

I assume it is doable with MarshalAs magic, but how?

It's not. A managed array must be on the managed heap. The only way to get
the data onto the managed heap is to copy it.

-cd
 
Back
Top