Marshal SafeArray as array<T>

  • Thread starter Christian Schmidt
  • 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);
}
}
 
C

Carl Daniel [VC++ MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top