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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the UserName 1

Status
Not open for further replies.

Slarti

Programmer
Feb 5, 2003
45
GB
I need to identify the UserName of a person logged onto an NT network. Not the unique identifier that GetUserName finds but resolve that to find the actual name associated with that UserName ie if Arthur Butler logs using 645873 I want my Application to be able to identify that it is Arthur Butler logged in not just User 645873. I hope my question makes a bit of sense?
 
Thanks BB. This retrieves the unique ID that the User logs on with but I want the name of the individual associated with that ID. When a User opens my App I want them to be greeted with their name not just their ID. How do I achieve this?

Slarti.
 
You might have to query the Domain server (NT). That could get tricky, but may be possible.

good luck, John
 
What operating system are you targeting. What is your network domian set up with.



Hope I've been helpful,
Wayne Francis

If you want to get the best response to a question, please check out FAQ222-2244 first
 
I'm on NT and don't know what my network domain is set up with.

Slarti.
 
I'm on NT and don't know what my network domain is set up with. How can I find this out?

Slarti.
 
That's a shame. If you'd been on W2K/XP/W2K3, and you were in an AD domain (rather than an older NT domain) then you could have used the nice and simple GetUserNameEx API call.

As it is, it looks like you'll have to use GetUserName and then the somewhat harder to use (from VB) NetQueryDisplayInformation to get the friendly display name
 
Try this: You'll have to change the value of PRIMARY_USER_ACCOUNT_DOMAIN

Option Explicit
Private Type USER_INFO_3
usri3_name As Long 'LPWSTR in SDK
usri3_password As Long 'LPWSTR in SDK
usri3_password_age As Long 'DWORD in SDK
usri3_priv As Long 'DWORD in SDK
usri3_home_dir As Long 'LPWSTR in SDK
usri3_comment As Long 'LPWSTR in SDK
usri3_flags As Long 'DWORD in SDK
usri3_script_path As Long 'LPWSTR in SDK
usri3_auth_flags As Long 'DWORD in SDK
usri3_full_name As Long 'LPWSTR in SDK
usri3_usr_comment As Long 'LPWSTR in SDK
usri3_parms As Long 'LPWSTR in SDK
usri3_workstations As Long 'LPWSTR in SDK
usri3_last_logon As Long 'DWORD in SDK
usri3_last_logoff As Long 'DWORD in SDK
usri3_acct_expires As Long 'DWORD in SDK
usri3_max_storage As Long 'DWORD in SDK
usri3_units_per_week As Long 'DWORD in SDK
usri3_logon_hours As Long 'PBYTE in SDK
usri3_bad_pw_count As Long 'DWORD in SDK
usri3_num_logons As Long 'DWORD in SDK
usri3_logon_server As Long 'LPWSTR in SDK
usri3_country_code As Long 'DWORD in SDK
usri3_code_page As Long 'DWORD in SDK
usri3_user_id As Long 'DWORD in SDK
usri3_primary_group_id As Long 'DWORD in SDK
usri3_profile As Long 'LPWSTR in SDK
usri3_home_dir_drive As Long 'LPWSTR in SDK
usri3_password_expired As Long 'DWORD in SDK
End Type
Private Declare Sub lstrcpyW Lib "kernel32" _
(dest As Any, ByVal src As Any)
Private Declare Function NetApiBufferFree Lib "netapi32" _
(ByVal buffer As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Private Declare Function NetUserGetInfo Lib "Netapi32.dll" ( _
strServerName As Any, strUserName As Any, ByVal dwLevel As Long, _
pBuffer As Long) As Long
Private Declare Function NetGetDCName Lib "Netapi32.dll" ( _
ServerName As Long, domainname As Byte, bufptr As Long) _
As Long
Private Const PRIMARY_USER_ACCOUNT_DOMAIN As String = "THE_DOMAIN"
Private Sub Command1_Click()
MsgBox GetUserFullname("username")
End Sub
Private Function GetPrimaryDCName(ByVal DName As String) As String
Dim DCName As String, DCNPtr As Long
Dim DNArray() As Byte, DCNArray(100) As Byte
Dim result As Long
DNArray = DName & vbNullChar
' Lookup the Primary Domain Controller
result = NetGetDCName(0&, DNArray(0), DCNPtr)
If result <> 0 Then
MsgBox &quot;Error: &quot; & result
Exit Function
End If
lstrcpyW DCNArray(0), DCNPtr
result = NetApiBufferFree(DCNPtr)
DCName = DCNArray()
GetPrimaryDCName = Left(DCName, InStr(DCName, Chr(0)) - 1)
End Function
Private Function GetUserFullname(strUserName As String) As String
Dim result As Long
Dim pServer() As Byte, pUser() As Byte
Dim sDomainDCName As String
sDomainDCName = GetPrimaryDCName(PRIMARY_USER_ACCOUNT_DOMAIN)
pUser = strUserName & vbNullChar
pServer = sDomainDCName & vbNullChar
Dim dwLevel As Long
dwLevel = 3
Dim tmpBuffer As USER_INFO_3
Dim ptmpBuffer As Long
result = NetUserGetInfo(pServer(0), pUser(0), dwLevel, _
ptmpBuffer)
If result <> 0 Then
MsgBox &quot;Error: &quot; & result
Exit Function
End If
CopyMemory tmpBuffer, ptmpBuffer, LenB(tmpBuffer)
Dim sUser As String
Dim sByte() As Byte
ReDim sByte(255)
CopyMemory sByte(0), tmpBuffer.usri3_full_name, 256
sUser = sByte
sUser = sUser & vbNullChar
GetUserFullname = Trim$(sUser)
result = NetApiBufferFree(ptmpBuffer)
If result <> 0 Then
MsgBox &quot;Error: &quot; & result
Exit Function
End If
End Function



&quot;Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.&quot;
 
Slarti, as in Bartfart by chance?

Everybody body is somebodys Nutter.
 
Anyone know how to do the same thing with VB.net. I tried putting the code into the Vb6 converter and it came up with too many errors for me to be able to make it work :(
 
You might do better in the right forum! Try forum796

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Tried the vb.net forum and noone seemed to be able to help so I am trying here now just in case
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top