• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

Displaying user's full name from active directory


Can anyone tell me code that will allow me to display the current logon user's full name taken from the active directory and display it in a textbox or caption for my visual basic app ?
1 Solution
here is a sample code that may get you what you need
How about summat like this, add to a module then call GetLoggedInUserID

Private Declare Function GetUserName Lib "advapi32" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long

Public Function GetLoggedInUserID() As String

    Dim sBuffer As String
    Dim lSize As Long
    sBuffer = Space$(255)
    lSize = Len(sBuffer)
    If GetUserName(sBuffer, lSize) = 1 Then
        GetLoggedInUserID = Left$(sBuffer, lstrlenW(StrPtr(sBuffer)))
        GetLoggedInUserID = Environ("Username")
    End If

End Function

TechLadAuthor Commented:
Thats not going to work as that looks like it's going to display the username, i'm wanting to display the full name so for example

Username = 123654
Full name = Techlad
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Sigurdur HaraldssonSystem AdministratorCommented:
Check this out: http://groups.google.com/group/VBScript/browse_thread/thread/dfff2a978ff19fa7

I adapted the script there to this one:

Set objRootDSE = GetObject("LDAP://RootDSE")
If Err.Number = 0 Then
    strNamingContext = objRootDSE.Get("defaultNamingContext")
    WScript.Echo("Connected to:...... " & strNamingContext)
    WScript.Echo("Unable to connect to Active Directory." & vbCrLf & 
"Exiting Script." & vbCrLf)
    objShell.Popup line, 10, vbTab & "Logon Script", 48
End If
Set objADSysInfo = CreateObject("ADSystemInfo")
strUserDN = objADSysInfo.username
WScript.Echo("DN:................ " & strUserDN)
' Bind to user object
Set objUser = Getobject("LDAP://" & strUserDN)

strFirstName = objUser.Get("givenName")
strInitials = objUser.Get("initials")
strLastName = objUser.Get("sn")
TechLadAuthor Commented:
how does that allow me to display the user's full name in a text box or lable or something ?
I found code on the ms website which iterates all users but can be tweaked to get current user but sighar has already posted code which looks like it will work so unnecessary to post what I found.

the values you can get you just assign to a textbox textbox.value or use the variables like sighar has done
TechLadAuthor Commented:
This is the problem I don't know how to change the code to work with my Visual Basic 6 application
TechLadAuthor Commented:
I found a solution now

Set oADSystemInfo = CreateObject("ADSystemInfo")
Set oADsUser = GetObject("LDAP://" & oADSystemInfo.UserName)
GetLoggedInUserID = oADsUser.DisplayName
ok, if you find sighar's code working then change it to a function to return the name

public function GetFullName()

 ... sighar's code here

  'Return value
  GetFullName = strFirstName & " " & strLastName

end function

add it to a module

now in your form code you do this

mytextbox.value= GetFullName
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.

Join & Write a Comment

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now