简体   繁体   English

如何在Windows中登录用户的全名?

[英]How to get logged-in user's full name in windows?

How to get logged-in user's full name (the one he/she entered as his/her real name) using windows API or something else? 如何使用Windows API或其他东西获取登录用户的全名(他/她输入的名称)? For example how to get "John Smith", not "john" (as it were his username). 例如,如何获得“John Smith”,而不是“john”(因为这是他的用户名)。

GetUserName(...) doesn't do the job because it returns the username, not the full name. GetUserName(...)不执行该作业,因为它返回用户名,而不是全名。

你试过GetUserNameEx (NameDisplay,...)吗?

A quick Google reveals that NetUserGetInfo should do this. 一个快速的谷歌揭示NetUserGetInfo应该这样做。 It doesn't look like the easiest API in the world to use. 它看起来不像世界上最容易使用的API。

I think the level you're after is 10, which returns a USER_INFO_10 structure, containing, among other things, a usri10_full_name . 我认为你所追求的级别是10,它返回一个USER_INFO_10结构,其中包含一个usri10_full_name

Make sure you remember to free the structure when finished, using NetApiBufferFree ! 使用NetApiBufferFree确保您记得在完成后释放结构!

NetQueryDisplayInformation should help. NetQueryDisplayInformation应该有所帮助。 The field usri1_full_name will give the full name 字段usri1_full_name将给出全名

Well, if the user never entered it, there's no way for you to get it. 好吧,如果用户从未输入过,那么你无法获得它。 You could look for installed email programs and politely ask them for the info, but that's a bad idea for many reasons. 您可以查找已安装的电子邮件程序并礼貌地询问他们的信息,但出于多种原因这是一个坏主意。

Here's your best shot: Get the name the user entered when registering the copy of Windows. 这是您的最佳镜头:获取用户在注册Windows副本时输入的名称。 This is in the registry. 这是在注册表中。 The exact location differs between Windows versions, but in recent versions, it's at HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion (and in Windows 95/98/ME, at HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion ). Windows版本的确切位置不同,但在最新版本中,它位于HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion (以及Windows 95/98 / ME, HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion )。

In there, you'll find RegisteredOwner, which is where the was supposed to enter the name. 在那里,你会找到RegisteredOwner,这是应该输入名称的地方。 Obviously, if this is a company computer and the machine was set up by IT, it's very common to find a standardized company string there. 显然,如果这是一台公司计算机而且机器是由IT设置的,那么在那里找到标准化的公司字符串是很常见的。 And, of course, lots of people will enter names there like "Joe Sixpack" or "Cracked by Quartex". 当然,很多人会在那里输入名字,如“Joe Sixpack”或“Cracked by Quartex”。 However, that's as close as you can get. 然而,这是尽可能接近的。

I have found some places in windows registry with my email or my full name: 我在Windows注册表中找到了一些地方用我的电子邮件或我的全名:

  1. HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\Common\\UserInfo has UserName which contains my full name. HKEY_CURRENT_USER \\ Software \\ Microsoft \\ Office \\ Common \\ UserInfo具有包含我的全名的UserName
  2. KEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer has Logon User Name which contains my email. KEY_CURRENT_USER \\ Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Explorer具有包含我的电子邮件的登录用户名
  3. HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon has DefaultUserName which contains my email. HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Windows NT \\ CurrentVersion \\ Winlogon具有包含我的电子邮件的DefaultUserName

The first one obviously requires Microsoft Office to be installed and that the user has entered his/her name in the Office settings. 第一个显然需要安装Microsoft Office,并且用户已在Office设置中输入了他/她的名字。 The second and the third are more reliable but contains email instead but email is also good (or better) identification than full name. 第二个和第三个更可靠,但包含电子邮件,但电子邮件也比完整名称更好(或更好)识别。

I believe I have found a related answer which works better than GetUserNameEx alone, that is to say, I can handle some cases where GetUserNameEx fails. 我相信我找到了一个比GetUserNameEx更好的相关答案,也就是说,我可以处理GetUserNameEx失败的一些情况。

My alternative answer is here including sample code for Delphi. 我的备选答案是这里包括德尔福的示例代码。

In short, if GetUserInfoEx(3,...) fails, read GetUserInfoEx(2,...) which returns a name in the form "machinename\\username", which you can then pass into NetUserInfo functions in NETAPI32.dll, which will read the local SAM database, which is where the user's full name is stored, if they have set it in the local SAM database. 简而言之,如果GetUserInfoEx(3,...)失败,请读取GetUserInfoEx(2,...),它返回“machinename \\ username”形式的名称,然后您可以将其传递到NETAPI32.dll中的NetUserInfo函数,将读取本地SAM数据库,如果用户的全名已存储在本地SAM数据库中,则该数据库将存储该数据库。 Of course, many home non-domain users have never set this up, so the other answers here are likely to provide some hints too. 当然,许多家庭非域用户从未设置过这个,所以这里的其他答案也可能提供一些提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM