AnsiString problem between .NET Server and Delphi(2007) Client using BinMessage

Hello.

My .NET application servers is providing services in .NET Framework(4.5.2) on Windows and Mono(5.2.0.224) on CentOS(6,7).
And Client made in Delphi(2007) communicates with server using BinMessage.

In the test, client with with .NET Framework on Windows server was good but not Mono on CentOS.
And the cause was AnsiString datatype in BinMessage.

[BinHelpers.cs] processes ansi string like this

...
		public static Byte[] AnsiStringToBuffer(String value)
		{
#if SILVERLIGHT || WINRT || PORTABLE_LIBRARY
            return Encoding.UTF8.GetBytes(value);
#else
			return Encoding.Default.GetBytes(value);
#endif
		}
...

System.Text.Encoding.Default in .NET Framework on Windows is

System.Text.DBCSCodePageEncoding (ks_c_5601-1987 / 949)

(Default Encoding in .Net Framework on Windows would be different depends on country and region but it would not be UTF-8.)

and Mono on CentOS is

System.Text.UTF8Encoding (utf-8 / 65001).

So the results of BinHelpers.AnsiStringToBuffer is different depends on platform.

Can I get an API to change the AnsiString encoding type of BinMessage?
or Is it Possible to change Mono’s default encoding?
What would be a good idea to solve this problem?

sorry my poor english :sweat:

Short answer: don’t use AnsiString of you need to preserve chars > 127 between systems where you don’t control the code page on both sides. That’s what Wide and UTF8 strings are for. Ansi strings are “by design” imprecise. :wink:

Hello

First of all, it is not possible to change DefaultEncoding in Mono (you can checi it out by yourself by looking at source files https://github.com/mono/mono/blob/07adebd8bf94f2c4310421460940fd6c873b24d5/mcs/class/referencesource/mscorlib/system/text/encoding.cs and https://github.com/mono/mono/blob/0bcbe39b148bb498742fc68416f8293ccd350fb6/mcs/class/corlib/System.Text/EncodingHelper.cs)

Seems this happens on OSes that don’t have such thing as a codepage. I’ll log an issue to use ASCII encoding if the default one used by the system is UTF8. This means that non-ANSI chars will be replaced with ‘?’ chars, but atm they aren’t transferred correctly anyway.

Note: You still won’t be able to transfer non-ANSI chars via AnsiString datatype from Windows to Mono using the AnsiString datatype. You have to use proper datatype for this

Thanks, logged as bugs://78580

bugs://78580 got closed with status fixed.