Get user id from Windows 2000 - Tough question?


Posted by Ron on June 22, 2001 1:04 PM

Does anyone know if it's possible through code to get the name of the user that is logged into the PC with Windows 2000??

We would like to be able to populate parts of and Excel application with the user name if this can be done.

Thanks!!



Posted by Ivan F Moala on June 22, 2001 8:23 PM


Not sure if this will work with windows 2000
BUT.......

Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
(ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long

Function GetName() As String
Dim strUserName As String
strUserName = Space(255)
WNetGetUser "", strUserName, 255
GetName = Trim(strUserName)
End Function

· lpName
Points to a null-terminated string that specifies either the name of the local device that has been redirected to a network resource, or the remote name of a network resource to which a connection has been made.
If this parameter is NULL, Windows returns the name of the current user for the process.

· lpUserName
Points to a buffer that receives the null-terminated user name.

· lpnLength
Points to a variable that specifies the size, in characters, of the buffer pointed to by lpUserName. If the call fails because the buffer is not big enough, this variable contains the required buffer size.

OR

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

Function GetUser() As String
Dim lpUserID As String
Dim nBuffer As Long
Dim Ret As Long

lpUserID = String(25, 0)
nBuffer = 255
Ret = GetUserName(lpUserID, nBuffer)
If Ret Then
GetUser = lpUserID
End If

End Function

Ivan