Solved

Getting user name

Posted on 1998-08-19
4
204 Views
Last Modified: 2012-08-13
How do I retrieve the full user name from an NT environment?  
I've tried using Environ("username") and WNetGetUser but this only retrieves the logon id whereas I want the full user name associated with that id.
0
Comment
Question by:saffron
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
waty earned 30 total points
ID: 1429807
Here are functions to retrieve what you want

' *** Get computer and user information
Private Declare Function GetComputerNameA Lib "Kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserNameA Lib "advapi32.dll" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function WNetGetUser Lib "mpr" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long

Public Function GetComputerName() As String
   ' *** Get the computer name
   
   Dim UserName         As String * 255

   Call GetComputerNameA(UserName, 255)
   GetComputerName = Left$(UserName, InStr(UserName, Chr$(0)) - 1)

End Function

Public Function GetUserName() As String
   ' *** Get the user name
   
   Dim UserName         As String * 255

   Call GetUserNameA(UserName, 255)
   GetUserName = Left$(UserName, InStr(UserName, Chr$(0)) - 1)

End Function

Public Function NetUserName() As String
   ' *** Returns the user name or "" if the user is not logged on.
   Dim i As Long
   Dim UserName As String * 255

   i = WNetGetUser("", UserName, 255)
   
   If i = 0 Then
      NetUserName = Left$(UserName, InStr(UserName, Chr$(0)) - 1)
   Else
      NetUserName = ""
   End If
   
End Function

0
 

Author Comment

by:saffron
ID: 1429808
These functions are useful but they still only return the user id.  Ie my logon id onto Windows NT is 'SN1' which I can retrieve using your functions but the value I need is "Saffron Newell" - my full user name.  I believe that NetUserGetInfo can return this if I want level 2 info returned but I haven't had much luck getting this to work.  Is this the API call I need and if it is how do I use it?
0
 
LVL 14

Expert Comment

by:waty
ID: 1429809
Try this class to have more informations :

Option Explicit

'
' Win32 APIs to determine OS information.
'
Private Declare Function GetVersionEx Lib "KERNEL32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Type OSVERSIONINFO
   dwOSVersionInfoSize As Long
   dwMajorVersion As Long
   dwMinorVersion As Long
   dwBuildNumber As Long
   dwPlatformId As Long
   szCSDVersion As String * 128
End Type
Private Const VER_PLATFORM_WIN32s = 0
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT = 2

'
' Win32 NetAPIs.
'
Private Declare Function NetWkstaGetInfo Lib "Netapi32.dll" (lpServer As Any, ByVal Level As Long, lpBuffer As Any) As Long
Private Declare Function NetWkstaUserGetInfo Lib "Netapi32.dll" (ByVal Reserved As Any, ByVal Level As Long, lpBuffer As Any) As Long
Private Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Long) As Long

'
' Data handling APIs
'
Private Declare Sub CopyMem Lib "KERNEL32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function lstrlenW Lib "KERNEL32" (ByVal lpString As Long) As Long
Private Declare Function lstrcpyW Lib "KERNEL32" (lpString1 As Byte, ByVal lpString2 As Long) As Long

Private Type WKSTA_INFO_102
   wki102_platform_id As Long
   wki102_computername As Long
   wki102_langroup As Long
   wki102_ver_major As Long
   wki102_ver_minor As Long
   wki102_lanroot As Long
   wki102_logged_on_users As Long
End Type

Private Type WkstaInfo102
   PlatformId As Long
   ComputerName As String
   LanGroup As String
   VerMajor As Long
   VerMinor As Long
   LanRoot As String
   LoggedOnUsers As Long
End Type

Private Type WKSTA_USER_INFO_1
   wkui1_username As Long
   wkui1_logon_domain As Long
   wkui1_oth_domains As Long
   wkui1_logon_server As Long
End Type

Private Type WkstaUserInfo1
   UserName As String
   LogonDomain As String
   OtherDomains As String
   LogonServer As String
End Type

Private Const NERR_Success As Long = 0&
'
' Member variables
'
Private m_Wks As WkstaInfo102
Private m_User As WkstaUserInfo1
Private m_IsWinNT As Boolean
   
' *********************************************************
'  Initialization
' *********************************************************
Private Sub Class_Initialize()
   Dim os As OSVERSIONINFO
   '
   ' Check to make sure we're running NT!
   '
   os.dwOSVersionInfoSize = Len(os)
   Call GetVersionEx(os)
   If os.dwPlatformId = VER_PLATFORM_WIN32_NT Then
      m_IsWinNT = True
      Me.Refresh
   End If

   SocketsInitialize

