程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved