Using Unicode in INI files and WritePrivateProfileStringW
2012-10-18 16:36
1241 查看
http://blogs.msdn.com/b/michkap/archive/2006/09/15/754992.aspx
今天看了老外的博客,关于WritePrivateProfileStringW是否能生成unicode文件的问题. 这个博客的博主看来对msdn上
解释也看不明白,看来老外看msdn的解释也会犯晕阿. 说白了,就是这个博主觉得: 微软既然已经提供了WritePrivateProfileStringW而
非WritePrivateProfileString 函数, 那么WritePrivateProfileStringW生成的.ini文件就应该是unicode的文件. 这是这位博主的一厢情愿罢了.
一般而言,WritePrivateProfileStringW生成的文件还是anscii文件, 这样就需要程序员自己把这个文件转化为unicode文件.或者在调用
WritePrivateProfileStringW函数之前,程序员自己去生成一个unicode文件, 然后再调用WritePrivateProfileStringW去写.ini文件。
详细可以参考:
http://www.codeproject.com/Articles/9071/Using-Unicode-in-INI-files
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
into the beginning of the file before using initially
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
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 particularly names UTF16-little Endian "Unicode".
今天看了老外的博客,关于WritePrivateProfileStringW是否能生成unicode文件的问题. 这个博客的博主看来对msdn上
解释也看不明白,看来老外看msdn的解释也会犯晕阿. 说白了,就是这个博主觉得: 微软既然已经提供了WritePrivateProfileStringW而
非WritePrivateProfileString 函数, 那么WritePrivateProfileStringW生成的.ini文件就应该是unicode的文件. 这是这位博主的一厢情愿罢了.
一般而言,WritePrivateProfileStringW生成的文件还是anscii文件, 这样就需要程序员自己把这个文件转化为unicode文件.或者在调用
WritePrivateProfileStringW函数之前,程序员自己去生成一个unicode文件, 然后再调用WritePrivateProfileStringW去写.ini文件。
详细可以参考:
http://www.codeproject.com/Articles/9071/Using-Unicode-in-INI-files
Using Unicode in INI files
By Mana#,15 Dec 2004Introduction
This article shows how to use Unicode in an INI file. Even though we callWritePrivateProfileStringWAPI 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 Endianinto the beginning of the file before using initially
WritePrivateProfileW.
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 bracket LPTSTR 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 included 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. Theother 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 particularly names UTF16-little Endian "Unicode".
Reference
Unicode Enabled - about Unicode around Windows相关文章推荐
- How do I write to files using cheerio and request in node?
- win7之GetPrivateProfileStringW和WritePrivateProfileStringW访问C:\\ProgramFiles\\目录下文件权限不足问题
- 利用WritePrivateProfileString和GetPrivateProfileString读写配置文件.ini
- C++读写ini配置文件GetPrivateProfileString()&WritePrivateProfileString()
- INI文件编程,WINAPI函数WritePrivateProfileString,GetPrivateProfileString
- C++读写ini配置文件GetPrivateProfileString()&WritePrivateProfileString()
- WritePrivateProfileString等读写.ini配置文件
- BW:How to load and generate a custom hierarchy in SAP Business Warehouse (SAP BW) without using flat files
- WritePrivateProfileString和GetPrivateProfileString读写ini配置文件
- 利用GetPrivateProfileString/WritePrivateProfileString读写配置文件(.ini)
- How to read and write JSON files in Java(Gson)
- Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s are also using JDK installs
- Read and Write files IN PHP
- WritePrivateProfileString等读写.ini配置文件
- WritePrivateProfileString等读写.ini配置文件
- Working with PDF files in C# using PdfBox and IKVM
- INI文件编程,WINAPI函数WritePrivateProfileString,GetPrivateProfileString
- Tips: 使用autotools生成动态.so库文件 / Generate .so library files using autotools in Solaris and Linux
- *.ini 文件 与 WritePrivateProfileString()和GetPrivateProfileString()
- INI文件编程,WINAPI函数WritePrivateProfileString,GetPrivateProfileString