Reputation: 86552
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.
Upvotes: 2
Views: 1727
Reputation: 86552
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)
Upvotes: 4
Reputation: 83051
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.
Upvotes: 5
Reputation: 116714
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.
Upvotes: 5
Reputation: 1336
From the API docs, GetUserNameA will return the name in ANSI and GetUserNameW returns the name in Unicode. You will have to use GetUserNameW.
Upvotes: 0