I created these classes to make it easy to work with Network Management. For
this I created the CNetDomain
class, the CNetUsers
class,
The CNetGroups
class and the CNetLocalGroups
class.
Construct a CNetDomain object.
CNetDomain();
CNetDomain(CString strDomainName);
Parameters
CString strDomainName
a string that contains the Domain name. If
this parameter is empty, the domain where the machine is logued, is used.
Remarks:
If you use the first construct, the CNetDomain class will work only with the
machine that it is running, getting and updating the information on it
For Example:
CNetDomain pDomain(_T(""));
is logued the Machine...
CNetDomain pDomain(_T("Developers"));
domain...
CNetDomain pDomain();
machine...
SetDomainName set the domain name in the CNetDomain object.
void SetDomainName(CString strDomainName);
Parameters
CString strDomainName
a string that contains the Domain name.
The GetDomainName function returns the domain name in the CNetDomain
object.
CString GetDomainName();
The GetPDC function returns the name of the Primary Domain Controller
(PDC).
DWORD GetPDC(CString& strPDC);
Parameters
CString& strPDC
a buffer receives the server name of the Primary Domain Controller (PDC) of the
domain.
Return Value
Returns 0
if was successfully.
Remarks:
The m_strError data member Contains the last error string.
The CNetUsers .
CNetUsers();
CNetUsers(CNetDomain pDomain);
Parameters
CNetDomain pDomain
The Add function Adds a user.
DWORD Add(CString strUser, CString strPassword);
Parameters
CString strUser
A string containing the User Name to add.
CString strPassword
A string containing the User Password.
Return Value
The function returns 0
if was succesfully.
See Sample
<A name=Sample01>
CNetDomain pDomain(_T(""));
CNetUsers pUsers(pDomain);
if(pUsers.Add("TestUser", "Newpass") == 0)
{
pUsers.DisableUser(FALSE);
BOOL bRet;
pUsers.IsUserDisable(bRet);
if(bRet)
AfxMessageBox("The user is Disable");
else
AfxMessageBox("The user is Enable");
}
</A>
The Delete function deletes a user.
DWORD Delete(CString strUserName);
Parameters
CString strUserName
A string specifying the name of the user
account.
Return Value
The function returns 0
if was succesfully.
The SetUser function set a user in the CNetUsers objects.
void SetUser(CString strUserName);
Parameters
CString strUserName
A string specifying the name of the user
account.
The GetUser function returns the user name in the CNetUsers objects.
CString GetUser();
Return Value
The function returns a string with the user name.
The SetPassword function sets the user password.
DWORD SetPassword(CString strUserName, CString strPassword);
DWORD SetPassword(CString strPassword);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString strPassword
A string containing the User Password.
Return Value
The function returns 0
if was succesfully.
The ChangePassword function changes the user password.
DWORD ChangePassword(CString strUserName, CString strOldPassword, CString
strNewPassword);
DWORD ChangePassword(CString strOldPassword, CString strNewPassword);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString strOldPassword
A string containing the old User Password.
CString strNewPassword
A string containing the new User Password.
Return Value
The function returns 0
if was succesfully.
The ForceChangePassword function forces to the user to change the
password in the next logon.
DWORD ForceChangePassword(CString strUserName, BOOL bChangePass = TRUE);
DWORD ForceChangePassword(BOOL bChangePass = TRUE);
Parameters
CString strUserName
A string specifying the name of the user
account.
BOOL bChangePass
A pointer to set the change of password.
Return Value
The function returns 0
if was succesfully.
The IsForcedChangePassword function returns if the user must to change
the password in the next logon.
DWORD IsForcedChangePassword(CString strUserName, BOOL& bChangePass);
DWORD IsForcedChangePassword(BOOL& bChangePass);
Parameters
CString strUserName
A string specifying the name of the user
account.
BOOL& bChangePass
A reference to a variable that returns if the
user must to change the password.
Return Value
The function returns 0
if was succesfully.
The DisableUser function disables the user.
DWORD DisableUser(CString strUserName, BOOL bDisable = TRUE);
DWORD DisableUser(BOOL bDisable = TRUE);
Parameters
CString strUserName
A string specifying the name of the user
account.
BOOL bDisable
A pointer to Disable the user.
Return Value
The function returns 0
if was succesfully.
See Sample
The GetUserInfo function gets info about the user.
DWORD GetUserInfo(CString strUserName, CNetUserInfo* pUserInfo);
DWORD GetUserInfo(CNetUserInfo* pUserInfo);
Parameters
CString strUserName
A string specifying the name of the user
account.
CNetUserInfo* pUserInfo
A pointer to a CNetUserInfo object.
struct CNetUserInfo
{
LPSTR name;
LPSTR password;
DWORD password_age;
DWORD priv;
LPSTR home_dir;
LPSTR comment;
DWORD flags;
LPSTR script_path;
DWORD auth_flags;
LPSTR full_name;
LPSTR usr_comment;
LPSTR parms;
LPSTR workstations;
DWORD last_logon;
DWORD last_logoff;
DWORD acct_expires;
DWORD max_storage;
DWORD units_per_week;
PBYTE logon_hours;
DWORD bad_pw_count;
DWORD num_logons;
LPSTR logon_server;
DWORD country_code;
DWORD code_page;
DWORD user_id;
DWORD primary_group_id;
LPSTR profile;
LPSTR home_dir_drive;
BOOL password_expired;
};
For Example:
CNetUserInfo pUserInfo;
pUsers.GetUserInfo("Administrator", &pUserInfo);
Return Value
The function returns 0
if was succesfully.
The SetFullName function sets the User Full Name.
DWORD SetFullName(CString strUserName, CString strFullName);
DWORD SetFullName(CString strFullName);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString strFullName
A string containing the User Full Name.
Return Value
The function returns 0
if was succesfully.
The GetFullName function gets the User Full Name.
DWORD GetFullName(CString strUserName, CString& strFullName);
DWORD GetFullName(CString& strFullName);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString& strFullName
A reference to a string that returns the User
Full Name.
Return Value
The function returns 0
if was succesfully.
The SetComment function sets the user comment.
DWORD SetComment(CString strUserName, CString strComment);
DWORD SetComment(CString strComment);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString strComment
A string containing the User Comment.
Return Value
The function returns 0
if was succesfully.
The GetComment function gets the user comment.
DWORD GetComment(CString strUserName, CString& strComment);
DWORD GetComment(CString& strComment);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString strComment
A reference to string that returns the User
Comment.
Return Value
The function returns 0
if was succesfully.
The SetHomeDirectory function sets the user Home Directory.
DWORD SetHomeDirectory(CString strUserName, CString strHomeDirectory);
DWORD SetHomeDirectory(CString strHomeDirectory);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString strHomeDirectory
A string containing the User Home
Directory.
Return Value
The function returns 0
if was succesfully.
The GetHomeDirectory function gets the user Home Directory.
DWORD GetHomeDirectory(CString strUserName, CString& strHomeDirectory);
DWORD GetHomeDirectory(CString& strHomeDirectory);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString& strHomeDirectory
A reference to a string that returns the
User Home Directory.
Return Value
The function returns 0
if was succesfully.
The SetHomeDirDrive function sets the drive letter to assign to the
user's home directory for logon purposes.
DWORD SetHomeDirDrive(CString strUserName, CString strHomeDirDrive);
DWORD SetHomeDirDrive(CString strHomeDirDrive);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString strHomeDirDrive
A string containing the Drive letter.
Return Value
The function returns 0
if was succesfully.
The GetHomeDirDrive function gets the drive letter assigned to the user's
home directory for logon purposes.
DWORD GetHomeDirDrive(CString strUserName, CString& strHomeDirectory);
DWORD GetHomeDirDrive(CString& strHomeDirectory);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString& strHomeDirDrive
A string containing the Drive letter.
Return Value
The function returns 0
if was succesfully.
The SetUserPrivilege function sets the user Privilege.
DWORD SetUserPrivilege(CString strUserName, int nPrivilege);
DWORD SetUserPrivilege(int nPrivilege);
Parameters
CString strUserName
A string specifying the name of the user
account.
int nPrivilege
can be one of the following values:
CNetUsers::usrPrivGuest Guest
CNetUsers::usrPrivUser User
CNetUsers::usrPrivAdmin Administrator
Return Value
The function returns 0
if was succesfully.
The GetUserPrivilege function gets the user Privilege.
DWORD GetUserPrivilege(CString strUserName, int& nPrivilege);
DWORD GetUserPrivilege(int& nPrivilege);
Parameters
CString strUserName
A string specifying the name of the user
account.
int& nPrivilege
A numeric reference that returns the User
Privilege. See SetUserPrivilege
Return Value
The function returns 0
if was succesfully.
The SetOperatorPrivilege function specifies the user's operator
privileges assigned to a user network account.
DWORD SetOperatorPrivilege(CString strUserName, int nPrivileges);
DWORD SetOperatorPrivilege(int nPrivileges);
Parameters
CString strUserName
A string specifying the name of the user
account.
int nPrivileges
a set of bit flags defining the operator privileges
assigned to a user network account.
CNetUers::opPrivPrint The
print operator privilege is enabled.
CNetUers::opPrivComm The
communications operator privilege is enabled.
CNetUers::opPrivServer The
server operator privilege is enabled.
CNetUers::opPrivAccounts The accounts
operator privilege is enabled.
The GetOperatorPrivilege function retrieves the user's operator
privileges assigned to a user network account.
DWORD GetOperatorPrivilege(CString strUserName, int& nPrivileges);
DWORD GetOperatorPrivilege(int& nPrivileges);
Parameters
CString strUserName
A string specifying the name of the user
account.
int& nPrivileges
a numeric reference that returns a set of
bit flags defining the operator privileges assigned to a user network account.
Return Value
The function returns 0
if was succesfully.
The SetNotChangePass function sets if the user can change the password.
DWORD SetNotChangePass(CString strUserName, BOOL bPermit = FALSE);
DWORD SetNotChangePass(BOOL bPermit = FALSE);
Parameters
CString strUserName
A string specifying the name of the user
account.
BOOL bPermit
A boolean that determines if the user can
change the password.
Return Value
The function returns 0
if was succesfully.
The GetNotChangePass function gets if the user can change the password.
DWORD GetNotChangePass(CString strUserName, BOOL& bNotPermit);
DWORD GetNotChangePass(BOOL& bNotPermit);
Parameters
CString strUserName
A string specifying the name of the user
account.
BOOL& bNotPermit
A boolean reference that determines if the
user can change the password.
Return Value
The function returns 0
if was succesfully.
The SetWorkStations function specifies the names of workstations from
which the user can log on.
DWORD SetWorkStations(CString strUserName, CString strWorkStations);
DWORD SetWorkStations(CString strWorkStations);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString strWorkStations
A string that contains the names of
workstations from which the user can log on.
-
As many as eight workstations can be specified; the
names must be separated by commas. An empty string indicates that there is no
restriction.
For Example:
pUsers.SetWorkStations("pc1,pc2,pc3");
pUsers.SetWorkStations("");
Return Value
The function returns 0
if was succesfully.
The GetWorkStations function gets the names of workstations from which
the user can log on.
DWORD GetWorkStations(CString strUserName, CString& strWorkStations);
DWORD GetWorkStations(CString& strWorkStations);
Parameters
CString strUserName
A string specifying the name of the user
account.
CString& strWorkStations
A reference to a string that contains the
names of workstations from which the user can log on.
Return Value
The function returns 0
if was succesfully.
The GetBadPasswordCount function returns the number of times the user
tried to log on to this account using an incorrect password.
DWORD GetBadPasswordCount(CString strUserName, long& nCount);
DWORD GetBadPasswordCount(long& nCount);
Parameters
CString strUserName
A string specifying the name of the user
account.
long& nCount
a reference to a long value that indicates the
number of times the user tried to log on.
Return Value
The function returns 0
if was succesfully.
The GetLogonCount function returns the number of times the user has
logged on successfully to this account.
DWORD GetLogonCount(CString strUserName, long& nCount);
DWORD GetLogonCount(long& nCount);
Parameters
CString strUserName
A string specifying the name of the user
account.
long& nCount
a reference to a long value that indicates the
number of times the user has logged on successfully.
Return Value
The function returns 0
if was succesfully.
The GetPasswordAge function returns the number of seconds that have
elapsed since the user password was last changed.
DWORD GetPasswordAge(CString strUserName, DWORD& nSeconds);
DWORD GetPasswordAge(DWORD& nSeconds);
Parameters
CString strUserName
A string specifying the name of the user
account.
DWORD& nSeconds
a reference to a DWORD value that indicates
the number of seconds that have elapsed.
Return Value
The function returns 0
if was succesfully.
The IsUserDisable function returns if the user account is disabled.
DWORD IsUserDisable(CString strUserName, BOOL& bDisable);
DWORD IsUserDisable(BOOL& bDisable);
Parameters
CString strUserName
A string specifying the name of the user
account.
BOOL& bDisable
a reference to a BOOL value that indicates if
the user account is disabled..
Return Value
The function returns 0
if was succesfully.
See Sample
The QueryFirstUser function returns user account information.
DWORD QueryFirstUser(CString& strUserName, BOOL& bMoreData);
Parameters
CString& strUserName
a reference to a string that returns the name
of the user account.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
The QueryNextUser function returns user account information.
DWORD QueryNextUser(CString& strUserName, BOOL& bMoreData);
Parameters
CString& strUserName
a reference to a string that returns the name
of the user account.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
The FindFirstUser function provides information about all user accounts
on a server.
DWORD FindFirstUser(CString& strUserName, BOOL& bMoreData);
DWORD FindFirstUser(BOOL& bMoreData);
Parameters
CString& strUserName
a reference to a string that returns the name
of the user account.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
See Sample
The FindNextUser function provides information about all user accounts on
a server.
DWORD FindNextUser(CString& strUserName, BOOL& bMoreData);
DWORD FindNextUser(BOOL& bMoreData);
Parameters
CString& strUserName
a reference to a string that returns the name
of the user account.
BOOL& bMoreData
a reference to a BOOL value that indicates
if have more data to get.
Return Value
The function returns 0
if was succesfully.
See Sample
<A name=FindUser&GroupSample>
CNetDomain pDomain(_T(""));
CNetUsers pUsers(pDomain);
CNetGroups pGroups(pDomain);
CString str;
BOOL bMore;
pUsers.FindFirstUser(str, bMore);
while(bMore)
{
.
.
pUsers.FindNextUser(str, bMore);
}
pGroups.FindFirstGroup(str, bMore);
while(bMore)
{
.
.
pGroups.FindNextGroup(str, bMore);
}
</A>
Remarks:
The m_strError data member Contains the last error string.
The CNetGroups .
CNetGroups(CNetDomain pDomain);
CNetGroups();
Parameters
CNetDomain pDomain
The GetGroup function returns the global group name in the CNetGroups
objects.
CString GetGroup();
Return Value
The function returns a string with the global group name.
The Add function creates a global group in the security database.
DWORD Add(CString strGroupName, CString strComments = _T(""));
Parameters
CString strGroupName
A string specifying the name of the global
group to add.
CString strComments
a string specifying a comment associated with
the global group.
Return Value
The function returns 0
if was succesfully.
The AddUser function adds membership of one existing user accounts to an
existing global group.
DWORD AddUser(CString strGroupName, CString strUserName);
DWORD AddUser(CString strUserName);
Parameters
CString strGroupName
A string specifying the name of the global
group.
CString strUserName
a string specifying the name of the user to be
given membership in the global group.
Return Value
The function returns 0
if was succesfully.
The DeleteUser function removes a user from a particular global group in
the security database.
DWORD DeleteUser(CString strGroupName, CString strUserName);
DWORD DeleteUser(CString strUserName);
Parameters
CString strGroupName
A string specifying the name of the global
group.
CString strUserName
a string specifying the name of the user to
remove from the global group.
Return Value
The function returns 0
if was succesfully.
The Delete function removes a global group from the security database.
DWORD Delete();
DWORD Delete(CString strGroupName);
Parameters
CString strGroupName
A string specifying the name of the global
group to remove.
Return Value
The function returns 0
if was succesfully.
The FindFirstGroup function retrieves information about each global group
in the security database.
DWORD FindFirstGroup(CString& strGroupName, BOOL& bMoreData);
DWORD FindFirstGroup(BOOL& bMoreData);
Parameters
CString& strGroupName
A reference to a string specifying the name
of the global group that was founded.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
See Sample
The FindNextGroup function retrieves information about each global group
in the security database.
DWORD FindNextGroup(CString& strGroupName, BOOL& bMoreData);
DWORD FindNextGroup(BOOL& bMoreData);
Parameters
CString& strGroupName
A reference to a string specifying the name
of the global group that was founded.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
The FindFirstUser function retrieves information about each user in a
global group account.
DWORD FindFirstUser(CString strGroupName, CString& strUserName, BOOL&
bMoreData);
DWORD FindFirstUser(CString& strUserName, BOOL& bMoreData);
Parameters
CString strGroupName
A string specifying the name of the global
group where look for.
CString& strUserName
A reference to a string returns the user name.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
The FindNextUser function retrieves information about each user in a
global group account.
DWORD FindNextUser(CString& strUserName, BOOL& bMoreData);
Parameters
CString& strUserName
A reference to a string returns the user name.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
The SetGroup sets a current global group account.
void SetGroup(CString strGroupName);
Parameters
CString strGroupName
A string specifying the name of the global
group account.
The GetGroup returns the name of the current global group account.
CString GetGroup();
Return Value
The function returns the global group account name.
Remarks:
The m_strError data member Contains the last error string.
The CNetLocalGroups .
CNetLocalGroups(CNetDomain pDomain);
CNetLocalGroups();
Parameters
CNetDomain pDomain
The GetGroup function returns the local group name in the CNetLocalGroups
objects.
CString GetGroup();
Return Value
The function returns a string with the local group name.
The Add function creates a local group in the security database.
DWORD Add(CString strLocalGroupName, CString strComments = _T(""));
Parameters
CString strLocalGroupName
A string specifying the name of the local
group to add.
CString strComments
a string specifying a comment associated with
the local group.
Return Value
The function returns 0
if was succesfully.
The AddUser function adds membership of one existing user accounts to an
existing local group.
DWORD AddUser(CString strLocalGroupName, CString strUserName);
DWORD AddUser(CString strUserName);
Parameters
CString strLocalGroupName
A string specifying the name of the local
group to add.
CString strUserName
a string specifying the name of the user to be
given membership in the local group.
Return Value
The function returns 0
if was succesfully.
The DeleteUser function removes a user from a local group.
DWORD DeleteUser(CString strLocalGroupName, CString strUserName);
DWORD DeleteUser(CString strUserName);
Parameters
CString strLocalGroupName
A string specifying the name of the local
group.
CString strUserName
a string specifying the name of the user to
remove from the local group.
Return Value
The function returns 0
if was succesfully.
The Delete function removes a local group from the security database.
DWORD Delete();
DWORD Delete(CString strLocalGroupName);
Parameters
CString strLocalGroupName
A string specifying the name of the local
group to remove.
Return Value
The function returns 0
if was succesfully.
The FindFirstGroup function retrieves information about each local group
in the security database.
DWORD FindFirstGroup(CString& strLocalGroupName, BOOL& bMoreData);
DWORD FindFirstGroup(BOOL& bMoreData);
Parameters
CString strLocalGroupName
A string specifying the name of the local
group that was founded.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
The FindNextGroup function retrieves information about each local group
in the security database.
DWORD FindNextGroup(CString& strLocalGroupName, BOOL& bMoreData);
DWORD FindNextGroup(BOOL& bMoreData);
Parameters
CString strLocalGroupName
A string specifying the name of the local
group that was founded.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
The FindFirstUser function retrieves information about each user in a
local group account.
DWORD FindFirstUser(CString strLocalGroupName, CString& strUserName, BOOL&
bMoreData);
DWORD FindFirstUser(CString& strUserName, BOOL& bMoreData);
Parameters
CString strLocalGroupName
A string specifying the name of the local
group prefixed by the domain name and the "\" separator character. For Example:
DEVELOPER\Administrator
CString& strUserName
A reference to a string returns the user name.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
The FindNextUser function retrieves information about each user in a
local group account.
DWORD FindNextUser(CString& strUserName, BOOL& bMoreData);
Parameters
CString& strUserName
A reference to a string returns the user name
prefixed by the domain name and the "\" separator character.
BOOL& bMoreData
a reference to a BOOL value that indicates
if has more data to get.
Return Value
The function returns 0
if was succesfully.
The SetGroup sets a local group account.
void SetGroup(CString strLocalGroupName);
Parameters
CString strLocalGroupName
A string specifying the name of the local
group.
The GetGroup returns the name of the current local group account.
CString GetGroup();
Return Value
The function returns the local group account name.
Remarks:
The m_strError data member Contains the last error string.
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Unsupported.
Library: Use Netapi32.lib.
Carlos A. Antollini.
Updates
28 Nov 2001 Version 1.0 Released.
03 Dec 2001 Version 1.08
- Added the following functions:
CNetGroups::FindFirstUser, CNetGroups::FindNextUser,
CNetLocalGroups::FindFirstUser, CNetLocalGroups::FindNextUser
16 Jan 2002 Version 1.10
- Added the following functions:
CNetUsers::GetHomeDirDrive, CNetUsers::SetHomeDirDrive
Special thanks
These Net Classes received many suggestions from the users.
Thank to All for your collaboration and Ideas.
Ingo Stapel
Spiros Prantalos
Carlos Antollini is a software engineer working on Object Oriented, Visual C++, MFC, COM, ATL, ADO, Internet technologies and Business Intelligence.
Carlos is originally from Argentina, he was living for several years in Fort Lauderdale, Florida, working for Citibank. Then he started his own business.
Carlos is the creator of <a href="http://www.piFive.com">piFive</a>[<a target="_blank" title="piFive" href="http://www.piFive.com">^</a>]
, a family of BI Analytic Platform software, that it deals next to, <a href="http://www.latinsys.com">latinsys</a>[<a target="_blank" title="latinsys" href="http://www.latinsys.com">^</a>]
, his partner in businesses...
Currently he is sharing his passion for project management and BI at Citigroup.