Unicode unmanaged string routing in .net. Tall characters replaced by question marks

This may or may not be a SWIG issue.

I am trying to return std :: wstring by value from a C ++ function in C #. The returned string has a mixture of simple old English characters and Hebrew characters. English characters do well, but Hebrew characters at some point translate into a question mark symbol.

I use SWIG to generate the marshalling code. After going through all this with the help of the debugger, he goes on to the next SWIG code ...

static string CreateWString([MarshalAs(UnmanagedType.LPWStr)]IntPtr cString) { string str = System.Runtime.InteropServices.Marshal.PtrToStringUni(cString); return str; } 

At this point, str looks perfect.

This is then returned to the CIG SWIG code, which returns str as void *.

Then void * again becomes the .net chain in the calling code. Are all Jewish characters in the calling code? signs.

Any ideas what causes this?

Edit:

Additional Information

Once CreateWString returns to the calling C ++, will you see that the pointer in the debugger looks like an ANSI c string? notes in it. It seems that .net is doing some kind of conversion on the string before it gets back to the caller. Does this sound right? How can I control this?

Edit 2: It seems I need to do MarshallAs on the return type of CreateWString in order to stop .net from converting to TCHAR (?) Type

+4
source share
1 answer

Change the definition of CreateWString to:

 [return: MarshalAs(UnmanagedType.LPWStr)] static string CreateWString([MarshalAs(UnmanagedType.LPWStr)]IntPtr cString) 

See the examples in the MarshalAsAttribute documentation.

+4
source

All Articles