C language user account management

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.