[Mono-list] Marshaling string on Windows returns Garbage??

Jonathan Chambers joncham at gmail.com
Tue Mar 13 13:31:43 UTC 2012

You could try making the argument an out parameter and seeing if that
works. If so, there must be a bug in marshaling strings as return values.
Please file a bug in that case.

void CSharp_Out_String(out string str);

void CSharp_Out_String(char** str)
     const char* testValue = "test"
     *str = CoTaskMemAlloc(strlen(testValue)+1);
     strcpy(*str, testValue);


On Tue, Mar 13, 2012 at 8:21 AM, efontana <eric at fontanas.net> wrote:

> No, it's a std::string (there an above using namespace std)
> FWIW: I tried even doing this:
> const char* testValue = "test"
> char* s = CoTaskMemAlloc(strlen(testValue)+1);
> strcpy(s, testValue);
> return s;
> That also returns the 8 Question Marks.
> Even more interesting, if I change the signature to
> IntPtr CSharp_Return_String and use Marshal.PtrToStringAnsi, the string is
> correct!!
> If I use the IntPtr method, the C# wrapper will have to call
> Mashal.FreeCoTaskMem to avoid
> the leak, because I'm forced to allocate the string on the C++ side since
> the string is really
> embedded in either a std::string or a Qt QString.
> --
> View this message in context:
> http://mono.1490590.n4.nabble.com/Marshaling-string-on-Windows-returns-Garbage-tp4468863p4468951.html
> Sent from the Mono - General mailing list archive at Nabble.com.
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20120313/f1e01788/attachment.html>

More information about the Mono-list mailing list