Getting NT user id


  How can i get the nt user id on VB 5?

LVL 12
Alfredo Luis Torres SerranoDevelopment Director / DBAAsked:
Who is Participating?
Just asign them to me...
You can check-out Look under Windows NT. They have code which covers most NT Admin functions.

Under NT, 2000, and 9x I believe the GetUserName API function call is sufficient:

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Heres the code that you need to get the log in name. You will need to modify it to suit your needs.
Let me know if you need futrther assitants.


'Call Windows API
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long

Public Sub Buffer_Set()
sBuffer = Space$(255)                                   'Buffer for the name
lSize = Len(sBuffer)                                    'Actual size if the name
Call GetUserName(sBuffer, lSize)                        'Get the name from system
End Sub

 Call Buffer_Set                                         'Go and get the user name
    txtUSER.Text = Left$(sBuffer, lSize)              'Display user
While you can use many different API function to get user and domain information, if you're running under Windows NT or 2000 Server there is a shortest path, based on the fact that the operating system loads many piece of data into environment variables:

' this code works only under Windows NT and Windows 2000 Server
Debug.Print "Computer Name: " & Environ$("COMPUTERNAME")
Debug.Print "Logon Server: " & Environ$("LOGONSERVER")
Debug.Print "User Domain: " & Environ$("USERNAME")
Debug.Print "User Name: " & Environ$("USERNAME")

Alfredo Luis Torres SerranoDevelopment Director / DBAAuthor Commented:
Your answer does not work for me, thanks anyway.

tmack: please let me know how can i give the points to you, i have solved the problem with you answer.

Thanks a lot

Just an FYI, the answer tmack posted was the same as mine although posted several minutes later. Just to verify, did my code example reference not work for you?

If the code is bad I would like to know so that I can correct the source. Thanks.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.