Search Tools Links Login

Get the Username of the Logged On User


This function will allow you to easily get the username of the currently logged on user.

Just pop it in to a module, and you're good to go.

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function UserName() As String
   Dim cn As String
   Dim ls As Long
   Dim res As Long

   cn = String(1024, 0)
   ls = 1024
   res = GetUserName(cn, ls)
   If res <> 0 Then
      UserName = Mid(cn, 1, InStr(cn, Chr(0)) - 1)
   Else
      UserName = ""
   End If
End Function

About this post

Posted: 2019-08-22
By: AndreaTincani
Viewed: 207 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Special Instructions

This code originally appeared on AndreaVB.com, and has been republished here with the permission of Andrea Tincani.


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.