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 Rhinorhino 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 from Windows 2000

How To

Get user name from Windows 2000

by  randysmid  Posted    (Edited  )
If not already done so, create a new module in the Modules section.

Paste this statement into the General Declarations section of the module:
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long


Paste this code:

Function fWin2KUserName() As String
Dim lngLength As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLength = 255
lngX = apiGetUserName(strUserName, lngLength)
If lngX <> 0 Then
fWin2KUserName = Left$(strUserName, lngLength - 1)
Else
fWin2KUserName = "unknown"
End If
End Function

You can run this function (fWin2KUserName) from anywhere within your application. The best way is to set some variable to be equal to the function:
Dim strUser as String
strUser = fWin2KUserName


Questions? rsmith@cta.org


Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top