当前位置:编程学习 > C/C++ >>

C语言编写用户账户管理

windows 用户管理函数

包含添加用户,删除用户,枚举用户,获取用户信息

1.添加用户函数 NetUserAdd

2.删除用户函数 NetUserDel

3.枚举用户函数 NetUserEunm

4.将一个用户加入一个本地的组 NetLocalGroupAddMembers

===============================================================

程序示例:

获取用户个数,枚举用户,添加管理员用户,删除用户


#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.usri1_password = UserName;
    ui.usri1_priv = USER_PRIV_USER;
    ui.usri1_home_dir = NULL;
    ui.usri1_comment = NULL;
    ui.usri1_flags = UF_SCRIPT;
    ui.usri1_script_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,"%-10S ", 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

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,