7

I'm trying to get the user's full name. Not the login name, but the full name that shows up on the upper right side of the start menu in Windows 7. It might only show up as the full name in an active directory setting.

os.environ['USERNAME']
win32api.GetUserName()

These both return the login name. How do I get the user's full name?

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Brian Schlenker
  • 4,966
  • 6
  • 31
  • 44

2 Answers2

20

A bit of googling gives me this link

and this code:

import ctypes
 
def get_display_name():
    GetUserNameEx = ctypes.windll.secur32.GetUserNameExW
    NameDisplay = 3
 
    size = ctypes.pointer(ctypes.c_ulong(0))
    GetUserNameEx(NameDisplay, None, size)
 
    nameBuffer = ctypes.create_unicode_buffer(size.contents.value)
    GetUserNameEx(NameDisplay, nameBuffer, size)
    return nameBuffer.value

Tested and works on Windows XP


As noted by OP in comment here, pywin32 wraps this same API call into a simpler function:

win32api.GetUserName(3)

GetUserName pointing to ctypes.windll.secur32.GetUserNameExW, and 3 being the same 3 as the constant from ctypes

Socowi
  • 25,550
  • 3
  • 32
  • 54
mhlester
  • 22,781
  • 10
  • 52
  • 75
  • 2
    Just figured out you can also use win32api.GetUserName(3) if you have pywin32 installed – Brian Schlenker Feb 13 '14 at 22:39
  • 1
    And a link to the docs. http://msdn.microsoft.com/en-us/library/windows/desktop/ms724435(v=vs.85).aspx. EXTENDED_NAME_FORMAT explains what different parameters to this function do. – Brian Schlenker Feb 14 '14 at 02:45
  • 8
    I'm using `pywin32` v219 (latest as of this writing) and `win32api.GetUserName` does not accept any parameters. I had to use `win32api.GetUserNameEx(3)`, which works like a charm. Not sure if this is an issue for anyone else. – self. Jul 22 '15 at 13:30
2

For more detailed information:

import ctypes
 
def get_data(EXTENDED_NAME_FORMAT: int):
    GetUserNameEx = ctypes.windll.secur32.GetUserNameExW
    data = EXTENDED_NAME_FORMAT
 
    size = ctypes.pointer(ctypes.c_ulong(0))
    GetUserNameEx(data, None, size)
 
    nameBuffer = ctypes.create_unicode_buffer(size.contents.value)
    GetUserNameEx(data, nameBuffer, size)
    return nameBuffer.value

print("NameUnknown            : ", get_data(0))
print("NameFullyQualifiedDN   : ", get_data(1))
print("NameSamCompatible      : ", get_data(2))
print("NameDisplay            : ", get_data(3))
print("NameUniqueId           : ", get_data(6))
print("NameCanonical          : ", get_data(7))
print("NameUserPrincipal      : ", get_data(8))
print("NameCanonicalEx        : ", get_data(9))
print("NameServicePrincipal   : ", get_data(10))
print("NameDnsDomain          : ", get_data(12))

example output:

NameUnknown            :  
NameFullyQualifiedDN   :  CN=Tomasevic Milovan (TomasevicM),OU=Engineering,DC=Microsoft,DC=Com
NameSamCompatible      :  Microsoft\TomasevicM
NameDisplay            :  Tomašević Milovan
NameUniqueId           :  {4fa050f0-f561-11cf-bdd9-00aa003a77b6}
NameCanonical          :  Microsoft.Com/Engineering/Tomasevic Milovan (TomasevicM)
NameUserPrincipal      :  TomasevicM@Microsoft.Com
NameCanonicalEx        :  Microsoft.Com/Engineering Tomasevic Milovan (TomasevicM)
NameServicePrincipal   :
NameDnsDomain          :  Microsoft.Com\TomasevicM

Constants

  • NameUnknown: An unknown name type.
  • NameFullyQualifiedDN: The fully qualified distinguished name (for example, CN=Jeff Smith,OU=Users,DC=Engineering,DC=Microsoft,DC=Com).
  • NameSamCompatible: A legacy account name (for example, Engineering\JSmith). The domain-only version includes trailing backslashes ().
  • NameDisplay: A "friendly" display name (for example, Jeff Smith). The display name is not necessarily the defining relative distinguished name (RDN).
  • NameUniqueId: A GUID string that the IIDFromString function returns (for example, {4fa050f0-f561-11cf-bdd9-00aa003a77b6}).
  • NameCanonical: The complete canonical name (for example, engineering.microsoft.com/software/someone). The domain-only version includes a trailing forward slash (/).
  • NameUserPrincipal: The user principal name (for example, someone@example.com).
  • NameCanonicalEx: The same as NameCanonical except that the rightmost forward slash (/) is replaced with a new line character (\n), even in a domain-only case (for example, engineering.microsoft.com/software\nJSmith).
  • NameServicePrincipal: The generalized service principal name (for example, www/www.microsoft.com@microsoft.com).
  • NameDnsDomain: The DNS domain name followed by a backward-slash and the SAM user name.

Requirements

  • Minimum supported client: Windows 2000 Professional [desktop apps only]
  • Minimum supported server: Windows 2000 Server [desktop apps only]
  • Header: secext.h (include Security.h)
Milovan Tomašević
  • 6,823
  • 1
  • 50
  • 42