Platform SDK: Network Management

NetUserChangePassword

The NetUserChangePassword function changes a user's password for a specified network server or domain.

Security Requirements

Windows NT: 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 can change the password for a user account. If logon is not required, a user can change the password for any user account, as long as the user knows the current password.

Windows 2000: If you call this function on a Windows 2000 domain controller that is running Active Directory, access is allowed or denied based on the access-control list (ACL) for the securable object. The default ACL permits only Administrators and account operators to call this function. On a member server or workstation, only Administrators and account operators can call this function. For more information, see Security Requirements for the Network Management Functions.

NET_API_STATUS NetUserChangePassword(
  LPCWSTR domainname,  
  LPCWSTR username,    
  LPCWSTR oldpassword, 
  LPCWSTR newpassword  
);

Parameters

domainname
[in] Pointer to a constant null-terminated Unicode string that specifies the name of a remote server or domain. The NetUserChangePassword function changes the user's password on the specified remote server or domain.

If this parameter is NULL, the logon domain of the caller is used.

username
[in] Pointer to a constant null-terminated Unicode string that specifies a user name. The NetUserChangePassword function changes the password for the specified user.

If this parameter is NULL, the logon name of the caller is used.

oldpassword
[in] Pointer to a constant null-terminated Unicode string that specifies the user's old password.
newpassword
[in] Pointer to a constant null-terminated Unicode string that specifies the user's new password.

Return Values

If the function succeeds, the return value is NERR_Success.

If the function fails, the return value can be one of the following error codes.

Value Meaning
ERROR_ACCESS_DENIED The user does not have access to the requested information.
ERROR_INVALID_PASSWORD The user has entered an invalid password.
NERR_InvalidComputer The computer name is invalid.
NERR_NotPrimary The operation is allowed only on the primary domain controller of the domain.
NERR_UserNotFound The user name could not be found.
NERR_PasswordTooShort The password is shorter than required. (The password could also be too long, be too recent in its change history, not have enough unique characters, or not meet another password policy requirement.)

Remarks

The following code sample demonstrates how to change a user's password with a call to the NetUserChangePassword function. All parameters to the function are required.

#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <windows.h> 
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
   DWORD dwError = 0;
   NET_API_STATUS nStatus;
   //
   // All parameters are required.
   //
   if (argc != 5)
   {
      fwprintf(stderr, L"Usage: %s \\\\ServerName UserName OldPassword NewPassword\n", argv[0]);
      exit(1);
   }
   //
   // Call the NetUserChangePassword function.
   //
   nStatus = NetUserChangePassword(argv[1], argv[2], argv[3], argv[4]);
   //
   // If the call succeeds, inform the user.
   //
   if (nStatus == NERR_Success)
      fwprintf(stderr, L"User password has been changed successfully\n");
   //
   // Otherwise, print the system error.
   //
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);

   return 0;
}

See Forcing a User to Change the Logon Password for a code sample that demonstrates how to force a user to change the logon password on the next logon using the NetUserGetInfo and NetUserSetInfo functions.

If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management user functions. For more information, see IADsUser and IADsComputer.

Windows 95/98: You can use the PwdChangePassword function to change a user's Windows logon password on Windows 95 and Windows 98. The PwdChangePassword function is defined in the header file Pwdspi.h, which is distributed with the Windows 95 Driver Development Kit (DDK). For more information about PwdChangePassword, see the Windows 95 DDK documentation. For more information about designing a routine to change domain passwords on Windows 95 and Windows 98, as well as a code sample, see the following article in the Microsoft Knowledge Base:

ARTICLE ID: Q177200
TITLE: HOWTO: Programmatically Change Network Password Under Windows 95.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Unsupported.
  Header: Declared in Lmaccess.h; include Lm.h.
  Library: Use Netapi32.lib.

See Also

Network Management Overview, Network Management Functions, User Functions, NetUserSetInfo, NetUserGetInfo