Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: NT
Expertise: Beginner
Oct 25, 2000

NetUserGetInfo - API Call

I am using "NetUserGetInfo" API call to get the NT user profile. I use:
typedef struct _USER_INFO_3
How can I parse the "usri3_flags" member in detail?

Use this:

Option Explicit

Private Const UserInfo1 As Long = 1&

Private Type USER_INFO_1
   usri1_name              As Long
   usri1_password          As Long
   usri1_password_age      As Long
   usri1_priv              As Long
   usri1_home_dir          As Long
   usri1_comment           As Long
   usri1_flags             As Long
   usri1_script_path       As Long
End Type
Public Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" _
   (pTo As Any, pFrom As Any, ByVal lCount As Long)
Private Declare Function NetApiBufferFree Lib "netapi32.dll" _
   (ByVal lngPtrBuffer As Long) As Long
Private Declare Function NetUserGetInfo Lib "netapi32" _
   (ByVal psServer As Long, ByVal psUser As Long, ByVal lLevel As Long, _
   lngPtrBuffer As Long) As Long

Private Const NERR_Success             As Long = 0&

Private Const UF_ACCOUNTDISABLE        As Long = &H2
Private Const UF_SCRIPT                As Long = &H1&
Private Const UF_HOMEDIR_REQUIRED      As Long = &H8&
Private Const UF_LOCKOUT               As Long = &H10&
Private Const UF_PASSWD_NOTREQD        As Long = &H20&
Private Const UF_PASSWD_CANT_CHANGE    As Long = &H40&
Private Const UF_NORMAL_ACCOUNT        As Long = &H200&
Private Const UF_SERVER_TRUST_ACCOUNT  As Long = &H2000&
Private Const UF_DONT_EXPIRE_PASSWD    As Long = &H10000
Private Const UF_MNS_LOGON_ACCOUNT     As Long = &H20000
Private Const UF_TEMP_DUPLICATE_ACCOUNT      As Long = &H100&
Private Const UF_INTERDOMAIN_TRUST_ACCOUNT   As Long = &H800&
Private Const UF_WORKSTATION_TRUST_ACCOUNT   As Long = &H1000&

Public Sub Main()
   MsgBox GetUserFlags("", "Administrator")
End Sub

Public Function GetUserFlags(ByVal strServerName As String, _
                             ByVal strUserName As String) As String
   Dim typUserInfo1        As USER_INFO_1
   Dim lngBuf              As Long
   Dim lngSvrName          As Long
   Dim lngUserName         As Long
   Dim lngParmErr          As Long
   Dim lngRtn              As Long
   Dim lngFlags            As Long
   Dim blnIsNowDisabled    As Boolean
   Dim strTmp              As String
   ' Convert the server name to a pointer
   If Len(Trim$(strServerName)) = 0 Then
      lngSvrName = 0&
      lngSvrName = StrPtr(strServerName)
   End If
   ' Convert the user name to a pointer
   If Len(Trim$(lngUserName)) = 0 Then
      Exit Function 'Handle the error
      lngUserName = StrPtr(strUserName)
   End If
   ' Get the user's current flags
   lngRtn = NetUserGetInfo(lngSvrName, _
                           lngUserName, _
                           UserInfo1, _
   If lngRtn = NERR_Success Then
      CopyMem typUserInfo1, _
              ByVal lngBuf, _
      If lngBuf Then
         NetApiBufferFree lngBuf
      End If
      MsgBox "Error Number: " & lngRtn
   End If
   lngFlags = typUserInfo1.usri1_flags
   If (lngFlags And UF_SCRIPT) Then
      strTmp = "Script is executed"
      strTmp = "Script is NOT executed"
   End If
   If (lngFlags And UF_ACCOUNTDISABLE) Then
      strTmp = strTmp & vbCrLf & "Account is disabled"
      strTmp = strTmp & vbCrLf & "Account is NOT disabled"
   End If
   If (lngFlags And UF_HOMEDIR_REQUIRED) Then
      strTmp = strTmp & vbCrLf & "Home directory is required"
      strTmp = strTmp & vbCrLf & "Home directory is NOT required"
   End If
   If (lngFlags And UF_PASSWD_NOTREQD) Then
      strTmp = strTmp & vbCrLf & "Password is NOT required"
      strTmp = strTmp & vbCrLf & "Password is required"
   End If
   If (lngFlags And UF_PASSWD_CANT_CHANGE) Then
      strTmp = strTmp & vbCrLf & "User cannot change password"
      strTmp = strTmp & vbCrLf & "User can change password"
   End If

   If (lngFlags And UF_LOCKOUT) Then
      strTmp = strTmp & vbCrLf & "Account is locked out"
      strTmp = strTmp & vbCrLf & "Account is NOT locked out"
   End If
   If (lngFlags And UF_DONT_EXPIRE_PASSWD) Then
      If Len(strTmp) > 0 Then
         strTmp = strTmp & vbCrLf & "Password never expires"
         strTmp = strTmp & vbCrLf & "Password does expire"
      End If
   End If
   GetUserFlags = strTmp
End Function
DevX Pro
Thanks for your registration, follow us on our social networks to keep up-to-date