Marshalling char** from C DLL to C#


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Marshalling char** from C DLL to C#

  1. #1
    Ricardo Nunnini Guest

    Marshalling char** from C DLL to C#


    I have a function in a DLL that returns a char**.

    What is the correct MarshalAs attribute incantation to use? I've tried just
    about every combination I can think of, the best seems to be [MarshalAs(UnmanagedType.LPArray,
    ArraySubType=UnmanagedType.LPStr, SizeParamIndex=1)] but even that just keeps
    returning a (char**)null.

    All ideas gratefully received...

  2. #2
    Mattias Sjögren Guest

    Re: Marshalling char** from C DLL to C#

    Ricardo,

    >What is the correct MarshalAs attribute incantation to use?


    I suggest you type the parameter as "out int", and then pass the
    pointer you get back to Marshal.PtrToStringAnsi() to retrieve the
    actual string. In beta 2, you'll use IntPtr instead of int.


    Mattias

    ====================================
    Mattias Sjögren - mattias @ mvps.org
    http://www.msjogren.net/dotnet/

    CodeHound - The Software Developer's Search Engine
    http://www.codehound.com

  3. #3
    Ricardo Nunnini Guest

    Re: Marshalling char** from C DLL to C#


    Mattias

    Yipes! I thought we weren't meant to do naughty things like that anymore
    in the .NET world. I'll do it if it works, but I *know* I'll feel guilty
    afterwards

    I can see that Marshal.PtrToStringAnsi() would give me a nice Ansi string
    from a pointer, but how about my array of strings (the function in the DLL
    returns a char**, not just a char*)? Is there some magic with Marshal.AddOfArrayElement(arr,index)?

    Ricardo

    PS Thanks for the fast, and helpful, response.

  4. #4
    Eric Gunnerson Guest

    Re: Marshalling char** from C DLL to C#

    Note that you also have to worry about who's going to free the returned
    object (if it needs to be freed).

    You could consider the managed extensions to C++; they provide much more
    control.

    "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
    news:3af3fd3f.625709@news.devx.com...
    > Ricardo,
    >
    > >What is the correct MarshalAs attribute incantation to use?

    >
    > I suggest you type the parameter as "out int", and then pass the
    > pointer you get back to Marshal.PtrToStringAnsi() to retrieve the
    > actual string. In beta 2, you'll use IntPtr instead of int.
    >
    >
    > Mattias
    >
    > ====================================
    > Mattias Sjögren - mattias @ mvps.org
    > http://www.msjogren.net/dotnet/
    >
    > CodeHound - The Software Developer's Search Engine
    > http://www.codehound.com




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center