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

#endif

 

#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, L"Usage: %s \\\\ServerName UserName OldPassword NewPassword\n", argv[0]);

      wprintf(L"Example: %s \\\\MikeServer \"Mike Tyson\" 1234 4321\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)

   {

         wprintf(L"NetUserChangePassword() is OK!\n");

         fwprintf_s(stderr, L"User password has been changed successfully\n");

   }

   // Otherwise, print the system error.

   else

   {

         wprintf(L"NetUserChangePassword() failed!\n");

         fwprintf_s(stderr, L"A 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 >