Get Windows2000 Userid?

bayman

Registered User.
Local time
Today, 07:34
Joined
May 25, 2001
Messages
96
How do I code to determine the Windows NT/2000 Userid of the current user?

Thanks in advance.
 
bayman

the following code certainly works on win98/NT - don't use Win2K at the moment, but it should work ok. the following code uses Win32 API. I have given a function for getting the UserName, and also a function for getting the Computer Name, as i find it is often usefull to have both peices of information.

_______________________________________

Private Declare Function API_GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

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


Public Function GetWinComputerName()
    Dim strBuf$, lngSize&, lngReturn&
    lngSize = 255
    strBuf = String(lngSize, " ")
    lngReturn = API_GetComputerName(strBuf, lngSize)
    
    strBuf = Left(strBuf, lngSize)
    GetWinComputerName = strBuf
End Function

Public Function GetWinUserName()
    Dim strBuf$, lngSize&, lngReturn&
    lngSize = 255
    strBuf = String(lngSize, " ")
    lngReturn = API_GetUserName(strBuf, lngSize)
    
    strBuf = Left(strBuf, lngSize - 1)
    GetWinUserName = strBuf
End Function

_______________________________________

you can then call either the GetWinUserName() or GetWinComputerName() funcitons to get the info you want. i.e.

Dim strUserName As String

strUserName = GeWinUserName()

Hope that helps

axa
 
axa and bayman,

It works fine on Win2K, I just tried it out. bayman, you can do a quick test with this:

Dim strUserName As String
Dim strCompName As String

strUserName = GetWinUserName()
strCompName = GetWinComputerName()

MsgBox ("User name is " & GetWinUserName & ". " & " The computer name is " & _
strCompName & ".")

axa, thanks for the code, I was looking for something like that for one of my projects.
 

Users who are viewing this thread

Back
Top Bottom