End Sub

' *********************************************************
'  Public Properties (Workstation)
' *********************************************************
Public Property Get ComputerName() As String
   ComputerName = m_Wks.ComputerName
End Property

Public Property Get Domain() As String
   Domain = m_Wks.LanGroup
End Property

Public Property Get LanRoot() As String
   LanRoot = m_Wks.LanRoot
End Property

Public Property Get LoggedOnUsers() As Long
   LoggedOnUsers = m_Wks.LoggedOnUsers
End Property

Public Property Get PlatformId() As Long
   PlatformId = m_Wks.PlatformId
End Property

Public Property Get VerMajor() As Long
   VerMajor = m_Wks.VerMajor
End Property

Public Property Get VerMinor() As Long
   VerMinor = m_Wks.VerMinor
End Property

' *********************************************************
'  Public Properties (Workstation User)
' *********************************************************
Public Property Get LogonDomain() As String
   LogonDomain = m_User.LogonDomain
End Property

Public Property Get LogonServer() As String
   LogonServer = m_User.LogonServer
End Property

Public Property Get OtherDomains() As String
   OtherDomains = m_User.OtherDomains
End Property

Public Property Get UserName() As String
   UserName = m_User.UserName
End Property

Public Property Get IPAdress() As String
   IPAdress = GetIPAdress()
End Property

' *********************************************************
'  Public Methods
' *********************************************************
Public Sub Refresh()
   Dim lpBuffer As Long
   Dim nRet As Long
   Dim wki As WKSTA_INFO_102
   Dim wkui As WKSTA_USER_INFO_1
   '
   ' These functions only exist in Windows NT!!!
   '
   If Not m_IsWinNT Then Exit Sub
   '
   ' Obtain workstation information
   '
   nRet = NetWkstaGetInfo(ByVal 0&, 102&, lpBuffer)
   If nRet = NERR_Success Then
      '
      ' Transfer data to VB-friendly structure
      '
      CopyMem wki, ByVal lpBuffer, Len(wki)
      m_Wks.PlatformId = wki.wki102_platform_id
      m_Wks.ComputerName = PointerToStringW(wki.wki102_computername)
      m_Wks.LanGroup = PointerToStringW(wki.wki102_langroup)
      m_Wks.VerMajor = wki.wki102_ver_major
      m_Wks.VerMinor = wki.wki102_ver_minor
      m_Wks.LanRoot = PointerToStringW(wki.wki102_lanroot)
      m_Wks.LoggedOnUsers = wki.wki102_logged_on_users
      '
      ' Clean up
      '
      If lpBuffer Then
         Call NetApiBufferFree(lpBuffer)
      End If
   End If
   '
   ' Obtain user information for this workstation
   '
   nRet = NetWkstaUserGetInfo(0&, 1&, lpBuffer)
   If nRet = NERR_Success Then
      '
      ' Transfer data to VB-friendly structure
      '
      CopyMem wkui, ByVal lpBuffer, Len(wkui)
      m_User.UserName = PointerToStringW(wkui.wkui1_username)
      m_User.LogonDomain = PointerToStringW(wkui.wkui1_logon_domain)
      m_User.OtherDomains = PointerToStringW(wkui.wkui1_oth_domains)
      m_User.LogonServer = PointerToStringW(wkui.wkui1_logon_server)
      '
      ' Clean up
      '
      If lpBuffer Then
         Call NetApiBufferFree(lpBuffer)
      End If
   End If
End Sub

' *********************************************************
'  Private Methods
' *********************************************************
Private Function PointerToStringW(lpStringW As Long) As String
   Dim buffer() As Byte
   Dim nLen As Long
   
   If lpStringW Then
      nLen = lstrlenW(lpStringW) * 2
      If nLen Then
         ReDim buffer(0 To (nLen - 1)) As Byte
         CopyMem buffer(0), ByVal lpStringW, nLen
         PointerToStringW = buffer
      End If
   End If
End Function

Private Sub Class_Terminate()
   SocketsCleanup
End Sub

0
 

Author Comment

by:saffron
ID: 1429810
Thanks for your help but the 2nd answer still didn't allow me access to the user's full name.  However I found the actual example on the internet from which you 'lifted' the code and a corresponding file had an example for NetWkstaUserGetInfo which retrieves the full name.  I used this in combination with the other example you put forth and I can now retrieve the full user name.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now