河池企业网站开发公司新东方教育培训机构
【WINAPI】NetUserEnum-获取系统所有账户名称
- 一、说明
- 二、头文件
- 三、函数原型
- 四、参数说明
- 五、返回值
- 六、例子
- 七、参考网址
一、说明
该 NetUserEnum函数获取服务器上所有用户的账户信息。
二、头文件
lmaccess.h
三、函数原型
NET_API_STATUS NET_API_FUNCTION NetUserEnum(LPCWSTR servername,DWORD level,DWORD filter,LPBYTE *bufptr,DWORD prefmaxlen,LPDWORD entriesread,LPDWORD totalentries,PDWORD resume_handle
);
四、参数说明
servername [in]
:指向常量字符串的指针,该字符串指定要在其上执行函数的远程服务器的DNS或NetBIOS名称。如果此参数为NULL,则使用本地计算机。level[in]
:指定数据的信息级别。具体信息filter [in]
:指向要包含在枚举中的用户帐户类型的值。值为零表示应该包括所有普通用户,信任数据和机器账户数据。bufptr [out]
:指向接收数据的缓冲区的指针。该数据的格式取决于level参数的值。prefmaxlen [in]
:返回数据的首选最大长度(以字节为单位)。如果指定MAX_PREFERRED_LENGTH
,则NetUserEnum
函数会分配数据所需的内存量。如果在此参数中指定另一个值,则可以限制该函数返回的字节数。如果缓冲区大小不足以保存所有条目,则函数返回ERROR_MORE_DATA
。entriesread [out]
:指向一个值的指针,该值接收实际枚举的元素数。totalentries [out]
:指向一个值的指针,该值接收可能已从当前恢复位置枚举的条目总数。resume_handle [in, out]
:指向包含恢复句柄的值的指针,该恢复句柄用于继续现有的用户搜索。
五、返回值
- 如果函数成功,则返回值为
NERR_Sucess
. - 如果返回失败,则返回值可以是以下错误代码之一。
返回码 | 描述 |
---|---|
ERROR_ACCESS_DENIED | 用户无权访问所请求的信息。 |
ERROR_INVALID_LEVEL | 系统调用级别不正确。如果将level参数设置为不支持的值,则会返回此错误。 |
NERR_BufTooSmall | 缓冲区太小,无法包含条目。尚未将任何信息写入缓冲区。 |
NERR_InvalidComputer | 计算机名称无效。 |
ERROR_MORE_DATA | 有更多条目可用。指定足够大的缓冲区以接收所有条目。 |
六、例子
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <lm.h>int wmain(int argc, wchar_t* argv[])
{LPUSER_INFO_0 pBuf = NULL;LPUSER_INFO_0 pTmpBuf;DWORD dwLevel = 0;DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;DWORD dwEntriesRead = 0;DWORD dwTotalEntries = 0;DWORD dwResumeHandle = 0;DWORD i;DWORD dwTotalCount = 0;NET_API_STATUS nStatus;LPTSTR pszServerName = NULL;if (argc > 2){fwprintf(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);exit(1);}// The server is not the default local computer.//if (argc == 2)pszServerName = (LPTSTR)argv[1];wprintf(L"\nUser account on %s: \n", pszServerName);//// Call the NetUserEnum function, specifying level 0; // enumerate global user account types only.//do // begin do{nStatus = NetUserEnum((LPCWSTR)pszServerName,dwLevel,FILTER_NORMAL_ACCOUNT, // global users(LPBYTE*)&pBuf,dwPrefMaxLen,&dwEntriesRead,&dwTotalEntries,&dwResumeHandle);//// If the call succeeds,//if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)){if ((pTmpBuf = pBuf) != NULL){//// Loop through the entries.//for (i = 0; (i < dwEntriesRead); i++){assert(pTmpBuf != NULL);if (pTmpBuf == NULL){fprintf(stderr, "An access violation has occurred\n");break;}//// Print the name of the user account.//wprintf(L"\t-- %s\n", pTmpBuf->usri0_name);pTmpBuf++;dwTotalCount++;}}}//// Otherwise, print the system error.//elsefprintf(stderr, "A system error has occurred: %d\n", nStatus);//// Free the allocated buffer.//if (pBuf != NULL){NetApiBufferFree(pBuf);pBuf = NULL;}}// Continue to call NetUserEnum while // there are more entries. // while (nStatus == ERROR_MORE_DATA); // end do//// Check again for allocated memory.//if (pBuf != NULL)NetApiBufferFree(pBuf);//// Print the final count of users enumerated.//fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount);return 0;
}
七、参考网址
https://docs.microsoft.com/en-us/windows/win32/api/lmaccess/nf-lmaccess-netuserenum