5

I need the current user and the domain. I am using a VB 6 application.

Thanks

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
Jedi Master Spooky
  • 5,629
  • 13
  • 57
  • 86

5 Answers5

17

One way would be to ask the environment:

Dim UserName As String
Dim UserDomain As String
UserName   = Environ("USERNAME")
UserDomain = Environ("USERDOMAIN")

(Works on Windows NT and up only, obviously.)

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • This is quite unsecure if you need to do checks based on the current user. It is enough to modify the environment variables (use `SET USERNAME=pwned` in a command prompt and run the application from that prompt). – Fulvio Mar 10 '16 at 19:04
  • The word "insecure" implies that it's security risk. That's not at all the case. Of course the entire environment of a process can be written to, but that's generally the case - and nonetheless, all kinds of applications read their stuff from the environment. If anyone bases tasks that need proper authorization upon values that they have read from the environment then it's entirely their fault. – Tomalak Mar 10 '16 at 19:30
  • I wanted to add additional information to the above comment but SO blocked me yesterday for whatever reason. My point was to underline that the API-version posted by Stefan is a bit better from a security point of view, given that the Username is provided using Win32 API and can't be modified as easily as the env variable USERNAME/USERDOMAIN. – Fulvio Mar 11 '16 at 09:47
  • I understand that. My point is that this should not be used for security-related tasks in the first place. Windows has an inbuilt authentication and security scheme: The account token that is bound to the process determines your access rights, not strings you read from one function or the other. – Tomalak Mar 11 '16 at 11:59
8

And the API-version:

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

Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long

Private Sub Form_Load()  
     Dim sDomainName As String * 255   
     Dim lDomainNameLength As Long     
     Dim sUserName as String
     Dim bUserSid(255) As Byte      
     Dim lSIDType As Long 

    Rem Create a buffer
    sUserName = String(100, Chr$(0))  

    Rem Get the username
     GetUserName sUserName, 100  

    Rem strip the rest of the buffer
    sUserName = Left$(sUserName, InStr(sUserName, Chr$(0)) - 1)

     rem Show the temppath and the username
     MsgBox "Hello " + strUserName 

     lResult = LookupAccountName(vbNullString, sUserName, bUserSid(0), 255, sDomainName, lDomainNameLength, _
  lSIDType)
    if lResult <>0 then
       msgbox sDomainName
    end if
end sub
Stefan
  • 11,423
  • 8
  • 50
  • 75
4

Use the following methods of the WshNetwork object, which is available after you reference the Windows Script Host Object Model in your project:

Dim Network As WshNetwork
Set Network = New WshNetwork

Debug.Print "ComputerName: " & Network.ComputerName
Debug.Print "UserDomain: " & Network.UserDomain
Debug.Print "UserName: " & Network.UserName

I cast the results to upper or lower case for consistency, but those are the methods you need.

Note that when run on a machine that's not logged into a domain, both ComputerName and UserDomain return the same thing -- the computer name.

JeffK
  • 3,019
  • 2
  • 26
  • 29
2

Basically you need to make Windows API calls. Searching vbnet.mvps.org I get the following answers.

Will Rickards
  • 2,776
  • 2
  • 19
  • 25
0

What about this?

Private Function IsAdmin() As Boolean
Dim groups As Object
Dim user As Object

Set groups = GetObject("WinNT://./administrators")

For Each user In groups.members

If UCase(Environ("USERNAME")) = UCase(user.Name) Then
IsAdmin = True
End If

Next user

End Function
Saiyine
  • 1,579
  • 3
  • 15
  • 27
  • Can't recommend using this implementation even with purpose of determining whether the user has administrative rights. First, the default return value is not set, even though it is False implicitly. Second, you should exit the For loop once names match. – Vadim Belyaev Aug 01 '13 at 12:15
  • the code might be less than ideal but it does answer the question namely Environ("USERNAME") – MikeT Jul 30 '14 at 08:42