Platform SDK: Network Management

Forcing a User to Change the Logon Password

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.

Set the usri3_password_expired member of the USER_INFO_3 structure to a nonzero value using the following code fragment:

#define UNICODE
#include <windows.h>
#define INCL_NET
#include <lm.h>

#define USERNAME TEXT("your_user_name")
#define SERVER TEXT(\\\\server)


void main( void )
{
         PUSER_INFO_3 pUsr;
         DWORD netRet = 0, 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, &pUsr);
 if( netRet == NERR_Success )
 {
//
// 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( PDC, USERNAME, 3, 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 )
     printf("User %S will need to change password at next logon", USERNAME);
else printf("Error %d occurred.  Parm Error %d returned.\n", netRet, dwParmError);
//
// Must free the buffer returned by NetUserGetInfo.
//
NetApiBufferFree( pUsr);
}
else printf("NetUserGetInfo failed: %d\n",netRet);