Take the 2-minute tour ×

How do I get the encoding that is used for the string returned by GetUserName from the win32 API? I'm using pywin32 and it returns an 8-bit string. On my German XP, this string is obviously encoded using Latin-1, but this might not be the case for other Windows installations.

I could use GetUserNameW, but I would have to wrap that myself using ctypes, which I'd like to avoid for now if there is a simpler solution.

share|improve this question

4 Answers 4

up vote 5 down vote accepted

You can call GetACP to find the current ANSI codepage, which is what non-Unicode APIs use. You can also use MultiByteToWideChar, and pass zero as the codepage (CP_ACP is defined as zero in the Windows headers) to convert a codepage string to Unicode.

share|improve this answer
+1 for the right answer, but I still believe in my answer of using the wide API even though it's more work. –  Jason Cohen Mar 21 '09 at 19:11
So do I! I personally wouldn't use any development environment that made it harder to use Unicode. –  Daniel Earwicker Mar 21 '09 at 19:14
Thanks for the hint to CP_ACP, with that I found it. –  Torsten Marek Mar 22 '09 at 13:05

I realize this isn't answering your question directly, but I strongly recommend you go through the trouble of using the Unicode-clean GetUserNameW as you mentioned.

The non-wide commands work differently on different Windows editions (e.g. ME, although I admit that example is old!), so IMHO it's worth just getting it right.

Having done a lot of multi-lingual Windows development, although the wide API can add a layer of translation or wrapping (as you suggest!), it's worth it.

share|improve this answer
+1. You're right, but I'd rather have pywin32 use the underlying Unicode-ready function. I've filed a bug for that, let's see how that turns out. –  Torsten Marek Mar 22 '09 at 13:07

Okay, here's what I'm using right now:

>>> import win32api
>>> u = unicode(win32api.GetUserName(), "mbcs")
>>> type(u)
<type 'unicode'>

mbcs is a special standard encoding in Windows:

Windows only: Encode operand according to the ANSI codepage (CP_ACP)

share|improve this answer
That is almost equivalent to using GetUserNameW() as others have suggested. It will only fail when the user name contains characters that cannot be represented in the selected "ANSI" code page. –  D.Shawley Mar 22 '09 at 18:00
Well, I hope that either Windows stops users from having a username that is not representable in ANS, that I never have a user where that is the case or that pywin32 fixes their function and uses GetUserNameW ;) –  Torsten Marek Mar 23 '09 at 21:17

From the API docs, GetUserNameA will return the name in ANSI and GetUserNameW returns the name in Unicode. You will have to use GetUserNameW.

share|improve this answer

Your Answer


