WritePrivateProfileString - is unpredictable

后端 未结 2 1208
夕颜
夕颜 2021-01-16 02:41

We are using a tool that uses internally the simple Win-Api call WritePrivateProfileString.

I\'m aware of the define flag UNICODE to map either to

相关标签:
2条回答
  • 2021-01-16 03:07
    1. Always use WritePrivateProfileStringW version.

       [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true,
       EntryPoint = "WritePrivateProfileStringW")]
       [return: MarshalAs(UnmanagedType.Bool)]
       public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
      
    2. When the INI files does not exist, create it with Unicode file marker (first two bytes FF FE). By this way all strings are stored as Unicode. To create a blank ini file you can use this:

       File.WriteAllText(ini_file_path, "; Your comment\r\n", Encoding.Unicode);
      

    A blank/comment line at the beginning is recommended. Write at least "\r\n".

    0 讨论(0)
  • 2021-01-16 03:15

    I found the root cause:
    https://en.wikipedia.org/wiki/Unicode_in_Microsoft_Windows

    In April 2018 with insider build 17035 (nominal build 17134) for Windows 10, a "Beta: Use Unicode UTF-8 for worldwide language support" checkbox appeared for setting the locale code page to UTF-8.[a] This allows for calling "narrow" functions, including fopen and SetWindowTextA, with UTF-8 strings. In May 2019 Microsoft added the ability for a program to set the code page to UTF-8 itself, and started recommending that all software do this and use UTF-8 exclusively.

    This setting was activated on some systems and it was responsible for changing my expected ANSI code to UNICODE in the INI-file.

    Yes, I understand that we have to go in the UNICODE direction in the future. But now also Microsoft is pushing us to this direction, which is not bad, but I was not aware of this setting or strategy before.

    @IInspectable: yes you are right 0xA7 is not ASCII but ANSI (or ASCII-8, Extended ASCII) I had mixed it up, thank you.

    0 讨论(0)
提交回复
热议问题