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

Maser, Dan Dan.Maser at inin.com
Wed Jun 3 13:38:16 EDT 2009


  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 | dan.maser at inin.com

Interactive Intelligence Inc.
Deliberately Innovative
www.inin.com

From: ptr [mailto:ptrajkumar at gmail.com]
Sent: Wednesday, June 03, 2009 12:36 PM
To: Maser, Dan
Cc: mono-list at lists.ximian.com
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 <Dan.Maser at inin.com<mailto: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> [mailto: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<mailto: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<mailto: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/43dce190/attachment-0001.html 


More information about the Mono-list mailing list