The Win32 Network Management APIs 32





NetUserChangePassword() Program Example


The NetUserChangePassword() function changes a user's password for a specified network server or domain. 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.

Create a new empty Win32 console application project. Give the project name and change the project location is needed.


NetUserChangePassword() Program Example: Creating new Win32 C++ empty console application project


Then, add the source file and give it a suitable name.


NetUserChangePassword() Program Example: Adding new C++ source file for C++ source code

Then, add the following source code.


#ifndef UNICODE

#define UNICODE



#pragma comment(lib, netapi32.lib)


#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_s(stderr, LUsage: %s \\\\ServerName UserName OldPassword NewPassword\n, argv[0]);

      wprintf(LExample: %s \\\\MikeServer \Mike Tyson\ 1234 4321\n, argv[0]);




   // 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)


         wprintf(LNetUserChangePassword() is OK!\n);

         fwprintf_s(stderr, LUser password has been changed successfully\n);


   // Otherwise, print the system error.



         wprintf(LNetUserChangePassword() failed!\n);

         fwprintf_s(stderr, LA system error has occurred: %d\n, nStatus);



   return 0;



Build and run the project. The following screenshot is a sample output. Firstly, we set the password to 1234.


NetUserChangePassword() Program Example: Setting new password for the Win XP Pro user


Then we try to change it. Then you may want to log off and re-login using the new password.


NetUserChangePassword() Program Example: A sample program output - changing user password




< Win32 Network Management APIs 31 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 33 >