[Mono-list] What is Correct way to marshal wchar_t ?

ptr ptrajkumar at gmail.com
Wed Jun 3 13:35:40 EDT 2009


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 <Dan.Maser at inin.com> 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: mono-list-bounces at lists.ximian.com [mailto:
> mono-list-bounces at lists.ximian.com] On Behalf Of ptr2009
> Sent: Wednesday, June 03, 2009 11:54 AM
> To: mono-list at lists.ximian.com
> 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  -  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/20090603/43e875b3/attachment.html 


More information about the Mono-list mailing list