Returning an array with a variable doubling size from C ++ to C # - an easier way?

I have the following C ++ method:

__ declspec (dllexport) void __stdcall getDoubles (int * count, double ** values); the method selects and fills the double array and sets * calculates the size of the array.

The only way I was able to get this to work through pinvoke is:

[System.Runtime.InteropServices.DllImportAttribute("xx.dll")] public static extern void getDoubles(ref int count, ref System.IntPtr values); 

and use:

 int count = 0; IntPtr doubles = new IntPtr(); Nappy.getDoubles(ref count, ref doubles); double[] dvs = new double[count]; for(int i = 0;i < count;++{ dvs[i] = (double)Marshal.PtrToStructure(doubles, typeof(System.Double)); doubles = new IntPtr(doubles.ToInt64()+Marshal.SizeOf(typeof(System.Double))); } 

the values ​​are in the dvs array Is there a better way to do this without forcing pointer arithmetic in a controlled language ...

+2
c ++ arrays c # pinvoke
source share
2 answers

I think you can use

 Marshal.Copy( source, destination, 0, size ); 
+2
source share

You will need to change the unmanaged method signature so that it reads as follows:

 __declspec(dllexport) void __stdcall getDoubles(SAFEARRAY *array); 

Then you can use the following managed version of the function:

 [System.Runtime.InteropServices.DllImportAttribute("xx.dll")] public static extern void getDoubles( [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VT_R8)] double[] array ); 

Of course, you will also have to rewrite unmanaged code to work with SAFEARRAY. More information on this topic can be found on MSDN.

One question, however, I remember working with ZLib in C #, which without a shell can work with byte [], while an unmanaged BYTE * analog, have you tried working directly with double * / double []?

+1
source share

All Articles