Back to the WFC main page

CNetworkUsers : CNetwork

$Revision: 24 $

Description

This class allows you to add, delete or enumerate user accounts in Win32. It will also create computer accounts.

Constructors

CNetworkUsers()
CNetworkUsers( LPCTSTR machine_name )
Creates the object and let's you specify which machine to execute on (i.e. which machine's accounts you want to play with).

Methods

BOOL Add( const CNetworkUserInformation& user_to_add )
Creates a new user account with the options you specify. The return value will be TRUE if the account was created, FALSE otherwise.
void Close( void )
Closes the connection with the machine and cleans up any internal buffers used.
BOOL CreateComputerAccount( LPCTSTR computer_name = NULL, DWORD type = accountMachine )
Creates a computer account. If computer_name is NULL, the name of the computer where this function executes will be used. type can be one of the following:
  • accountBackupDomainController
  • accountInterdomain
  • accountMachine
The return value will be TRUE if the account was created, FALSE otherwise. CreateComputerAccount() uses the method described in the Microsoft Knowledge Base article Q136867 to create the account.
BOOL Delete( const CNetworkUserInformation& user_to_delete )
BOOL Delete( const CString& user_to_delete )
Deletes a user's account. The return value will be TRUE if the account was deleted, FALSE otherwise.
BOOL Enumerate( void )
Initializes the object for enumeration.
DWORD GetLevel( void ) const
When Enumerate() has successfully been called, this method will return the level of information it is returning. If it returns 0xFFFFFFFF, then it has failed. The return value maps to the USER_INFO_xxx data structures. For example, if GetLevel() returns 3, it means it is retrieving USER_INFO_3 data structures from the server.
BOOL GetNext( CNetworkUserInformation& information )
Retrieves the next CNetworkUserInformation. When you reach the end of the list, GetNext() will return FALSE.

Example

#include <wfc.h>
#pragma hdrstop

void test_CNetworkUsers( LPCTSTR machine_name )
{
   WFCLTRACEINIT( TEXT( "test_CNetworkUsers()" ) );

   CNetworkUsers users( machine_name );

   CNetworkUserInformation user_information;

   if ( users.Enumerate() != FALSE )
   {
      _tprintf( TEXT( "User Information for %s:\n" ), (LPCTSTR) users.GetMachineName() );

      while( users.GetNext( user_information ) != FALSE )
      {
         _tprintf( TEXT( " Name                 - \"%s\"\n" ), (LPCTSTR) user_information.Name          );
         _tprintf( TEXT( " Full Name            - \"%s\"\n" ), (LPCTSTR) user_information.FullName      );
         _tprintf( TEXT( " Comment              - \"%s\"\n" ), (LPCTSTR) user_information.Comment       );
         _tprintf( TEXT( " User Comment         - \"%s\"\n" ), (LPCTSTR) user_information.UserComment   );
         _tprintf( TEXT( " ID                   - %lu\n" ),              user_information.UserID        );
         _tprintf( TEXT( " Flags                - %lX\n" ),              user_information.Flags         );
         _tprintf( TEXT( " Privileges           - %lX\n" ),              user_information.Privileges    );
         _tprintf( TEXT( " Password             - \"%s\"\n" ), (LPCTSTR) user_information.Password      );
         _tprintf( TEXT( " Password Age         - %d Days %d Hours %d Minutes %d seconds\n" ),
                   user_information.PasswordAge.GetDays(),
                   user_information.PasswordAge.GetHours(),
                   user_information.PasswordAge.GetMinutes(),
                   user_information.PasswordAge.GetSeconds() );
         _tprintf( TEXT( " HomeDirectory        - \"%s\"\n" ), (LPCTSTR) user_information.HomeDirectory );
         _tprintf( TEXT( " Script Path          - \"%s\"\n" ), (LPCTSTR) user_information.ScriptPath    );
         _tprintf( TEXT( " Parameters           - \"%s\"\n" ), (LPCTSTR) user_information.Parameters    );
         _tprintf( TEXT( " Workstations         - \"%s\"\n" ), (LPCTSTR) user_information.Workstations  );
         _tprintf( TEXT( " LastLogon            - \"%s\"\n" ), (LPCTSTR) user_information.LastLogon.Format( "%a %d %b %y, %H:%M:%S" ) );
         _tprintf( TEXT( " LastLogoff           - \"%s\"\n" ), (LPCTSTR) user_information.LastLogoff.Format( "%a %d %b %y, %H:%M:%S" ) );
         _tprintf( TEXT( " Account Expires      - \"%s\"\n" ), (LPCTSTR) user_information.AccountExpires.Format( "%a %d %b %y, %H:%M:%S" ) );
         _tprintf( TEXT( " Maximum Storage      - %lu\n" ),              user_information.MaximumStorage     );
         _tprintf( TEXT( " Bad Password Count   - %lu\n" ),              user_information.BadPasswordCount   );
         _tprintf( TEXT( " Number Of Logons     - %lu\n" ),              user_information.NumberOfLogons     );
         _tprintf( TEXT( " Logon Server         - \"%s\"\n" ), (LPCTSTR) user_information.LogonServer        );
         _tprintf( TEXT( " Country Code         - %lX\n" ),              user_information.CountryCode        );
         _tprintf( TEXT( " Code Page            - %lX\n" ),              user_information.CodePage           );
         _tprintf( TEXT( " Primary Group ID     - %lu\n" ),              user_information.PrimaryGroupID     );
         _tprintf( TEXT( " Home Directory Drive - \"%s\"\n" ), (LPCTSTR) user_information.HomeDirectoryDrive );
         _tprintf( TEXT( " Profile              - \"%s\"\n" ), (LPCTSTR) user_information.Profile            );
         _tprintf( TEXT( " Password Has Expired - %lu\n" ),              user_information.PasswordHasExpired );
         _tprintf( TEXT( " Encrypted Password: " ) );

         int index = 0;

         while( index < user_information.EncryptedPassword.GetSize() )
         {
            _tprintf( TEXT( "%02X" ), (int) user_information.EncryptedPassword.GetAt( index ) );
            index++;
         }

         _tprintf( TEXT( "\n\n" ) );
      }
   }
   else
   {
      DWORD error_code = users.GetErrorCode();

      CString error_message;

      Convert_NERR_Code_to_String( error_code, error_message );

      _tprintf( TEXT( "CNetworkUsers.Enumerate( user_information ), ErrorCode == %d \"%s\"\n" ), error_code, (LPCTSTR) error_message );
   }

   user_information.Empty();

   user_information.Name     = TEXT( "Laura" );
   user_information.Password = TEXT( "LovesSammy" );

   user_information.SetAddDefaults();

   if ( users.Add( user_information ) != FALSE )
   {
      _tprintf( TEXT( "Laura Added.\n" ) );
   }
   else
   {
      _tprintf( TEXT( "Can't Add User\n" ) );
   }

   if ( users.Delete( "Laura" ) != FALSE )
   {
      _tprintf( TEXT( "Deleted Laura\n" ) );
   }
   else
   {
      _tprintf( TEXT( "Can't delete Laura\n" ) );
   }
}

API's Used

CNetworkUsers uses the following API's:
Copyright, 2000, Samuel R. Blackburn
$Workfile: CNetworkUser.cpp $
$Modtime: 1/04/00 5:18a $