[Mono-list] Embedded Mono using DllImport(__Internal) and returning string

Robert Jordan robertj at gmx.net
Mon Mar 12 20:34:36 UTC 2012


On 12.03.2012 20:51, efontana wrote:
> I'm using Embedded Mono and P/Invoke DllImport.   If I have a method which
> returns a string
>
>
>    [DllImport("__Internal", EntryPoint = "CSharp_Test_ReturnString")]
>      public extern static string CSharp_Test_ReturnString();
>
> The corresponding "C" method should strdup the string right?

Yes.

>
> extern "C"
> {
>    char* CSharp_Test_ReturnString()
>    {
>      QString sv = "QString allocated";
>      return strdup((const char*)sv.toAscii());
>    }
> }
>
> Won't the CLR free that string for me?


It will, but under Windows you must take care which function
you're using for the allocation. By definition, only memory
allocated with CoTaskMemAlloc is safely freed by the CLR,
but I don't know which combination will actually work
with Mono/Windows. You may want to look at the source code.


Robert



More information about the Mono-list mailing list