Visual Basic Network Services
NetUserGetInfo: Get User Password Age
     
Posted:   Sunday December 19, 2004
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   Windows NT4 / Windows 2000, Windows XP, Windows 2003
Author:   VBnet - Randy Birch
     

Related:  

LookupAccountSid: Determine if the Current Process is Running Under Admin Account
NetServerEnum: Get Password Age All Users on the Specified Machine
LookupAccountName: Verify a User's Account
     
 Prerequisites
One of the operating systems listed under OS Restrictions above.

NetUserGetInfo accepts a machine name, username and returns a number of items about the user.

Using the USER_INFO_1 structure, one of the items returned is a value representing the password age as the number of seconds elapsed since the usri1_password member was last changed, or in other words, since the user last changed his password.

The illustrations rotate to show three users on my machine. Because the time is returned in seconds, division by 86400 gives the age in days. The demo can be changed to use integer division (\) if the decimal portion of the age is not required.

The load event takes care of setting up the form; just enter a valid username for the local machine.  To retrieve the password age for user's on another machine, see NetServerEnum: Get Password Age All Users on the Specified Machine.

 BAS Module Code
None.

 Form Code
Add a single command button (Command1), a text box (Text1) and two labels (Label1, Label2) to a form along with the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const NERR_SUCCESS As Long = 0&

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

Private Declare Function NetUserGetInfo Lib "Netapi32" _
  (Servername As Byte, _
   Username As Byte, _
   ByVal Level As Long, _
   Buffer As Long) As Long

Private Declare Function NetApiBufferFree Lib "Netapi32" _
  (ByVal Buffer As Long) As Long

Private Declare Sub CopyMemory Lib "Kernel32" _
   Alias "RtlMoveMemory" _
  (pTo As Any, _
   uFrom As Any, _
   ByVal lSize As Long)



Private Sub Form_Load()

   With Label1
      .Caption = "The password for"
      .AutoSize = True
      .Move 200, 400
   End With
   
   With Text1
      .Text = "(enter a user name)"
      .Move 1550, 360, 1600, 285
   End With
   
   With Label2
      .Caption = "is xxxxxxx days old"
      .AutoSize = True
      .Move 3200, 400
   End With
      
   With Command1
      .Caption = "Get Password Age"
      .Move 1500, 800, 1600
   End With
   
End Sub


Private Sub Command1_Click()

  'if sServer is "" use the local machine
   Label2.Caption = "is " & GetPasswordAge("", Text1.Text)  & " days old"
   
End Sub


Private Function GetPasswordAge(sServer As String, _
                                sUser As String) As Single

   Dim buff As Long
   Dim bServer() As Byte
   Dim bUser() As Byte
   Dim ui1 As USER_INFO_1
  
   bUser = sUser & vbNullChar
   bServer = vbNullChar
   
   If NetUserGetInfo(bServer(0), bUser(0), 1, buff) = NERR_SUCCESS Then
      
      CopyMemory ui1, ByVal buff, Len(ui1)
      GetPasswordAge = (ui1.usri1_password_age / 86400)
      
   End If
   
   NetApiBufferFree buff
   
End Function
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter