Solved

Displaying user's full name from active directory

Posted on 2010-09-05
9
618 Views
Last Modified: 2012-05-10
Hi,

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 ?
0
Comment
Question by:TechLad
9 Comments
 
LVL 9

Expert Comment

by:khaledf
ID: 33607046
here is a sample code that may get you what you need
http://www.bigresource.com/VB-User-info-from-Active-Directory-evDBnWrl2w.html
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33607136
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)))
    Else
        GetLoggedInUserID = Environ("Username")
    End If

End Function

0
 

Author Comment

by:TechLad
ID: 33607209
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
0
 
LVL 11

Expert Comment

by:sighar
ID: 33607259
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)
Else
    WScript.Echo("Unable to connect to Active Directory." & vbCrLf & 
"Exiting Script." & vbCrLf)
    objShell.Popup line, 10, vbTab & "Logon Script", 48
    objLogFile.Close
    Wscript.Quit
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")
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:TechLad
ID: 33607343
how does that allow me to display the user's full name in a text box or lable or something ?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33607372
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
0
 

Author Comment

by:TechLad
ID: 33607424
This is the problem I don't know how to change the code to work with my Visual Basic 6 application
0
 

Accepted Solution

by:
TechLad earned 0 total points
ID: 33607436
I found a solution now

Set oADSystemInfo = CreateObject("ADSystemInfo")
Set oADsUser = GetObject("LDAP://" & oADSystemInfo.UserName)
GetLoggedInUserID = oADsUser.DisplayName
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33607445
ok, if you find sighar's code working then change it to a function to return the name
eg

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
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now