This may or may not be a SWIG question.
I am trying to return a std::wstring by value from a C++ function to C#. The returned string has a mixture of plain old English chars and Hebrew chars. The English chars come through fine, but the Hebrew chars are being converted to the question mark symbol at some point.
I am using SWIG to generate the marshalling code. Stepping through it all with a debugger, it gets to the following 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.
Next this is returned to the SWIG-generated C++ code which returns the str as a void*.
Then the void* becomes a .net string again in the calling code. In the calling code, all the hebrew chars are ? marks.
Any ideas what's causing this?
Edit:
Further information
As soon as CreateWString returns to the C++ caller you can see the pointer in the debugger looks to be an ANSI string with ? marks in it. So it seems that .net is performing some sort of conversion on the string before it returns to the caller. Does that sound right? How can I control that?
Edit 2: It seems I need to do MarshallAs on the return type CreateWString to stop .net from converting to a TCHAR type (?)
Modify the CreateWString
definition to:
[return: MarshalAs(UnmanagedType.LPWStr)]
static string CreateWString([MarshalAs(UnmanagedType.LPWStr)]IntPtr cString)
See the examples in the MarshalAsAttribute documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With