Click here to Skip to main content
15,868,164 members
Articles / Desktop Programming / MFC

Using Unicode in INI files

Rate me:
Please Sign up or sign in to vote.
4.88/5 (23 votes)
3 Apr 2014CPOL2 min read 129.3K   51   25
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, XP or later uses the encoding internally. This is why Windows particularly names UTF16-little Endian "Unicode".

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Japan Japan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to use the code to Create the ini file with VB? Pin
Member 124058916-Mar-18 21:41
Member 124058916-Mar-18 21:41 
QuestionMy Vote of Four Pin
Member 109444318-Dec-15 9:34
professionalMember 109444318-Dec-15 9:34 
Questiongratitude Pin
Member 114218752-Feb-15 13:46
Member 114218752-Feb-15 13:46 
QuestionPK collection Pin
Member 1123940521-Dec-14 21:59
Member 1123940521-Dec-14 21:59 
Question[My vote of 2] Serious bug in your code Pin
Victor Nijegorodov6-Apr-14 23:57
Victor Nijegorodov6-Apr-14 23:57 
AnswerRe: [My vote of 2] Serious bug in your code Pin
Manabu Kumeta7-Apr-14 3:39
Manabu Kumeta7-Apr-14 3:39 
QuestionMy Vote of 5 Pin
raybert2-Apr-14 11:03
raybert2-Apr-14 11:03 
AnswerRe: My Vote of 5 Pin
Manabu Kumeta3-Apr-14 5:47
Manabu Kumeta3-Apr-14 5:47 
GeneralMy vote of 5 Pin
Member 356017731-Jan-11 6:53
Member 356017731-Jan-11 6:53 
GeneralRe: My vote of 5 Pin
Manabu Kumeta18-Jun-11 4:55
Manabu Kumeta18-Jun-11 4:55 
GeneralRe: My vote of 5 Pin
Roger Gonsalves27-Sep-11 16:10
Roger Gonsalves27-Sep-11 16:10 
GeneralDownload source file Pin
Daniel Xu7-Jan-08 15:03
Daniel Xu7-Jan-08 15:03 
AnswerRe: Download source file Pin
Manabu Kumeta12-Jan-08 2:01
Manabu Kumeta12-Jan-08 2:01 
Generalgood one Pin
paulgafa28-Jun-07 4:44
paulgafa28-Jun-07 4:44 
Generaltypo UTF18 Pin
virtualnik8-May-07 4:53
virtualnik8-May-07 4:53 
GeneralRe: typo UTF18 Pin
peterchen22-Aug-08 4:09
peterchen22-Aug-08 4:09 
AnswerRe: typo UTF18 Pin
Manabu Kumeta24-Aug-08 1:42
Manabu Kumeta24-Aug-08 1:42 
GeneralRe: typo UTF18 Pin
chandanadhikari19-Jun-11 19:14
chandanadhikari19-Jun-11 19:14 
Hi mana , thanks for the very informative article Smile | :) . Just wanted to know if
you are sure that UTF-8 encoding is not supported for INI files??i am asking because i am working with a small application which generates an INI file and this INI file data is input to other functions. I wanted this INI file to be UTF-8 encoded but from your article i gather that this may not be possible. Is there some way of i can get INI file with UTF-8 encoding??---thanks in advance
Generalnice article... Pin
nsh_enp10-Jul-06 14:21
nsh_enp10-Jul-06 14:21 
GeneralThanks Pin
Manabu Kumeta11-Jul-06 5:31
Manabu Kumeta11-Jul-06 5:31 
GeneralVery good! Pin
fredzhu24-Mar-05 14:27
fredzhu24-Mar-05 14:27 
GeneralThanks Pin
Manabu Kumeta3-Apr-05 17:50
Manabu Kumeta3-Apr-05 17:50 
GeneralJust what I needed Pin
Rob Caldecott20-Dec-04 1:00
Rob Caldecott20-Dec-04 1:00 
GeneralThank you Pin
Manabu Kumeta25-Dec-04 5:03
Manabu Kumeta25-Dec-04 5:03 

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

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