-
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...
-
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
-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks