CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2003
    Posts
    18

    Question 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?

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    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.

  3. #3
    Join Date
    May 2003
    Posts
    18

    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()?

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    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.

  5. #5
    Join Date
    Mar 2007
    Posts
    155

    Re: PInvoke - How to convert IntPtr to double[]?

    Quote Originally Posted by senglory View Post
    If, for example, I know the size of returned data, is it possible to avoid Marshal.Copy()?
    In unsafe block.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    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.

  7. #7
    Join Date
    Mar 2007
    Posts
    155

    Re: PInvoke - How to convert IntPtr to double[]?

    Quote Originally Posted by Mutant_Fruit View Post
    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.

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    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
  •  





Click Here to Expand Forum to Full Width

Featured