tags:

views:

267

answers:

1

So I want to use SetThreadLocale to set a threads codepage to UTF8. Up to now, I've been using the second parameter of atl string conversion macros like "CT2A(szBUF, CP_UTF8)" to do this. But I want to be able to set the thread codepage once in the beginning with SetThreadLocale() and never have to use the second parameter of the conversion macro again.

How do I do this? SetThreadLocale won't take a code page parameter like CP_UTF8, just an LCID. What parameters should I be feeding SetThreadLocale to achieve this??

Keep in mind, I have no particular language in mind. The strings I get could be Japanese, Korean, English Etc. So far, I'm having no problems with this mix of strings when specifying CP_UTF8 as the second parameter of a conversion macro. You may ask "well then why not just keep using the second parameter". Answer, "because it can be easily forgotten by team members working on the code. It would be nice if it would just work correctly using the default 1 parameter version of the conversion macro."

+2  A: 

SetThreadLocale expects a language identifier, but UTF-8 is not a language identifier - it's a Unicode encoding. One of the purposes of the land ID is to tell the system how to treat ANSI text in the range 128-255. Given a real language, its code page will be used when dealing with such characters. UTF-8, OTOH, is a compressed representation of Unicode text. In order to create UTF-8 text, your input has to be Unicode. Given ANSI text, you just won't know how to convert the upper range of characters. This is way when done "manually", in order to convert ANSI to UTF-8 you have to first use MultiByteToWideChar with a specified codepage, and only then can you convert the resulting Unicode string to UTF-8.

Now, back to your question - I would go another way. If the additional codepage param bugs you that much, make a macro that will hide it or so (or inherit the CT2A class and have the second param fixed).

eran
This explanation makes sense, thanks. Yeah I think I'm just going to use the second parameter. Doesn't seem to be an easy way to set a code page for the thread once.
Dan G