Hi all
Could someone explain why the Function & Property Get below is used to return the username and computer name when the Environ method does exactly the same in only two lines of code.
I would sooner use the Environ method but not if there is a valid reason why the function is used.
Code
Option Explicit
Private Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" ( _
ByVal lpBuffer As String, _
ByRef nSize As Long) As Long
Private Declare Function GetComputerName Lib "kernel32.dll" _
Alias "GetComputerNameA" ( _
ByVal lbbuffer As String, _
nSize As Long) As Long
Public Property Get UserName() As String
Dim stBuff As String * 255, lAPIResult As Long
Dim lBuffLen As Long
On Error GoTo UserName_err
lBuffLen = 255
lAPIResult = GetUserName(stBuff, lBuffLen)
If lBuffLen > 0 Then UserName = Left(stBuff, lBuffLen - 1)
Exit Property
UserName_err:
UserName = "Unidentified"
End Property
Public Property Get ComputerName() As String
Dim stBuff As String * 255, lAPIResult As Long
Dim lBuffLen As Long
On Error GoTo ComputerName_err
lBuffLen = 255
lAPIResult = GetComputerName(stBuff, lBuffLen)
If lBuffLen > 0 Then ComputerName = Left(stBuff, lBuffLen)
Exit Property
ComputerName_err:
ComputerName = "unidentified"
End Property
Display More
Confused!!
Kev.