Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using GetPrivateProfileString to read unicode strings

2,409 views
Skip to first unread message

singurd

unread,
Aug 21, 2009, 4:13:22 AM8/21/09
to
Can i use GetPrivateProfileString/WritePrivateProfileString to read/
write unicode strings from a file ?

Goran

unread,
Aug 21, 2009, 5:54:14 AM8/21/09
to
On Aug 21, 10:13 am, singurd <sing...@yahoo.com> wrote:
> Can i use GetPrivateProfileString/WritePrivateProfileString to read/
> write unicode strings from a file ?

If you have unicode app and "unicode" file (in Windows parlance,
that's UTF-16 encoding), yes. UTF-8-encoded file didn't work last time
I tried.

Goran.

Joseph M. Newcomer

unread,
Aug 21, 2009, 10:19:23 AM8/21/09
to
You can use a Unicode app, or directly call the API
GetPrivateProfileStringW/WritePrivateProfileStringW.
joe

On Fri, 21 Aug 2009 01:13:22 -0700 (PDT), singurd <sin...@yahoo.com> wrote:

>Can i use GetPrivateProfileString/WritePrivateProfileString to read/
>write unicode strings from a file ?

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Mihai N.

unread,
Aug 22, 2009, 4:04:10 AM8/22/09
to
> Can i use GetPrivateProfileString/WritePrivateProfileString to read/
> write unicode strings from a file ?

Trick: if the file does not exist, or exists but it is not Unicode, then
the functions will convert everything to ANSI.
If you want to be unicode, then create an empty file with a BOM only
(FE FF) and then everything you write will be Unicode.

http://blogs.msdn.com/michkap/archive/2006/09/15/754992.aspx

--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Serge Wautier

unread,
Aug 22, 2009, 4:15:38 AM8/22/09
to
I've used UTF-8 encoded values in ANSI ini files a couple of times and
haven't had problems.

--
Serge.
http://www.apptranslator.com - Localization tool for your MFC applications


"Goran" <goran...@gmail.com> wrote in message
news:fd7257b6-8139-429d...@e27g2000yqm.googlegroups.com...

Joseph M. Newcomer

unread,
Aug 22, 2009, 8:39:19 AM8/22/09
to
Fascinating! Could I incorporate parts of that article into my MSDN Errors document, with
a hyperlink to your document?
joe

On Sat, 22 Aug 2009 01:04:10 -0700, "Mihai N." <nmihai_y...@yahoo.com> wrote:

>> Can i use GetPrivateProfileString/WritePrivateProfileString to read/
>> write unicode strings from a file ?
>
>Trick: if the file does not exist, or exists but it is not Unicode, then
>the functions will convert everything to ANSI.
>If you want to be unicode, then create an empty file with a BOM only
>(FE FF) and then everything you write will be Unicode.
>
>http://blogs.msdn.com/michkap/archive/2006/09/15/754992.aspx

Scot T Brennecke

unread,
Aug 22, 2009, 4:50:37 PM8/22/09
to
Come on, Joe. It's not an error. It's by design, and documented. Furthermore, it states this: "Note This function is provided
only for compatibility with 16-bit versions of Windows. Applications should store initialization information in the registry." back
in 16-bit days, we didn't even have Unicode.

Mihai N.

unread,
Aug 22, 2009, 5:49:47 PM8/22/09
to
> Fascinating! Could I incorporate parts of that article into my MSDN Errors
> document, with a hyperlink to your document?
> joe

That is not my blog, it belongs to Michael Kaplan (MS)

Joseph M. Newcomer

unread,
Aug 22, 2009, 8:51:52 PM8/22/09
to
The error appears to be that it is not documented that it does this. I have no problem
with the design, but it needs to say that. According to the cited article, this fact is
not documented.

Note that I primarily am reporting errors of documentation. Sometimes I point out that
the design is flawed. But mostly, it is the omission of crtical information that should
have been written down.

The issue of "carrying settings around" sometimes suggests .INI files, so more people use
them than Microsoft intended. So the reality is that this behavior needs to be
documented, and the failure to provide clear documentation of this interesting behavior is
the "error" I want to cite.
joe

Joseph M. Newcomer

unread,
Aug 22, 2009, 8:53:01 PM8/22/09
to
Yes, I saw that somewhat later. I was a victim of nouth/sorth; I saw some key characters
in the URL and thought it was yours, until I went back to re-read it. So I'll just put a
pointer in my page that references the blog value.
joe

On Sat, 22 Aug 2009 14:49:47 -0700, "Mihai N." <nmihai_y...@yahoo.com> wrote:

>> Fascinating! Could I incorporate parts of that article into my MSDN Errors
>> document, with a hyperlink to your document?
>> joe
>
>That is not my blog, it belongs to Michael Kaplan (MS)

Scot T Brennecke

unread,
Aug 22, 2009, 11:41:11 PM8/22/09
to
Both of these articles state "If the file exists and was created using Unicode characters, the function writes Unicode characters to
the file. Otherwise, the function creates a file using ANSI characters."

WritePrivateProfileString Function (Windows):
http://msdn.microsoft.com/en-us/library/ms725501.aspx

WritePrivateProfileSection Function (Windows):
http://msdn.microsoft.com/en-us/library/ms725500.aspx

Joseph M. Newcomer

unread,
Aug 23, 2009, 12:29:20 AM8/23/09
to
What is not obvious from this is that you must create the file yourself, external to the
WritePrivateProfileString. For example, I had assumed if I called
WritePrivateProfileStringW, I would get a Unicode version of the file, but the article
makes it clear that this really only deals with the input arguments, not the file format.
The fact that the file *cannot* be created as Unicode solely by the APIs is not at all
obvious from those statements.
joe

Scot T Brennecke

unread,
Aug 23, 2009, 3:24:42 PM8/23/09
to
Fair enough. If the API were created anytime within the last decade or so, I'm sure it would have been done more smartly. For
portable settings files, XML format is a much more useful choice.

Goran

unread,
Aug 24, 2009, 3:03:43 AM8/24/09
to
On Aug 22, 10:15 am, "Serge Wautier" <se...@wautier.nospam.net> wrote:
> I've used UTF-8 encoded values in ANSI ini files a couple of times and
> haven't had problems.
>
> --
> Serge.http://www.apptranslator.com- Localization tool for your MFC applications
>
> "Goran" <goran.pu...@gmail.com> wrote in message

>
> news:fd7257b6-8139-429d...@e27g2000yqm.googlegroups.com...
> On Aug 21, 10:13 am, singurd <sing...@yahoo.com> wrote:
>
> > Can i use GetPrivateProfileString/WritePrivateProfileString to read/
> > write unicode strings from a file ?
>
> If you have unicode app and "unicode" file (in Windows parlance,
> that's UTF-16 encoding), yes. UTF-8-encoded file didn't work last time
> I tried.
>
> Goran.

I saved my text with Notepad as UTF-8 (so, with BOM), tried reading
back, and didn't get what I expected. I just re-tried, it's the same.

How did you do it?

Goran.

Mihai N.

unread,
Aug 24, 2009, 3:22:23 AM8/24/09
to
> I saved my text with Notepad as UTF-8 (so, with BOM)

Must be UTF-16

singurd

unread,
Sep 2, 2009, 6:07:02 AM9/2/09
to

Thanks to all
Its all in the file actualy, so i created the unicode file using the
code from the codeproject site.

_TCHAR bom = (_TCHAR)0xFEFF;

CFile* pFile = new CFile();
pFile->Open( strFile, CFile::modeCreate | CFile::modeWrite );
pFile->Write( &bom, sizeof(_TCHAR) );
pFile->Write( LPCTSTR(m_Text), m_Text.GetLength()*sizeof(_TCHAR) );
pFile->Close();

now the strFile file can contain unicode characters


David Ching

unread,
Sep 2, 2009, 9:59:02 AM9/2/09
to
"singurd" <sin...@yahoo.com> wrote in message
news:001e298e-884e-4b47...@l35g2000pra.googlegroups.com...

> Thanks to all
> Its all in the file actualy, so i created the unicode file using the
> code from the codeproject site.
>
> _TCHAR bom = (_TCHAR)0xFEFF;
>
> CFile* pFile = new CFile();
> pFile->Open( strFile, CFile::modeCreate | CFile::modeWrite );
> pFile->Write( &bom, sizeof(_TCHAR) );
> pFile->Write( LPCTSTR(m_Text), m_Text.GetLength()*sizeof(_TCHAR) );
> pFile->Close();
>
> now the strFile file can contain unicode characters
>
>

Didn't Joe say you could just use the 'W' version of e.g.
WritePrivateProfileString --> WritePrivateProfileStringW, and it would
create a UTF-16 .ini file if it didn't already exist? That sounds much
easier.

-- David

Joseph M. Newcomer

unread,
Sep 2, 2009, 3:09:27 PM9/2/09
to
I had assumed it would work, but in fact it does not work that way.
joe
0 new messages