|
-
March 30th, 2010, 01:55 PM
#1
PInvoke - How to convert IntPtr to double[]?
Code:
[DllImport("PlainCLibrary.dll")]
internal static extern IntPtr TestDoubleArray(double[] dbl, long len);
The underlying TestDoubleArray() returns double*. I'd like to have it converti=ed on my C# side to double[]. How can I do it?
-
March 30th, 2010, 02:05 PM
#2
Re: PInvoke - How to convert IntPtr to double[]?
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.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
March 30th, 2010, 04:58 PM
#3
Re: PInvoke - How to convert IntPtr to double[]?
If, for example, I know the size of returned data, is it possible to avoid Marshal.Copy()?
-
March 30th, 2010, 06:08 PM
#4
Re: PInvoke - How to convert IntPtr to double[]?
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?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
March 31st, 2010, 12:24 AM
#5
Re: PInvoke - How to convert IntPtr to double[]?
 Originally Posted by senglory
If, for example, I know the size of returned data, is it possible to avoid Marshal.Copy()?
In unsafe block.
-
March 31st, 2010, 03:26 AM
#6
Re: PInvoke - How to convert IntPtr to double[]?
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.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
March 31st, 2010, 04:34 AM
#7
Re: PInvoke - How to convert IntPtr to double[]?
 Originally Posted by Mutant_Fruit
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.
I agree, but he can prevent the copy.
-
March 31st, 2010, 06:37 AM
#8
Re: PInvoke - How to convert IntPtr to double[]?
I agree, but he can prevent the 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[].
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|