Http://blogs.msdn.com/ B /michkap/archive/2006/09/15/754992.aspx
Today I read a foreigner's blog about whether writeprivateprofilestringw can generate Unicode files.
I can't understand the explanation. It seems that foreigners may also be confused when reading the msdn explanation. To put it bluntly, this blogger thinks that since Microsoft has provided writeprivateprofilestringw
Non-writeprivateprofilestring function, the. ini file generated by writeprivateprofilestringw should be a Unicode file. This is the hope of the blogger.
Generally, the file generated by writeprivateprofilestringw is still an anscii file, so that the programmer needs to convert the file to a Unicode file by himself.
Before the writeprivateprofilestringw function, the programmer generates a Unicode file and then calls writeprivateprofilestringw to write the. ini file.
For details, refer:
Http://www.codeproject.com/Articles/9071/Using-Unicode-in-INI-files
Using Unicode in ini filesby Mana #, 15 Dec 2004
Introduction
This article shows how to use Unicode in an INI file. Even though we call
WritePrivateProfileStringWAPI function for using Unicode in an INI file, an INI file is not saved as Unicode but saved as ANSI. However, the small code that I show here will let you use that.
What does writeprivateprofilestringw do?
From my experience, I explain thatWritePrivateProfileStringWFunction copies a string encoded to Unicode (UTF18-little endian) into an INI file which can be written with UTF18-little endian encoded character. If the INI file for an application
Does not exist, the API function will create a plain text file and write a section name and a key name and so on. the plain text file is not a unicode format text file but an ANSI format text file. so a unicode format file has to have the "Bom" in the beginning
Of a text file. When the unicode format file already exists as INI file, this function works well. After all, the function does not prepare it, and somebody has to prepare it.
How is the file created?
- Manually creating the INI File
- Preparing the INI file by Coding
Manually creating the INI File
This is the simplest way. We save the ansi ini file already existing as UTF16-little endian using a text editor such as Notepad. This method is suitable for trials.
Preparing the INI file by Coding
The difference between a Unicode and an ANSI format file is whether a file has the BOM in the beginning of the file or not. besides, the BOM has to be UTF16-little endian's Bom. so, we have to create a plain file and then add the BOM of UTF16-little endian
Into the beginning of the file before using initiallyWritePrivateProfileW.
In the following code, when the INI file is not found, the new INI file is created. do notice the Code uses generic text mappings.
Collapse | copy
Code
LPTSTR pszINIFilePath;::GetModuleFileName(NULL, pszINIFilePath, MAX_PATH);::PathRemoveFileSpec(pszINIFilePath);::StrCat(pszINIFilePath, _T("\\App.ini"));LPTSTR pszSectionB = _T("[StringTable]"); // section name with bracket LPTSTR pszSection = _T("StringTable"); // section name without bracketLPTSTR pszKey = _T("String");if(!::PathFileExists(pszINIFilePath)){ // UTF16-LE BOM(FFFE) WORD wBOM = 0xFEFF; DWORD NumberOfBytesWritten; HANDLE hFile = ::CreateFile(pszINIFilePath, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); ::WriteFile(hFile, &wBOM, sizeof(WORD), &NumberOfBytesWritten, NULL); ::WriteFile(hFile, pszSectionB, (_tcslen(pszSectionB)+1)*(sizeof(TCHAR)), &NumberOfBytesWritten, NULL); ::CloseHandle(hFile);} WritePrivateProfileString(pszSection, pszKey, _T(""), pszINIFilePath);Points of interest
In the code, I added not only the BOM but also the first section name into the file by usingWriteFile()Function. This coding can avoid making an empty line in the first line of the file.
Why is it only UTF16-little endian?
In notepad embedded with windows, we can choose 3 encoding formats in Unicode. these are "Unicode" (UTF16-little endian), "Unicode big endian" (UTF16-big endian), and "UTF-8 ". we can use only UTF16-little endian of these formats as an INI file format. the
Other encodings do not work correctly (you examine it once ). probably, the reason is that Windows NT, 2000 or XP uses the encoding internally. this is why Windows particle ly names UTF16-little endian "Unicode ".
Reference
- Unicode enabled-about Unicode around und windows