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

Get User Name

Status
Not open for further replies.

Hackster

Programmer
Mar 28, 2001
173
US
Much to my dismay and horror, I find that Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long doesn't work on Windows 2000, or at least the code I have doesn't work.

How do you get the logged in User Account on a Windows 2000 machine?
 
For Windows 2000 and later, you should use the GetUserNameEx call. This will not work with Win9X tho.

Private Enum EXTENDED_NAME_FORMAT
NameUnknown = 0
NameFullyQualifiedDN = 1
NameSamCompatible = 2
NameDisplay = 3
NameUniqueId = 6
NameCanonical = 7
NameUserPrincipal = 8
NameCanonicalEx = 9
NameServicePrincipal = 10
End Enum

Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long

Private Sub Form_Load()

Dim sBuffer As String, Ret As Long
sBuffer = String(256, 0)
Ret = Len(sBuffer)
If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
MsgBox &quot;Username: &quot; + Left$(sBuffer, Ret)
Else
MsgBox &quot;Error while retrieving the username&quot;
End If
End Sub

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Thank you very much. I really appreciate that.

Does the same hold true for GetComputerName? I use this API a lot as well.
 
To the best of my knowledge, GetComputerName does not have issues with 2000. There is a GetComputerNameEx which has added functionality in that it can also get host and domain DNS names.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Yes, the standard GetUserName works fine for me in win 2K
Here is the code I use:

Public Declare Function GetUserName Lib &quot;advapi32.dll&quot; Alias &quot;GetUserNameA&quot; (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetUserLogon() As String
' Display the name of the user currently logged on.
Dim strUser As String ' receives name of the user
Dim lngLength As Long ' length of the string
Dim lngReturn As Long ' return value

' Create room in the buffer to receive the returned string.
strUser = space(255) ' room for 255 characters
lngLength = 255 ' initialize the size of the string
' Get the user's name and display it.
lngReturn = GetUserName(strUser, lngLength) ' length is now the length of the returned string
strUser = Left(strUser, lngLength - 1) ' extract the returned info from the buffer
' (We subtracted one because we don't want the null character in the trimmed string.)
GetUserLogon = strUser
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top