The underlying TestDoubleArray() returns double*. I'd like to have it converti=ed on my C# side to double[]. How can I do it?Code:[DllImport("PlainCLibrary.dll")]
internal static extern IntPtr TestDoubleArray(double[] dbl, long len);
Printable View
The underlying TestDoubleArray() returns double*. I'd like to have it converti=ed on my C# side to double[]. How can I do it?Code:[DllImport("PlainCLibrary.dll")]
internal static extern IntPtr TestDoubleArray(double[] dbl, long len);
How do you know how long the array is? If you know exactly how long it is, you can just instantiate a double[] of the correct size and then use Marshal.Copy. If you don't know how long it is, there's nothing you can do.
If, for example, I know the size of returned data, is it possible to avoid Marshal.Copy()?
Nope, probably not. Does the function require you to free the returned pointer or does the native library 'own' the pointer? Also, is the size of the double* constant? i.e. is it *always* going to be a fixed size like 6 elements?
Which is no better than what Marshal.Copy does except now you've made your library unsafe. You're better off just using Marshal.Copy.
If he wants a managed double [] then there's no way for him to prevent the copy. No matter how it's achieved, a new double[] of the required length will be created and the data will be copied from the double* to the double[]. If he wants to pass a double * around the place and use that directly, then sure, he can prevent the copy. However he'd also open himself up to a whole bunch of memory corruption issues which couldn't happen if he just marshalled the double* straight away. There's absolutely no reason to avoid Marshal.Copy if you want a managed double[].Quote:
I agree, but he can prevent the copy.