(I understand this might be duplicate but I don't understand the other threads)
I'm working with C# an I have a third party dll
that needs int arrays (or pointers to int array) as parameters. How do I marshal an int array between C# and C/C++ ? The functions are declared like this:
// reads/writes int values from/into the array
__declspec(dllimport) void __stdcall ReadStuff(int id, int* buffer);
In C int*
would be a pointer right ? So I'm confused if I have to use IntPtr
or if I could use int[]
(preferred) ? I thought this might be ok:
[DllImport(dllName)]
static extern void ReadStuff(int id, [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_I4)] ref int[] buffer);
// call
int[] array = new int[12];
ReadStuff(1, ref array);
Would that work ? Or how do I have to declare this function in C# in safe code ?