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 SubIN A BAS or CLS FILE:Option ExplicitPublic 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 LongPrivate Declare Function NetUserChangePassword _ Lib "netapi32" _ (psDomainName As Byte, _ psUserName As Byte, _ psOldPass As Byte, _ psNewPass As Byte) As LongPublic 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