Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting logon username 3

Status
Not open for further replies.

fumei

Technical User
Oct 23, 2002
9,349
CA
Just to make things simple, there are two ways to get the system logon username.

1. Using the environment variable. This is straightforward and should work for most cases.
Code:
Sub GetNameUsingEnviron()
   MsgBox Environ("username")
End Sub

2. Using API.
Code:
Option Explicit

[COLOR=red]' the API function[/color red]
Private Declare Function GetUserName Lib "advapi32.dll" _
   Alias "GetUserNameA" _
   (ByVal lpBuffer As String, _
   nSize As Long) As Long

[COLOR=red]' the API-using function[/color red]
Function GetName() As String
  Dim lpBuffer As String
  Dim nSize As Long

[COLOR=red]' initialize buffer[/color red]
  nSize = 255
  lpBuffer = Space$(nSize)

[COLOR=red]' get the name[/color red]
  If GetUserName(lpBuffer, nSize) Then
[COLOR=red]     ' return the username
     ' remembering API returns an extra character[/color red]
     GetName = Left$(lpBuffer, nSize - 1)
  Else
     GetName = ""
  End If
End Function

[COLOR=red]' the Sub to use the API-using function[/color red]
Sub GetNameUsingAPI()
   MsgBox GetName
End Sub

In both cases, the messagebox returns my logon name:

gerry.knight

Gerry
 
Sorry Gerry, not meaning to hijack your thread, just to add to it.

As long as your PC is networked you can use the Windows Scripting Host to get the username (and Domain and Computer Name information) as well:
Code:
Sub GetNameUsingScriptingHost()
Dim wshDetails As Object
Set wshDetails = CreateObject("WScript.Network")

'Debug.Print wshDetails.UserDomain & "\" & wshDetails.UserName & " on " & wshDetails.ComputerName

MsgBox wshDetails.UserName

Set wshDetails = Nothing
End Sub
This will return my logon andrewb

Regards

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
So verbose ... ;-)
Code:
Public Function GetName() As String
    GetName = CreateObject("WScript.Network").UserName
End Function
 
Haha, I like it [smile]

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
So, OK, there are three basic ways...

Environ
API
WScript

I think that settles that, with the comment that using API is probably the least likely way to go. I mean...why bother? strongm's function is clean and short...and works.

Gerry
 
Well ... there's always WMI as well, for the sake of completeness (and it provides a little more info for the sake of some more opaque code)...
Code:
[blue]Public Function GetNameWMI() As String
    Dim objItem As Variant

    For Each objItem In GetObject("winmgmts:\\" & "." & "\root\CIMV2").ExecQuery("SELECT * FROM Win32_ComputerSystem")
        GetNameWMI = objItem.UserName
    Next
End Function[/blue]
 
LOL. You are just being a sh$^ disturber! Just kidding.

Any MORE ways???????

Gerry
 
Oh, probably ... :)
 
(for example, there's a more advanced version of the API solution, using GetUserNameEx ...)
 
All's Interesting, thanxs for that.

But how would one extract the User Id Details
like the Full Name from ActiveDirectory for instance ?

"In three words I can sum up everything I've learned about life: it goes on."
- Robert Frost 1874-1963
 
You would need to do an XPath query from that - AD is not the same beast!
Code:
Const ADS_SCOPE_SUBTREE = 2

  Set objConnection = CreateObject("ADODB.Connection")
  Set objCommand = CreateObject("ADODB.Command")
  
  objConnection.Provider = "ADsDSOObject"
  objConnection.Open "Active Directory Provider"
  
  Set objCommand.ActiveConnection = objConnection

  objCommand.Properties("Page Size") = 1000
  objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

  objCommand.CommandText = _
      "SELECT Name, Department, Title, sAMAccountName FROM 'LDAP://dc=Your,dc=domain,dc=here' WHERE objectCategory='user' "
  'AND sAMAccountName !=''
  
  Set objRecordset = objCommand.Execute

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
You can do that again using WMI, instead of using Win32_ComputerSystem, use Win32_UserAccount (for details of the available properties see here

Hope this helps

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Love this place. Thanks to all for contributing.

Gerry
 
The dumb version:
Code:
[blue]Public Function GetADFullName() As String
    Dim CurrentUser As Object
    Dim ADSysInfo As Object
    
    Set ADSysInfo = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
    GetADFullName = CurrentUser.FullName
End Function[/blue]

The slightly more helpful version:
Code:
[blue]Public Function GetADFullName() As String
    ' Add a reference to Active DS Type Library
    Dim CurrentUser As IADsUser
    Dim ADSysInfo As ADSystemInfo
    
    Set ADSysInfo = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
    GetADFullName = CurrentUser.FullName
End Function[/blue]

You can then have a look at the object browser to see all the properties and methods of IADsUser (or look at MSDN).
 
sorry - should specify that what I posted was a generic way to get AD info for all users rather than current user name...

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top