如何在python中获取Windows用户的全名? [英] How to get Windows user's full name in python?

查看:72
本文介绍了如何在python中获取Windows用户的全名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取用户的全名.不是登录名,而是显示在 Windows 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?

推荐答案

有点谷歌搜索让我此链接

和这段代码:

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

在 Windows XP 上测试并运行

Tested and works on Windows XP

正如 OP 在此处的评论中所指出的,pywin32 包装了相同的内容API 调用到更简单的函数中:

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

win32api.GetUserName(3)

GetUserName 指向 ctypes.windll.secur32.GetUserNameExW,并且 33 相同ctypes

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

这篇关于如何在python中获取Windows用户的全名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