Get Win95/NT username

By | 2002-06-01

95/NT username
“Joseph P. Fisher”

Original Author: Newsgroup Posting

API Declarations

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

Code

gsUserId = ClipNull(GetUser())

Function GetUser() As String
  Dim lpUserID As String
  Dim nBuffer As Long
  Dim Ret As Long
  
  lpUserID = String(25, 0)
  nBuffer = 25
  Ret = GetUserName(lpUserID, nBuffer)
  If Ret Then
  GetUser$ = lpUserID$
  End If
End Function
  
Function ClipNull(InString As String) As String
  Dim intpos As Integer
  If Len(InString) Then
   intpos = InStr(InString, vbNullChar)
   If intpos > 0 Then
    ClipNull = Left(InString, intpos - 1)
   Else
    ClipNull = InString
   End If
  End If
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.