Windows用户相关操作

  • 获取所有用户
NET_API_STATUS NetUserEnum(
  LPCWSTR servername,
  DWORD level,
  DWORD filter,
  LPBYTE* bufptr,
  DWORD prefmaxlen,
  LPDWORD entriesread,
  LPDWORD totalentries,
  LPDWORD resume_handle
);
  1 #ifndef UNICODE
  2 #define UNICODE
  3 #endif
  4 
  5 #include <stdio.h>
  6 #include <assert.h>
  7 #include <windows.h> 
  8 #include <lm.h>
  9 
 10 int wmain(int argc, wchar_t *argv[])
 11 {
 12    LPUSER_INFO_0 pBuf = NULL;
 13    LPUSER_INFO_0 pTmpBuf;
 14    DWORD dwLevel = 0;
 15    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
 16    DWORD dwEntriesRead = 0;
 17    DWORD dwTotalEntries = 0;
 18    DWORD dwResumeHandle = 0;
 19    DWORD i;
 20    DWORD dwTotalCount = 0;
 21    NET_API_STATUS nStatus;
 22    LPTSTR pszServerName = NULL;
 23 
 24    if (argc > 2)
 25    {
 26       fwprintf(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);
 27       exit(1);
 28    }
 29    // The server is not the default local computer.
 30    //
 31    if (argc == 2)
 32       pszServerName = argv[1];
 33    wprintf(L"\nUser account on %s: \n", pszServerName);
 34    //
 35    // Call the NetUserEnum function, specifying level 0; 
 36    //   enumerate global user account types only.
 37    //
 38    do // begin do
 39    {
 40       nStatus = NetUserEnum(pszServerName,
 41                             dwLevel,
 42                             FILTER_NORMAL_ACCOUNT, // global users
 43                             (LPBYTE*)&pBuf,
 44                             dwPrefMaxLen,
 45                             &dwEntriesRead,
 46                             &dwTotalEntries,
 47                             &dwResumeHandle);
 48       //
 49       // If the call succeeds,
 50       //
 51       if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
 52       {
 53          if ((pTmpBuf = pBuf) != NULL)
 54          {
 55             //
 56             // Loop through the entries.
 57             //
 58             for (i = 0; (i < dwEntriesRead); i++)
 59             {
 60                assert(pTmpBuf != NULL);
 61 
 62                if (pTmpBuf == NULL)
 63                {
 64                   fprintf(stderr, "An access violation has occurred\n");
 65                   break;
 66                }
 67                //
 68                //  Print the name of the user account.
 69                //
 70                wprintf(L"\t-- %s\n", pTmpBuf->usri0_name);
 71 
 72                pTmpBuf++;
 73                dwTotalCount++;
 74             }
 75          }
 76       }
 77       //
 78       // Otherwise, print the system error.
 79       //
 80       else
 81          fprintf(stderr, "A system error has occurred: %d\n", nStatus);
 82       //
 83       // Free the allocated buffer.
 84       //
 85       if (pBuf != NULL)
 86       {
 87          NetApiBufferFree(pBuf);
 88          pBuf = NULL;
 89       }
 90    }
 91    // Continue to call NetUserEnum while 
 92    //  there are more entries. 
 93    // 
 94    while (nStatus == ERROR_MORE_DATA); // end do
 95    //
 96    // Check again for allocated memory.
 97    //
 98    if (pBuf != NULL)
 99       NetApiBufferFree(pBuf);
100    //
101    // Print the final count of users enumerated.
102    //
103    fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount);
104 
105    return 0;
106 }
  • 获取用户信息
NET_API_STATUS NetUserGetInfo(
  LPCWSTR servername,
  LPCWSTR username,
  DWORD level,
  LPBYTE* bufptr
);
 1 #ifndef UNICODE
 2 #define UNICODE
 3 #endif
 4 
 5 #include <stdio.h>
 6 #include <windows.h> 
 7 #include <lm.h>
 8 
 9 int wmain(int argc, wchar_t *argv[])
10 {
11    DWORD dwLevel = 10;
12    LPUSER_INFO_10 pBuf = NULL;
13    NET_API_STATUS nStatus;
14 
15    if (argc != 3)
16    {
17       fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[0]);
18       exit(1);
19    }
20    //
21    // Call the NetUserGetInfo function; specify level 10.
22    //
23    nStatus = NetUserGetInfo(argv[1],
24                             argv[2],
25                             dwLevel,
26                             (LPBYTE *)&pBuf);
27    //
28    // If the call succeeds, print the user information.
29    //
30    if (nStatus == NERR_Success)
31    {
32     if (pBuf != NULL)
33       {
34          wprintf(L"\n\tAccount:      %s\n", pBuf->usri10_name);
35          wprintf(L"\tComment:      %s\n", pBuf->usri10_comment);
36          wprintf(L"\tUser comment: %s\n", pBuf->usri10_usr_comment);
37          wprintf(L"\tFull name:    %s\n", pBuf->usri10_full_name);
38       }
39    }
40    // Otherwise, print the system error.
41    //
42    else
43       fprintf(stderr, "A system error has occurred: %d\n", nStatus);
44    //
45    // Free the allocated memory.
46    //
47    if (pBuf != NULL)
48       NetApiBufferFree(pBuf);
49 
50    return 0;
51 }
  • 修改用户信息
