How do WritePrivateProfileStringA() and Unicode go together?

0

Issue

I’m working on a legacy project that uses INI files and I’m currently trying to understand how Microsoft deals with INI files.

The documentation of WritePrivateProfileStringA() [MSDN] says for lpFileName:

If the file was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function writes ANSI characters.

What exactly does that mean? What is a file "created using Unicode characters"? How does Microsoft determine whether a file was created using Unicode characters or not?

Since this is documented under lpFileName, do they refer to Unicode characters in the file name, like "if the file has a Japanese file name, we’ll read it as Unicode"?

Solution

By default neither the ...A() nor the ...W() method supports Unicode as file contents for INI files. If e.g. the file does not exist, they will both create a file with ANSI content.

However, if you create the INI file first and you give it a UTF-16 BOM (byte-order-mark), both ...A() and ...W() will respect that BOM and write UTF-16 characters to the file.

Other than the BOM, the file can be empty, so a 2 byte file with 0xFF 0xFE content is enough to get the Microsoft API to write Unicode characters.

Both methods will not recognize and respect a UTF-8 BOM. In fact, a UTF-8 BOM can break an existing file if the UTF-8 BOM and the first section are both in line 1. In that case you can’t access any of the keys in the affected section. If the first section is in line 2, the UTF-8 BOM will have no effect.

My tests on Windows 10 21H1 cannot confirm a statement about UTF16-BE support from 2006:

Just for fun, you can even reverse the BOM bytes and WritePrivateProfileString will write to it as a UTF-16 BE (Big Endian) file!

Answered By – Thomas Weller

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More