[redland-dev] C# binding encoding problem

Edd Dumbill edd at usefulinc.com
Wed Sep 22 17:32:09 BST 2004


On Tue, 2004-09-21 at 09:07 +0100, Victor Lindesay wrote:
> Firstly I had to make changes to the C# wrapper regarding passing
> strings between managed and unmanaged code. I have had to change all
> IntPtr pointer = Marshal.StringToHGlobalAuto(aString); lines to IntPtr
> pointer = Marshal.StringToHGlobalAnsi(aString); and string aString =
> Marshal.PtrToStringAuto(pointer); lines to string aString =
> Marshal.PtrToStringAnsi(pointer);.
> 
> Otherwise any calls that pass strings to or return strings from librdf
> do not work.
>
> For example, without these changes, stepping through the C code I
> notice that if I create a "rdfxml" parser, the line: 
> if(!strcmp(syntax_name, "rdfxml")) { ... 
> in 
> librdf_parser_raptor_constructor() fails.

Hmm, this is interesting but not necessarily wrong. The *Auto methods
marshal to and from Unicode on Windows.  You can't use strcmp against
Unicode strings and expect it to work, except with UTF-8 encoded strings
that only contain ASCII characters.

However, this code has only really been tested with Mono, so I'd be
interested to see what Windows does. What's the contents, byte by byte,
of syntax_name ?  I wonder whether it's in UTF-8 or some other Unicode
encoding.

> However with the string marshalling changes I get various text
> encoding problems. For example when I parse my FOAF file [1], loading
> from URL with librdf_parser_parse_into_model(), and stream out the
> triples, all my buddies with latin-1 characters come out wrong.
> Leandro Mariano López comes out as Leandro Mariano López!

You're seeing UTF-8 output there.  Redland converts strings into UTF-8
internally.

> Am I doing something wrong compiling Redland? Or is it something on
> the .Net side?
> 
> I am using .Net 1.1 on Windows 2003 and compiling Redland with VC++ 6.

If it's not a misunderstanding on your part, then it's smelling like a
difference between .NET and Mono.  I'd be grateful for your help in
investigating this.

-- Edd





More information about the redland-dev mailing list