[Mono-devel-list] reading char * from extern function

Miguel de Icaza miguel at ximian.com
Sat May 24 10:19:18 EDT 2003


Hello,

> You can use "string" as return type:
> 
> [DllImport ("libname", EntryPoint="some_func")]
> static extern string libname_some_func (int uid, byte [] program);
> 
> I guess the runtime should free the string for you, but we don't do that
> currently. Somebody should test how MS handles this.

I just watched a show on MSDN TV:

http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030424NETFXSK/manifest.xml

They say that the runtime will free memory in P/Invoke signatures with
the "string" return value (They use CoMemFree or something). 

If you want to avoid having the runtime release the memory then you
have to use the following signature:

	static extern IntPtr libname_some_func ();

And then use the PtrToStringAuto () on the returned IntPtr.

Miguel.



More information about the Mono-devel-list mailing list