M
MSweetG222
All,
I have reviewed the several ways of retrieving a User Name from various
postings here. I have listed 3 below. Are the results of each is coming
from different places? Can someone tell me from where each is pulling the
UserName info?
I am currently assuming that the API & WScript are coming from the same
place since I rec'd the same answer (until someone tells me otherwise). Are
there pros/cons of using the WScript vs. API function?
Thank your for any assistance.
MSweetG222
Option Explicit
'===================================================
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
Function UserNameWindows()
Dim lngLen As Long
Dim strBuffer As String
Const dhcMaxUserName = 255
strBuffer = Space(dhcMaxUserName)
lngLen = dhcMaxUserName
If CBool(GetUserName(strBuffer, lngLen)) Then
UserNameWindows = Left$(strBuffer, lngLen - 1)
Else
UserNameWindows = ""
End If
End Function
'===================================================
Function UserName()
Dim wsnet
Set wsnet = CreateObject("wscript.network")
UserName = wsnet.UserName
End Function
'===================================================
Function EnvironUserName()
EnvironUserName = Environ("USERNAME")
End Function
'===================================================
I have reviewed the several ways of retrieving a User Name from various
postings here. I have listed 3 below. Are the results of each is coming
from different places? Can someone tell me from where each is pulling the
UserName info?
I am currently assuming that the API & WScript are coming from the same
place since I rec'd the same answer (until someone tells me otherwise). Are
there pros/cons of using the WScript vs. API function?
Thank your for any assistance.
MSweetG222
Option Explicit
'===================================================
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
Function UserNameWindows()
Dim lngLen As Long
Dim strBuffer As String
Const dhcMaxUserName = 255
strBuffer = Space(dhcMaxUserName)
lngLen = dhcMaxUserName
If CBool(GetUserName(strBuffer, lngLen)) Then
UserNameWindows = Left$(strBuffer, lngLen - 1)
Else
UserNameWindows = ""
End If
End Function
'===================================================
Function UserName()
Dim wsnet
Set wsnet = CreateObject("wscript.network")
UserName = wsnet.UserName
End Function
'===================================================
Function EnvironUserName()
EnvironUserName = Environ("USERNAME")
End Function
'===================================================