views:

321

answers:

4

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.

A: 

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

Tommy Hui
+4  A: 

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.

Daniel Earwicker
+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
So do I! I personally wouldn't use any development environment that made it harder to use Unicode.
Daniel Earwicker
Thanks for the hint to CP_ACP, with that I found it.
Torsten Marek
+4  A: 

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.

Jason Cohen
+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
+1  A: 

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)

Torsten Marek
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
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