Unicode Strings in ini-Files

Write an Unicode String to an ini-File using WritePrivateProfileString

If you try to write an unicode string like e.g. "日本人" to an ini-file then you will notice that the function WritePrivateProfileString fails.
The solution is to create the ini-file with the encoding UTF-16LE before writing an unicode string to it.
Please note: You need to create the file before writing to it the first time (only once).

 

Sample Sourcecode:

CString inifile=L"c:\\temp\sample.ini";

FILE *stream = NULL;
// make sure the file does not already exist
if (_wfopen_s (&stream,inifile,L"r") == ENOENT)
{
    // create file with encoding UTF-16LE
    if (_wfopen_s (&stream,inifile,L"w, ccs=UTF-16LE") == 0)
    {
        // optional: add some text e.g.
        fputws (L"// UNICODE INI FILE\n",stream);
    }
}
if (stream != NULL) fclose (stream);

// write unicode string to ini-file
WritePrivateProfileString(L"SAMPLEKEY",L"SAMPLEVALUE",L"日本人",inifile);

Disclaimer: The information on this page is provided "as is" without warranty of any kind. Further, Arclab Software OHG does not warrant, guarantee, or make any representations regarding the use, or the results of use, in terms of correctness, accuracy, reliability, currentness, or otherwise. See: License Agreement