[Mono-dev] Interop with Native Libraries Question

Mike Edenfield kutulu at kutulu.org
Thu Mar 6 18:23:48 EST 2008

Sebi Onofrei wrote:
> Now, what I tried looks like this:
> *        [DllImport (libraryName, EntryPoint = "method_name", CharSet 
> = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
>         private static unsafe extern integer EXT_method_name(void* 
> handle, out string name, out int length);*
If I remember my interop correctly, you need to tack on a 
[MarshalAs.UnmanagedType(UnmanagedType.LPChar)], or something close to 
that, in front of the string parameter.  CLR strings are quite different 
from old-style null terminated C "strings", and there are many different 
ways the Interop library can interpret string, so you need to tell it.

I think there's also a parameter you can pass into the MarshalAs 
attribute that tells the compiler that the "length" parameter is 
expected to hold the length of the string on the return from the call, 
so it can get the allocation right.


More information about the Mono-devel-list mailing list