windows 如何在windows中获取登录用户的全名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3438634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:00:05  来源:igfitidea点击:

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

.netc++windowswinapi

提问by Termopetteri

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? For example how to get "John Smith", not "john" (as it were his username).

如何使用windows API或其他方式获取登录用户的全名(他/她输入的真实姓名)?例如如何获得“John Smith”,而不是“john”(因为这是他的用户名)。

GetUserName(...)doesn't do the job because it returns the username, not the full name.

GetUserName(...)没有完成这项工作,因为它返回的是用户名,而不是全名。

采纳答案by Anders

Did you try GetUserNameEx(NameDisplay,...)?

您是否尝试过GetUserNameEx(NameDisplay,...)?

回答by Roman Starkov

A quick Google reveals that NetUserGetInfoshould do this. It doesn't look like the easiest API in the world to use.

一个快速的谷歌显示NetUserGetInfo应该这样做。它看起来不像是世界上最容易使用的 API。

I think the level you're after is 10, which returns a USER_INFO_10structure, 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

回答by Termopetteri

I have found some places in windows registry with my email or my full name:

我在 Windows 注册表中找到了一些带有我的电子邮件或我的全名的地方:

  1. HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfohas UserNamewhich contains my full name.
  2. KEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorerhas Logon User Namewhich contains my email.
  3. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogonhas DefaultUserNamewhich contains my email.
  1. HKEY_CURRENT_USER \软件\微软\办公室\ COMMON \的UserInfo具有用户名包含我的全名。
  2. KEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer具有包含我的电子邮件的登录用户名
  3. 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. The second and the third are more reliable but contains email instead but email is also good (or better) identification than full name.

第一个显然需要安装 Microsoft Office 并且用户已经在 Office 设置中输入了他/她的名字。第二个和第三个更可靠,但包含电子邮件,但电子邮件也比全名更好(或更好)识别。

回答by EboMike

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. 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 副本时输入的名称。这是在注册表中。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. 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. And, of course, lots of people will enter names there like "Joe Sixpack" or "Cracked by Quartex". However, that's as close as you can get.

在那里,您会找到 RegisteredOwner,这是应该输入名称的地方。显然,如果这是一台公司计算机并且这台机器是由 IT 设置的,那么在那里找到标准化的公司字符串是很常见的。而且,当然,很多人会在那里输入诸如“Joe Sixpack”或“Cracked by Quartex”之类的名字。然而,这已经是你所能得到的。

回答by Warren P

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 hereincluding sample code for Delphi.

我的替代答案在这里包括 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. Of course, many home non-domain users have never set this up, so the other answers here are likely to provide some hints too.

简而言之,如果 GetUserInfoEx(3,...) 失败,请读取 GetUserInfoEx(2,...),它以“machinename\username”的形式返回一个名称,然后您可以将其传递给 NETAPI32.dll 中的 NetUserInfo 函数,该函数将读取本地 SAM 数据库,这是存储用户全名的地方,如果他们在本地 SAM 数据库中设置了它。当然,很多家庭非域用户从来没有设置过这个,所以这里的其他答案也可能提供一些提示。

回答by user2482613

NetQueryDisplayInformation should help. The field usri1_full_name will give the full name

NetQueryDisplayInformation 应该会有所帮助。字段 usr1_full_name 将给出全名