windows GetUserName Win32 API 有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/953954/
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 12:35:13  来源:igfitidea点击:

What's wrong with GetUserName Win32 API?

windowswinapi

提问by Yigang Wu

I'm using GetUserName Win32 API to get the user name of my computer, but I found the user name is different (uppercase vs. lowercase only) when using my VPN connection into work when I was at home. I'm wondering if the VPN client or other software could be affecting the username?

我正在使用 GetUserName Win32 API 来获取我的计算机的用户名,但是当我在家时使用我的 VPN 连接工作时,我发现用户名是不同的(仅大写与小写)。我想知道 VPN 客户端或其他软件是否会影响用户名?

回答by Shane Powell

The GetUserNameAPI states:

GetUserNameAPI状态:

Retrieves the name of the user associated with the current thread.

Use the GetUserNameEx function to retrieve the user name in a specified format. Additional information is provided by the IADsADSystemInfo interface.

检索与当前线程关联的用户的名称。

使用 GetUserNameEx 函数以指定格式检索用户名。附加信息由 IADsADSystemInfo 接口提供。

So it looks like that GetUserName uses IADsADSystemInfoto get it's information.

所以看起来 GetUserName 使用IADsADSystemInfo来获取它的信息。

If you look at the IADsADSystemInfointerface you see it has the method:

如果您查看IADsADSystemInfo接口,您会发现它具有以下方法:

get_UserName
Retrieves the Active Directory distinguished name of the current user, which is the logged-on user or the user impersonated by the calling thread.

get_UserName
检索当前用户的 Active Directory 专有名称,即登录用户或调用线程模拟的用户。

So when your connected via VPN to a domain login you will most likely get Active Directory distinguished name of the current user and when you aren't, you most likely get the user name that you typed in (in whatever case you typed in) to log onto the computer with.

因此,当您通过 VPN 连接到域登录时,您很可能会获得当前用户的 Active Directory 专有名称,如果不是,则很可能会获得您输入的用户名(无论您输入的是什么)登录到计算机。

回答by esac

This API returns the name as typed by the user when logging on to the computer. So if my username is 'esac', but I type 'Esac', this API will return 'Esac'. Subsequently if I type 'ESAC' that is what it will return as well.

此 API 返回用户在登录计算机时键入的名称。因此,如果我的用户名是“esac”,但我输入了“Esac”,则此 API 将返回“Esac”。随后,如果我输入“ESAC”,它也会返回。

回答by Ana Betts

Not 100% sure, but I suspect that GetUserName will end up talking to the DC when you're connected to your domain network, whereas it will use the local answer otherwise

不是 100% 确定,但我怀疑当您连接到域网络时 GetUserName 最终会与 DC 交谈,否则它将使用本地答案