获取当前登录的 Windows 用户 [英] Getting the currently logged-in windows user

查看:49
本文介绍了获取当前登录的 Windows 用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过谷歌找到了这个:http://www.mvps.org/access/api/api0008.htm

I found this via google: http://www.mvps.org/access/api/api0008.htm

'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If ( lngX > 0 ) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = vbNullString
    End If
End Function
'******************** Code End **************************

这是最好的方法吗?

推荐答案

您也可以使用 Environ$,但问题指定的方法更好.用户/应用程序可以更改环境变量.

You could also use Environ$ but the method specified by the question is better. Users/Applications can change the environment variables.

这篇关于获取当前登录的 Windows 用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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