如何可靠地读取所有版本的 Windows 上用户的显示(名字和姓氏)名称?
我发现在 Windows 7 64 位上,在具有域名的计算机上, GetUserNameEx( 3, .... ) 应该将扩展名称格式 DisplayName (==3) 获取到缓冲区中,工作正常。
但是,它不适用于 Windows 7 32 位、位于工作组而不是域上的虚拟机,它返回 ERROR_NONE_MAPPED。
例如,如何以适用于 Windows 的方式读取此人的友好名称“Fred Smith”? GetUserNameEx 显然已损坏。事实上,我被告知,这并没有被破坏,只是不适合不在域中的用户。我想知道,既然本地 SAM 信息存在,为什么不呢?而且似乎没有其他直接的 API 可以做到这一点。
如果 Windows 给你 ERROR_NONE_MAPPED,那么你就不走运了,而且可能不在域上。所以这并不是 API 的友好区域。
[看起来,当不在域上时,可以调用 NetUserGetInfo 来读取本地 SAM 信息,但您需要首先知道用户名和密码,然后它可能会查找友好名称。]
I have found that on Windows 7 64 bit, on a machine with a domain name, GetUserNameEx( 3, .... ) which should get the extended name format DisplayName (==3), into a buffer, works fine.
However, it does not work on Windows 7 32 bit, vm that is on a workgroup, rather than on a domain, it returns ERROR_NONE_MAPPED.
How do you read the person's friendly name "Fred Smith", for example, in a way that works on Windows? GetUserNameEx is manifestly broken. Actually, not broken, I'm told, just not intended to work for users who are not on a domain. Why not, I wonder, since the local SAM information exists? And there appears to be no other direct API to do this.
If Windows gives you ERROR_NONE_MAPPED, you are out of luck, and probably not on a domain. So this is not exactly a friendly area of the API.
[It is possible, it looks like, to call NetUserGetInfo, to read the local SAM info, when not on a domain, but you need to know the user name and password first, and then it will maybe look up the friendly name.]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Warren 的解决方案移植到 C#。我添加了从域名检索域控制器 IP 的功能,因为至少在我的域上,仅使用
\\
作为服务器名称是行不通的。Here is Warren's solution ported to C#. I added retrieval of a domain controller's IP from the domain name because at least on my domain, just using
\\<domain>
as the server name didn't work.我有一个似乎有效的解决方案,这通常意味着:
示例代码(Delphi 中),在 Matt 的回答中移植到 C#:
编辑:2011 年 11 月;删除了死链接。
I have a solution that appears to work, which in general means:
Sample code (in Delphi), ported to C# below in Matt's answer:
Edit: Nov 2011; Removed dead link.