Yes, I use SWIG also and I am successfully using both methods I described below. If you're using SWIG than I find that option #1 is easier provided that you have the source to the library you're interopping to, but if you don't then option #2 should work (ie, if the library interface is stuck taking a wchat_t* and you can't change that, option #2 will work for you).
Dan Maser | Lead Software Developer phone & fax +1.317.872.3000 | mobile +1.317.872.3000 | [email protected] Interactive Intelligence Inc. Deliberately Innovative www.inin.com From: ptr [mailto:[email protected]] Sent: Wednesday, June 03, 2009 12:36 PM To: Maser, Dan Cc: [email protected] Subject: Re: [Mono-list] What is Correct way to marshal wchar_t ? I am using swig to generate all my interop stuff as recommended by http://www.mono-project.com/Interop_with_Native_Libraries as I have lot of C++ code that needs to be wrapper. Can swig handle this ? Thanks Raj On Wed, Jun 3, 2009 at 1:31 PM, Maser, Dan <[email protected]<mailto:[email protected]>> wrote: I've found that you have two choices: 1. If you have source to the library you're interop'ing to, you can make the C interface take a "unsigned short*" instead of "wchar_t*" and it will work using the managed CharSet.Unicode for both systems. Of course your C code will have cast the paramter to a wchar_t* on windows but use a conversion function between UTF-16 and UCS-4 or UTF-8 on the non-windows library. 2. You can use C#'s custom marshal code to marshal as 2 bytes wide on windows and 4 bytes wide elsewhere and leave your C code using wchar_t everywhere. See the interfaces of System.Runtime.InteropServices.ICustomMarshaler and the attribute "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(YourClassDerivedFromICustomMarshaler))]". If you need an example of this let me know but the MSDN documentation using these classes should be enough for you to follow. -----Original Message----- From: [email protected]<mailto:[email protected]> [mailto:[email protected]<mailto:[email protected]>] On Behalf Of ptr2009 Sent: Wednesday, June 03, 2009 11:54 AM To: [email protected]<mailto:[email protected]> Subject: [Mono-list] What is Correct way to marshal wchar_t ? hey all I am trying to marshal strings from managed code to native code using DllImport. Marshalling both wchar_t* and char* works under mono on a windows box. But on mac os x wchart_t marshalling does not seem to work. The wchar_t* is all junk. I have tried to search this forum and people have talked about it and it seems to have been fixed. Am I missing something ? I have experimented with CharSet.Unicode on DllImport and it still doesnt work ? What is the recommended way ? Thanks Raj My cs files is something like this ____________________________________________ [DllImport("Test.dll", EntryPoint = "Global_PrintStringW")] public static extern void Global_PrintStringW( [MarshalAs(UnmanagedType.LPWStr)]string arg1); [DllImport("Test.dll", EntryPoint = "Global_PrintStringA")] public static extern void Global_PrintStringA(string arg1); private static void StringMarshallingTest() { string newString = "This is a cool string"; Console.WriteLine("Global_PrintStringA Test "); Global_PrintStringA(newString); Console.WriteLine("Global_PrintStringW Test "); Global_PrintStringW(newString); } My cpp file is something like this ________________________________ extern "C" { EXPORT_API void Global_PrintStringW(const wchar_t * str) { std::wcout << "Global_PrintStringW called with " << str << std::endl; } EXPORT_API void Global_PrintStringA(const char* str) { std::wcout << "Global_PrintStringA called with " << str << std::endl; } } -- View this message in context: http://www.nabble.com/What-is-Correct-way-to-marshal-wchar_t---tp23855173p23855173.html Sent from the Mono - General mailing list archive at Nabble.com. _______________________________________________ Mono-list maillist - [email protected]<mailto:[email protected]> http://lists.ximian.com/mailman/listinfo/mono-list
_______________________________________________ Mono-list maillist - [email protected] http://lists.ximian.com/mailman/listinfo/mono-list
