dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: NT
Expertise: Beginner
Oct 21, 1998

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Changing Windows NT Password Progmatically

Question:
How can I change my Windows NT server password through a program?

Answer:
Actually, this is rather simple. However, heed the following precaution from the MSDN documentation on this function:

"A server or domain can be configured to require a user to log on before changing the password on a user account. In that case, only members of the Administrators or Account Operators local group or the user himself can change the password for a user account. If logging on is not required, the user can change the password for any user account, as long as the user knows the current password."


IN A FORM:
Private Sub Command1_Click()
   Dim p_strUserName                As String
   Dim p_strOldPass                 As String
   Dim p_strNewPass                 As String
   Dim p_blnRtn                     As Boolean
   
   p_strUserName = Me.txtUserName.Text
   p_strOldPass = Me.txtOldPassword.Text
   p_strNewPass = Me.txtNewPassword.Text
   
   p_blnRtn = Module1.SetPassword("", _
                                  p_strUserName, _
                                  p_strOldPass, _
                                  p_strNewPass)
   If p_blnRtn = False Then
      MsgBox "Failed to reset the password for '" & _
         p_strUserName & "'."
   Else
      MsgBox "Successfully changed password!"
   End If
   
End Sub

IN A BAS or CLS FILE:
Option Explicit

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 NetUserChangePassword _
   Lib "netapi32" _
   (psDomainName As Byte, _
    psUserName As Byte, _
    psOldPass As Byte, _
    psNewPass As Byte) As Long

Public Function SetPassword(ByVal DomainName As String, _
                            ByVal UserName As String, _
                            ByVal OldPassword As String, _
                            ByVal NewPassword As String) As Boolean
   Dim p_lngRtn                        As Long
   Dim p_bytDomainName()               As Byte
   Dim p_bytUserName()                 As Byte
   Dim p_bytOldPassword()              As Byte
   Dim p_bytNewPassword()              As Byte
   Dim p_strNewPassword                As String
   
   ' Convert the domain name to a byte array
   If Len(Trim$(DomainName)) = 0 Then
      p_bytDomainName = vbNullChar
   Else
      p_bytDomainName = DomainName & vbNullChar
   End If
   
   p_bytUserName = UserName & vbNullChar
   p_bytOldPassword = OldPassword & vbNullChar
   p_bytNewPassword = NewPassword & vbNullChar
   
   p_lngRtn = NetUserChangePassword(p_bytDomainName(0), _
                                    p_bytUserName(0), _
                                    p_bytOldPassword(0), _
                                    p_bytNewPassword(0))
   If p_lngRtn = 0 Then
      SetPassword = True
   Else
      SetPassword = False
   End If
   
   UserName = p_bytUserName
   OldPassword = p_bytOldPassword
   p_strNewPassword = p_bytNewPassword
   If Right$(p_strNewPassword, 1) = vbNullChar Then
      p_strNewPassword = Mid$(p_strNewPassword, 1, Len(p_strNewPassword) - 1)
   End If
   
   If p_strNewPassword <> NewPassword Then
      SetPassword = False
   End If
   
End Function
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date