c# wrapping and DllImport attribute's CharSet field

View: New views
3 Messages — Rating Filter:   Alert me  

c# wrapping and DllImport attribute's CharSet field

by Antti Karanta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

                   Hi!

   I have been using swig to generate Java wrappers for some years. Now I  
am using swig to integrate native code with c#.


   The first issue I ran into is passing strings. I have an API that  
expects UTF-8 encoded strings and unfortunately this does not seem to be  
supported out-of-the-box by .NET P/Invoke. The approach I took is to pass  
in the string as UTF-16 to the native side and do the conversion to UTF-8  
on the c side by defining appropriate typemaps (I have the necessary c  
functions to convert from utf-16 to utf-8).
   Defining the appropriate typemaps is easy enough, but as the CharSet  
field of DllImport attribute seems to default to CharSet.Ansi, I'd need to  
be able to make swig set that field to CharSet.Unicode. I looked at the c#  
swig documenation (http://www.swig.org/Doc1.3/CSharp.html), but could find  
nothing about how to affect the CharSet field of DllImport. Is it  
supported?

   Setting attributes for individual parameters seems to be supported in  
the imtype typemap, so I guess this should do the trick:

%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.LPWStr)]")


   Am I on the right track? Or is there a better way to swig wrap UTF-8  
string parameters?




        ::Antti::




-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: c# wrapping and DllImport attribute's CharSet field

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Antti Karanta wrote:

>                    Hi!
>
>    I have been using swig to generate Java wrappers for some years. Now I  
> am using swig to integrate native code with c#.
>
>
>    The first issue I ran into is passing strings. I have an API that  
> expects UTF-8 encoded strings and unfortunately this does not seem to be  
> supported out-of-the-box by .NET P/Invoke. The approach I took is to pass  
> in the string as UTF-16 to the native side and do the conversion to UTF-8  
> on the c side by defining appropriate typemaps (I have the necessary c  
> functions to convert from utf-16 to utf-8).
>    Defining the appropriate typemaps is easy enough, but as the CharSet  
> field of DllImport attribute seems to default to CharSet.Ansi, I'd need to  
> be able to make swig set that field to CharSet.Unicode. I looked at the c#  
> swig documenation (http://www.swig.org/Doc1.3/CSharp.html), but could find  
> nothing about how to affect the CharSet field of DllImport. Is it  
> supported?
>
>    Setting attributes for individual parameters seems to be supported in  
> the imtype typemap, so I guess this should do the trick:
>
> %typemap(imtype, inattributes="[MarshalAs(UnmanagedType.LPWStr)]")
>
>
>    Am I on the right track? Or is there a better way to swig wrap UTF-8  
> string parameters?
>
Yes, I thnnk so!

William



-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: c# wrapping and DllImport attribute's CharSet field

by aviadr1 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Antti Karanta wrote:
                   Hi!

   I have been using swig to generate Java wrappers for some years. Now I  
am using swig to integrate native code with c#.


   The first issue I ran into is passing strings. I have an API that  
expects UTF-8 encoded strings and unfortunately this does not seem to be  
supported out-of-the-box by .NET P/Invoke. The approach I took is to pass  
in the string as UTF-16 to the native side and do the conversion to UTF-8  
on the c side by defining appropriate typemaps (I have the necessary c  
functions to convert from utf-16 to utf-8).
   Defining the appropriate typemaps is easy enough, but as the CharSet  
field of DllImport attribute seems to default to CharSet.Ansi, I'd need to  
be able to make swig set that field to CharSet.Unicode. I looked at the c#  
swig documenation (http://www.swig.org/Doc1.3/CSharp.html), but could find  
nothing about how to affect the CharSet field of DllImport. Is it  
supported?

   Setting attributes for individual parameters seems to be supported in  
the imtype typemap, so I guess this should do the trick:

%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.LPWStr)]")

        ::Antti::
I have the exact same issue, can you please share your solution?