Marshal array of doubles from c to c#

2019-10-09 17:33发布

问题:

Can someone please tell me exactly what I should write to marshal an array of double pointers between C and C#?

struct foo { double *abc[20]; };

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class foo {  // ??? abc;   }

I have had many oblique resonses to 2 previous queries, and I have read another dozen or so questions here on this same topic, but I can't find in any of them a simple answer to this question.

回答1:

Ok, here is the simple, straightforward answer to this question:

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public IntPtr[] abc;

The trick is that, counterintuitively, you must use an IntPtr despite the fact this is an array of double pointers, not int pointers.