您的位置:首页 > 其它

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

Using Unicode in INI files

By Mana#,15 Dec 2004

Introduction

This article shows how to use Unicode in an INI file. Even though we call
WritePrivateProfileStringW
API 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 that
WritePrivateProfileStringW
function 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 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 using
WriteFile()
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. 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 particularly names UTF16-little Endian "Unicode".

Reference

Unicode Enabled - about Unicode around Windows
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