GetUserNameA 不检索最新的用户名 [英] GetUserNameA doesn't retrieve the latest user name

查看:31
本文介绍了GetUserNameA 不检索最新的用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 windows api 获取当前 windows 用户登录的函数 (

在 OS Build 18362.418 上,您可以看到以下 UI 和设置.根据您的描述,您似乎在使用此构建版本并导致此问题.我也可以在这个构建版本上重现这个问题.

On OS Build 18362.449 与 18362.356 具有相同的 UI 和设置,如我上面所示,我无法在此构建版本上重现此问题.

I have a function which uses windows api to get current windows user login (https://support.microsoft.com/en-nz/help/152970/visual-basic-procedure-to-get-current-user-name).

Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

' Main routine to Dimension variables, retrieve user name.
Public Function WindowsUserName() As String

     ' Dimension variables
     Dim lpBuff As String * 25
     Dim ret As Long, UserName As String

     ' Get the user name minus any trailing spaces found in the name.
     ret = GetUserName(lpBuff, 25)
     WindowsUserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)

End Function

Problem: if I renamed the windows user (in Windows 10), this function always get the old name, even though I login with the new name.

Is there anyway to retrieve the latest user name in VBA?

Updated: In few comments, the account name changed in Control Panel is display name, not user name. Now back to my question: Is there anyway to retrieve the latest DISPLAY name in VBA?

解决方案

There are some differences in user account management between OS Build 18362.356 and 18362.418. (There are some other versions between these two. I didn't test those versions.)

On OS Build 18362.356 you can see the below UI and settings. You can change the user name via Control Panel\User Accounts\User Accounts -> Manage User Accounts -> Properties. Either you change other account user name as a administrator or change your logged in account user name. After restart you can get the new user name using GetUserName API or command set username.

On OS Build 18362.418 you can see the below UI and settings. Based on your description you seems using this build version and result in this issue. I can also reproduce this issue on this build version.

On OS Build 18362.449 has the same UI and settings with 18362.356 as I showed above and I can't reproduce this issue on this build version.

这篇关于GetUserNameA 不检索最新的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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