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.

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

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, LUsage: %s \\\\ServerName UserName OldPassword NewPassword\n, argv[0]);
wprintf(LExample: %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(LNetUserChangePassword() is OK!\n);
fwprintf_s(stderr, LUser password has been changed successfully\n);
}
// Otherwise, print the system error.
else
{
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.

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