VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CUser"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Declare Function NetUserChangePassword _
    Lib "Netapi32.dll" ( _
    ByVal domainname As String, _
    ByVal Username As String, _
    ByVal OldPassword As String, _
    ByVal NewPassword As String) _
    As Long

Private Declare Function NetUserDel _
    Lib "Netapi32.dll" ( _
    ByVal Servername As String, _
    ByVal Username As String) _
    As Long

Private Declare Function NetUserSetInfo _
    Lib "Netapi32.dll" ( _
    ByVal Servername As String, ByVal Username As String, ByVal Level As Long, _
    UserInfo As Any, ParmError As Long) _
    As Long
    
Private Type USER_INFO_3
   usri3_name              As Long
   usri3_password          As Long
   usri3_password_age      As Long
   usri3_priv              As Long
   usri3_home_dir          As Long
   usri3_comment           As Long
   usri3_flags             As Long
   usri3_script_path       As Long
   usri3_auth_flags        As Long
   usri3_full_name         As Long
   usri3_usr_comment       As Long
   usri3_parms             As Long
   usri3_workstations      As Long
   usri3_last_logon        As Long
   usri3_last_logoff       As Long
   usri3_acct_expires      As Long
   usri3_max_storage       As Long
   usri3_units_per_week    As Long
   usri3_logon_hours       As Long
   usri3_bad_pw_count      As Long
   usri3_num_logons        As Long
   usri3_logon_server      As Long
   usri3_country_code      As Long
   usri3_code_page         As Long
   usri3_user_id           As Long
   usri3_primary_group_id  As Long
   usri3_profile           As Long
   usri3_home_dir_drive    As Long
   usri3_password_expired  As Long
End Type
    
Public Function ChangePassword(ByVal strUsername As String, strDomain As String, strOldPwl As String, strNewPwl As String) As Long
     
    Dim sServer As String, sUser As String
    Dim sNewPass As String, sOldPass As String
    Dim UI1003 As USER_INFO_1003
    Dim dwLevel As Long
    Dim lRet As String
    Dim sNew As String
    Dim thisnet As New CNetwork
    Set thisnet = New CNetwork
     
    sUser = StrConv(strUsername, vbUnicode)
    sNewPass = StrConv(strNewPwl, vbUnicode)
    
    If Left(strDomain, 2) = "\\" Then
      sServer = StrConv(strDomain, vbUnicode)
    Else
      sServer = StrConv(thisnet.GetPrimaryDCName(strDomain), vbUnicode)
    End If
     
    If strOldPwl = "" Then
      ' Administrative over-ride of existing password. Does not require old password
      dwLevel = 1003
      sNew = strNewPwl
      UI1003.usri1003_password = StrPtr(sNew)
      lRet = NetUserSetInfo(sServer, sUser, dwLevel, UI1003, 0&)
    Else
      sOldPass = StrConv(strOldPwl, vbUnicode)
      lRet = NetUserChangePassword(sServer, sUser, sOldPass, sNewPass)
    End If
    
    ChangePassword = lRet
    
End Function

Public Function DelUser(strServername As String, strUsername As String)
    DelUser = NetUserDel(StrConv(strServername, vbUnicode), StrConv(strUsername, vbUnicode))
End Function


