Click here to Skip to main content
Licence 
First Posted 15 Dec 2004
Views 59,612
Bookmarked 31 times

Using Unicode in INI files

By | 15 Dec 2004 | Article
How to use Unicode in INI files.

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.

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mana#



Japan Japan

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberMember 35601776:53 31 Jan '11  
GeneralRe: My vote of 5 PinmemberMana#4:55 18 Jun '11  
GeneralRe: My vote of 5 PinmemberRoger Gonsalves16:10 27 Sep '11  
GeneralDownload source file PinmemberDaniel Xu15:03 7 Jan '08  
AnswerRe: Download source file PinmemberMana#2:01 12 Jan '08  
Generalgood one Pinmemberpaulgafa4:44 28 Jun '07  
Generaltypo UTF18 Pinmembervirtualnik4:53 8 May '07  
GeneralRe: typo UTF18 Pinmemberpeterchen4:09 22 Aug '08  
AnswerRe: typo UTF18 PinmemberMana#1:42 24 Aug '08  
GeneralRe: typo UTF18 Pinmemberchandanadhikari19:14 19 Jun '11  
Generalnice article... Pinmembernsh_enp14:21 10 Jul '06  
GeneralThanks PinmemberMana#5:31 11 Jul '06  
GeneralVery good! Pinmemberfredzhu14:27 24 Mar '05  
GeneralThanks PinmemberMana#17:50 3 Apr '05  
GeneralJust what I needed PinmemberRobert Edward Caldecott1:00 20 Dec '04  
GeneralThank you PinmemberMana#5:03 25 Dec '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 15 Dec 2004
Article Copyright 2004 by Mana#
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid