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

Getting user name from logon id

Status
Not open for further replies.

Jonathan

Programmer
Mar 19, 1999
116
GB
Is there any way, given a users NT logon ID, to obtain their full name? I've looked through the Win 32 API reference, but nothing leapt out at me... does anyone know how to do it?

Thanks
Jonathan
jon_george1@hotmail.com

Working against: Visual Basic, Access, Visual Interdev, VBScript, JavaScript, Active Server Pages, SQL Server, Oracle, XML
 
You might try the following....
[tt]
Const NERR_Success = 0
Const CP_ACP = 0
Private Type USER_INFO_3
usri3_name As Long
usri3_password As Long
usri3_password_age As Long
usri3_priv As Long
usri3_home_dir As Long
usri3_comment As Long
usri3_flags As Long
usri3_script_path As Long
usri3_auth_flags As Long
usri3_full_name As Long
usri3_usr_comment As Long
usri3_parms As Long
usri3_workstations As Long
usri3_last_logon As Long
usri3_last_logoff As Long
usri3_acct_expires As Long
usri3_max_storage As Long
usri3_units_per_week As Long
usri3_logon_hours As Byte
usri3_bad_pw_count As Long
usri3_num_logons As Long
usri3_logon_server As String
usri3_country_code As Long
usri3_code_page As Long
usri3_user_id As Long
usri3_primary_group_id As Long
usri3_profile As Long
usri3_home_dir_drive As Long
usri3_password_expired As Long
End Type
Private Declare Function GetUserName _
Lib "advapi32.dll" _
Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long) As Long
Private Declare Function NetUserGetInfo _
Lib "netapi32" _
(ByVal servername As String, _
ByVal username As String, _
ByVal level As Long, _
bufptr As Long) As Long
Private Declare Function NetApiBufferFree _
Lib "netapi32" _
(ByVal Buffer As Long) As Long
Private Declare Sub MoveMemory _
Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal dwLength As Long)
Private Declare Function lstrlenW _
Lib "kernel32" _
(lpString As Any) As Long
Private Declare Function WideCharToMultiByte _
Lib "kernel32" _
(ByVal codepage As Long, _
ByVal dwFlags As Long, _
lpWideCharStr As Any, _
ByVal cchWideChar As Long, _
lpMultiByteStr As Any, _
ByVal cchMultiByte As Long, _
ByVal lpDefaultChar As String, _
ByVal lpUsedDefaultChar As Long) As Long
Public Function GetStrFromPtrW(lpszW As Long) _
As String
Dim sRtn As String
sRtn = String$(lstrlenW(ByVal lpszW) * 2, 0)
Call WideCharToMultiByte(CP_ACP, 0, _
ByVal lpszW, -1, ByVal sRtn, Len(sRtn), 0, 0)
GetStrFromPtrW = GetStrFromBufferA(sRtn)
End Function
Public Function GetStrFromBufferA(sz As String) _
As String
If InStr(sz, vbNullChar) Then
GetStrFromBufferA = Left$(sz, _
InStr(sz, vbNullChar) - 1)
Else
GetStrFromBufferA = sz
End If
End Function
Private Sub Form_Load()
Dim lpBuf As Long
Dim ui3 As USER_INFO_3
Dim strUserName As String
Dim strFullUserName As String
strUserName = String(100, Chr$(0))
GetUserName strUserName, 100
strUserName = Left$(strUserName, _
InStr(strUserName, Chr$(0)) - 1)
If NetUserGetInfo("", _
StrConv(strUserName, vbUnicode), _
3, lpBuf) = _
NERR_Success Then
Call MoveMemory(ui3, _
ByVal lpBuf, Len(ui3))
MsgBox GetStrFromPtrW(ui3.usri3_name) & _
" is " _
& GetStrFromPtrW(ui3.usri3_full_name)
Call NetApiBufferFree(ByVal lpBuf)
End If
End Sub
[/tt]

Since there may not be a requirement to record a full name in a user account, you may have equal luck with....
[tt]
Private Declare Function GetUserName _
Lib "advapi32.dll" _
Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long) As Long
Private Sub Form_Load()
Dim strUserName As String, strFullUserName As String
strUserName = String(100, Chr$(0))
GetUserName strUserName, 100
strUserName = Left$(strUserName, _
InStr(strUserName, Chr$(0)) - 1)
strFullUserName = InputBox(strUserName & _
", please enter your full name: ", _
"Missing important information!")
MsgBox "Thank you, " & strFullUserName
End Sub
[/tt]

Not really joking...
VCA.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top