How to get Windows user's full name in python?

拥有回忆 提交于 2019-12-31 02:54:26

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/21766954/how-to-get-windows-users-full-name-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!