NET_API_STATUS NetUserSetInfo(
  LPCWSTR servername,
  LPCWSTR username,
  DWORD level,
  LPBYTE buf,
  LPDWORD parm_err
);
 1 #ifndef UNICODE
 2 #define UNICODE
 3 #endif
 4 
 5 #include <stdio.h>
 6 #include <windows.h> 
 7 #include <lm.h>
 8 
 9 int wmain(int argc, wchar_t *argv[])
10 {
11    DWORD dwLevel = 1008;
12    USER_INFO_1008 ui;
13    NET_API_STATUS nStatus;
14 
15    if (argc != 3)
16    {
17       fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[0]);
18       exit(1);
19    }
20    // Fill in the USER_INFO_1008 structure member.
21    // UF_SCRIPT: required for LAN Manager 2.0 and
22    //  Windows NT and later.
23    //
24    ui.usri1008_flags = UF_SCRIPT | UF_ACCOUNTDISABLE;
25    //
26    // Call the NetUserSetInfo function 
27    //  to disable the account, specifying level 1008.
28    //
29    nStatus = NetUserSetInfo(argv[1],
30                             argv[2],
31                             dwLevel,
32                             (LPBYTE)&ui,
33                             NULL);
34    //
35    // Display the result of the call.
36    //
37    if (nStatus == NERR_Success)
38       fwprintf(stderr, L"User account %s has been disabled\n", argv[2]);
39    else
40       fprintf(stderr, "A system error has occurred: %d\n", nStatus);
41 
42    return 0;
43 }
  • 增加用户
NET_API_STATUS NetUserAdd(
  LMSTR servername,
  DWORD level,
  LPBYTE buf,
  LPDWORD parm_err
);
 1 #ifndef UNICODE
 2 #define UNICODE
 3 #endif
 4 
 5 #include <stdio.h>
 6 #include <windows.h> 
 7 #include <lm.h>
 8 
 9 int wmain(int argc, wchar_t *argv[])
10 {
11    USER_INFO_1 ui;
12    DWORD dwLevel = 1;
13    DWORD dwError = 0;
14    NET_API_STATUS nStatus;
15 
16    if (argc != 3)
17    {
18       fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[0]);
19       exit(1);
20    }
21    //
22    // Set up the USER_INFO_1 structure.
23    //  USER_PRIV_USER: name identifies a user, 
24    //    rather than an administrator or a guest.
25    //  UF_SCRIPT: required for LAN Manager 2.0 and
26    //    Windows NT and later.
27    //
28    ui.usri1_name = argv[2];
29    ui.usri1_password = argv[2];
30    ui.usri1_priv = USER_PRIV_USER;
31    ui.usri1_home_dir = NULL;
32    ui.usri1_comment = NULL;
33    ui.usri1_flags = UF_SCRIPT;
34    ui.usri1_script_path = NULL;
35    //
36    // Call the NetUserAdd function, specifying level 1.
37    //
38    nStatus = NetUserAdd(argv[1],
39                         dwLevel,
40                         (LPBYTE)&ui,
41                         &dwError);
42    //
43    // If the call succeeds, inform the user.
44    //
45    if (nStatus == NERR_Success)
46       fwprintf(stderr, L"User %s has been successfully added on %s\n",
47                argv[2], argv[1]);
48    //
49    // Otherwise, print the system error.
50    //
51    else
52       fprintf(stderr, "A system error has occurred: %d\n", nStatus);
53 
54    return 0;
55 }
  • 用户删除
NET_API_STATUS NetUserDel(
  LPCWSTR servername,
  LPCWSTR username
);
 1 #ifndef UNICODE
 2 #define UNICODE
 3 #endif
 4 
 5 #include <stdio.h>
 6 #include <windows.h> 
 7 #include <lm.h>
 8 
 9 int wmain(int argc, wchar_t *argv[])
10 {
11    DWORD dwError = 0;
12    NET_API_STATUS nStatus;
13    //
14    // All parameters are required.
15    //
16    if (argc != 3)
17    {
18       fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[0]);
19       exit(1);
20    }
21    //
22    // Call the NetUserDel function to delete the share.
23    //
24    nStatus = NetUserDel(argv[1], argv[2]);
25    //
26    // Display the result of the call.
27    //
28    if (nStatus == NERR_Success)
29       fwprintf(stderr, L"User %s has been successfully deleted on %s\n",
30                argv[2], argv[1]);
31    else
32       fprintf(stderr, "A system error has occurred: %d\n", nStatus);
33 
34    return 0;
35 }

 

上一篇:printf,fprintf(stdout,stderr),sprintf等的使用方法及区别


下一篇:[转]QNX的用户登录账号密码处理-源代码解析