The Win32 Network Management APIs 13






Forcing a User to Change the Logon Password Program Example


This code sample demonstrates how to force a user to change the logon password on the next logon using the NetUserGetInfo() and NetUserSetInfo() functions and the USER_INFO_3 structure. Note that starting with Windows XP, it is recommended that you use the USER_INFO_4 structure instead. Set the usri3_password_expired member of the USER_INFO_3 structure to a nonzero value using the following code example.

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


Forcing a User to Change the Logon Password Program Example: Creating new C++ Win32 console application project


Then, add the source file. Give it a name.


Forcing a User to Change the Logon Password Program Example: Adding new C++ source file


Then, add the following source code.


#include <windows.h>

// INCL_NET includes all LAN Manager headers if necessary

// #define INCL_NET

#include <stdio.h>

#include <lm.h>


// Use #pragma if you do not include the library in the VS project setting

#pragma comment(lib, "netapi32.lib")


// This is just a local user for testing. Change accordingly...

#define USERNAME L"mike spoon"

#define SERVER L"\\\\nazuri"


int wmain(void)


    PUSER_INFO_3 pUsr = NULL;

    NET_API_STATUS netRet = 0;

    DWORD dwParmError = 0;


      // First, retrieve the user information at level 3. This is

      // necessary to prevent resetting other user information when the NetUserSetInfo call is made.

      netRet = NetUserGetInfo(SERVER, USERNAME, 3, (LPBYTE *)&pUsr);


      if(netRet == NERR_Success)


            wprintf(L"NetUserGetInfo() is OK, return values is %d\n", netRet);

            // The function was successful; set the usri3_password_expired value to

            // a nonzero value. Call the NetUserSetInfo() function.

            pUsr->usri3_password_expired = TRUE;

            netRet = NetUserSetInfo( SERVER, USERNAME, 3, (LPBYTE)pUsr, &dwParmError);


            // A zero return indicates success.

            // If the return value is ERROR_INVALID_PARAMETER,

            //  the dwParmError parameter will contain a value indicating the

            //  invalid parameter within the user_info_3 structure. These values are defined in the lmaccess.h file.

            if(netRet == NERR_Success)

                  wprintf(L"You %s will need to change password at next logon!\n", USERNAME);


                  wprintf(L"Error %d occurred. Parm Error %d returned.\n", netRet, dwParmError);


            // Must free the buffer returned by NetUserGetInfo().




            wprintf(L"NetUserGetInfo() failed, error %d\n", netRet);


      return 0;



Build and run the project. The following screenshot is a sample output.


Forcing a User to Change the Logon Password Program Example: A sample console program output





< Win32 Network Management APIs 12 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 14 >