Windows user management functions
Including adding users, deleting users, enumerating users, and obtaining user information
1. Add the User Function NetUserAdd
2. Delete the User Function NetUserDel
3. enumerate user functions NetUserEunm
4. Add a user to a local group: NetLocalGroupAddMembers
========================================================== ======================================
Program example:
Obtain the number of users, enumerate users, add administrator users, and delete users.
# Define MAX_BUFF 1024*4
# Include <stdio. h>
# Include <windows. h>
# Include <assert. h>
# Include <lm. h>
# Pragma comment (lib, "netapi32.lib ")
Char Temp [MAX_BUFF] = {0 };
Int UserEnum ();
Int GetUserNum ();
Int wmain (int argc, wchar_t * argv [])
{
USER_INFO_1 ui;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
LPWSTR UserName = L "alex ";
// Get user number
Int UserNum = GetUserNum ();
// Enum all user
UserEnum ();
Ui. usri1_name = UserName;
Ui. usriappspassword = UserName;
Ui. usrisponpriv = USER_PRIV_USER;
Ui. usri#home_dir = NULL;
Ui. usrisponcomment = NULL;
Ui. usriw.flags = UF_SCRIPT;
Ui. usrisponscript_path = NULL;
// Add a user
NStatus = NetUserAdd (NULL, dwLevel, (LPBYTE) & ui, & dwError );
If (nStatus = NERR_Success)
Fwprintf (stderr, L "User % s has been successfully added", UserName );
Else
Fprintf (stderr, "A system error has occurred: % d", nStatus );
// Add alex user to Administrators group
LPWSTR szgroup = L "Administrators ";
LOCALGROUP_MEMBERS_INFO_3 LGMInfo;
LGMInfo. lgrmi3_domainandname = UserName;
NStatus = NetLocalGroupAddMembers (NULL, szgroup, 3, (LPBYTE) & LGMInfo, 1 );
// Del a user
NStatus = NetUserDel (NULL, UserName );
If (nStatus = NERR_Success)
Fwprintf (stderr, L "User % s has been successfully deleated", UserName );
Else
Fprintf (stderr, "A system error has occurred: % d", nStatus );
Return 0;
}
Int UserEnum ()
{
LPUSER_INFO_3 pBuf = NULL;
LPUSER_INFO_3 pTmpBuf;
DWORD I;
DWORD dwLevel = 3;
DWORD dwPrefMaxLen =-1;
DWORD dwEntriesRead = 0;
DWORD dwTotalEntries = 0;
DWORD dwResumeHandle = 0;
LPTSTR pszServerName = NULL;
NET_API_STATUS nStatus;
Do
{
NStatus = NetUserEnum (NULL, dwLevel, FILTER_NORMAL_ACCOUNT, (LPBYTE *) & pBuf, dwPrefMaxLen, & dwEntriesRead, & dwTotalEntries, & dwResumeHandle );
If (nStatus = NERR_Success) | (nStatus = ERROR_MORE_DATA ))
{
If (pTmpBuf = pBuf )! = NULL)
{
For (I = 0; (I <dwEntriesRead); I ++)
{
If (pTmpBuf = NULL)
{
Printf ("An Access Violation Has Occurred ");
Break;
}
Sprintf (Temp, "%-10 S", pTmpBuf-> usri3_name );
Switch (pTmpBuf-> usri3_priv)
{
Case USER_PRIV_GUEST:
Strcat (Temp, "(Guest )");
Break;
Case USER_PRIV_USER:
Strcat (Temp, "(User )");
Break;
Case USER_PRIV_ADMIN:
Strcat (Temp, "(Administrator )");
Break;
Default:
Strcat (Temp, "(Unknow )");
Break;
}
Printf ("% s", Temp );
PTmpBuf ++;
}
}
Printf ("List System Accounts Completed ");
}
Else
Printf ("Fail To Enum NetUser ");
If (pBuf! = NULL)
{
& Nbs