Hello everyone!

We have a good way to change attributes in windows-accounts:
using the function: NetUserSetInfo.
This is article on MSDN-protal is about its issue: http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx

That to change attributes we can use the structure USER_INFO_1008.
And I find it very cool, that the example in its arcticle uses this structure.

The example code changes the attribute UF_ACCOUNTDISABLE - it works very fine.
But I want to change another attribute: UF_PASSWORD_EXPIRED - it works very bad. I can't change this flag =(

If after setting this flag I call the NetUserGetInfo(), the account does not get this flag set.

But I set "password expired" in windows snap-in (means: the password must be changed in the next logon):
After calling NetUserGetInfo() I can see that the account has this flag.

Why doesn't it work?

I have made the evident code, below (MS VS2005, cmd-appl):

Code:
#include "stdafx.h"
#include "windows.h"
#include "atlstr.h"
#include <lm.h>
#include <iostream>
#include "conio.h"

#pragma comment(lib, "Wldap32.lib")
#pragma comment(lib, "netapi32.lib")
using namespace std;

CString GetStrFlags(DWORD nflags);
void PrintUsers();
CString Input(CString str);
void PrintFlagsUser(CString sName);

int _tmain(int argc, _TCHAR* argv[])
{

   CString sName;

   PrintUsers();
   sName = Input(L"Input name ->");
   PrintFlagsUser(sName);

   CString m_sServerName = getenv("COMPUTERNAME");
   DWORD dwLevel = 1008;
   USER_INFO_1008 ui;
   NET_API_STATUS nStatus;

   ui.usri1008_flags = UF_PASSWORD_EXPIRED | UF_SCRIPT;

   nStatus = NetUserSetInfo(m_sServerName, sName, dwLevel, (LPBYTE)&ui,    NULL);

   if (nStatus == NERR_Success)
   {
       fwprintf(stderr, L"Operation complеte:\n", sName);
       PrintFlagsUser(sName);

   }   else  wprintf(L"A system error has occurred: %d\n", nStatus);

   return 0;

}

CString GetStrFlags(DWORD nflags)
{
    CString str;
        str.Format(L"flag: %d\n",nflags);
        if(nflags&UF_SCRIPT) str += L"UF_SCRIPT\n";
        if(nflags&UF_ACCOUNTDISABLE) str += L"UF_ACCOUNTDISABLE\n";
        if(nflags&UF_HOMEDIR_REQUIRED) str += L"UF_HOMEDIR_REQUIRED\n";
        if(nflags&UF_PASSWD_NOTREQD) str += L"UF_PASSWD_NOTREQD\n";
        if(nflags&UF_PASSWD_CANT_CHANGE) str += L"UF_PASSWD_CANT_CHANGE\n";
        if(nflags&UF_LOCKOUT) str += L"UF_LOCKOUT\n";
        if(nflags&UF_DONT_EXPIRE_PASSWD) str += L"UF_DONT_EXPIRE_PASSWD\n";
        if(nflags&UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED) str += L"UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED\n";
        if(nflags&UF_NOT_DELEGATED) str += L"UF_NOT_DELEGATED\n";
        if(nflags&UF_SMARTCARD_REQUIRED) str += L"UF_SMARTCARD_REQUIRED\n";
        if(nflags&UF_USE_DES_KEY_ONLY) str += L"UF_USE_DES_KEY_ONLY\n";
        if(nflags&UF_DONT_REQUIRE_PREAUTH) str += L"UF_DONT_REQUIRE_PREAUTH\n";
        if(nflags&UF_TRUSTED_FOR_DELEGATION) str += L"UF_TRUSTED_FOR_DELEGATION\n";
        if(nflags&UF_PASSWORD_EXPIRED) str += L"UF_PASSWORD_EXPIRED\n";
        if(nflags&UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION) str += L"UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION\n";
        if(nflags&UF_NORMAL_ACCOUNT) str += L"UF_NORMAL_ACCOUNT\n";
        if(nflags&UF_TEMP_DUPLICATE_ACCOUNT) str += L"UF_TEMP_DUPLICATE_ACCOUNT\n";
        if(nflags&UF_WORKSTATION_TRUST_ACCOUNT) str += L"UF_WORKSTATION_TRUST_ACCOUNT\n";
        if(nflags&UF_SERVER_TRUST_ACCOUNT) str += L"UF_SERVER_TRUST_ACCOUNT\n";
        if(nflags&UF_INTERDOMAIN_TRUST_ACCOUNT) str += L"UF_INTERDOMAIN_TRUST_ACCOUNT\n";
    return str;
};

void PrintUsers()
{
    CString m_sServerName = getenv("COMPUTERNAME");

    CString sOutStr;

    DWORD entries_read;
    DWORD total_entries;

    NET_API_STATUS ret_status;

    USER_INFO_0 *ui=NULL;

    ret_status = NetUserEnum(m_sServerName,0,FILTER_NORMAL_ACCOUNT,(LPBYTE*)&ui,MAX_PREFERRED_LENGTH, &entries_read,&total_entries,NULL);

    switch(ret_status)
    {
    case NERR_Success:          sOutStr=L"NERR_Success";            break;
    case ERROR_ACCESS_DENIED:   sOutStr=L"ERROR_ACCESS_DENIED";     break;
    case NERR_InvalidComputer:  sOutStr=L"NERR_InvalidComputer";    break;
    case ERROR_MORE_DATA:       sOutStr=L"ERROR_MORE_DATA";         break;
    default:                    sOutStr=L"NetUserEnum";
    }
    wprintf(L"\n%s\n",sOutStr.GetBuffer());
    if(ret_status==NERR_Success)
    {
        sOutStr.Format(L"Number of accounts: %d of %d\n\n",entries_read, total_entries);

        for(int i = 0; i < entries_read; i++)
        {
            if(ui[i].usri0_name) 
            {
                sOutStr += ui[i].usri0_name;
                sOutStr += L"\n";
            }
        }
    }
    wprintf(L"\n%s\n",sOutStr.GetBuffer());

};

CString Input(CString str)
{
    char buf[250];
    wprintf(str.GetBuffer());
    cin >> buf;
    return buf;

}

void PrintFlagsUser(CString sName)
{
    CString sOutStr;
    CString m_sServerName = getenv("COMPUTERNAME");
    USER_INFO_1 *ui;
    NET_API_STATUS ret_status;
    ret_status = NetUserGetInfo(m_sServerName,sName,1,(LPBYTE*)&ui);
    if(!ret_status)
    {
        sOutStr=GetStrFlags(ui->usri1_flags);
        wprintf(L"\n%s\n",sOutStr.GetBuffer());
    }
}