调用NetUserGetLocalGroups出现User Not Found

zhenghe1234 2011-10-24 03:47:36
LPTSTR servername = NULL;
TCHAR *username = "Administrator";
DWORD level = 0;
DWORD flags = LG_INCLUDE_INDIRECT;
LPLOCALGROUP_USERS_INFO_0 bufptr = NULL;
DWORD prefmaxlen = MAX_PREFERRED_LENGTH;
DWORD entriesread = 0;
DWORD totalentries = 0;

NET_API_STATUS nStatus;

nStatus = NetUserGetLocalGroups((LPCWSTR)servername,(LPCWSTR)username,
level,flags,(LPBYTE*)&bufptr,prefmaxlen,&entriesread,&totalentries);
在运行以上代码时,出现2221错误,User Not Found。
刚学 C++,希望各位大神给个答案,
...全文
487 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhenghe1234 2011-10-24
  • 打赏
  • 举报
回复
哇,搞定了,果然是的,
我用的的是 VS 2010 ,在代码里指定了下编码

#ifndef UNICODE
#define UNICODE
#endif

然后就搞定了,谢谢了!
柯本 2011-10-24
  • 打赏
  • 举报
回复
你用的什么编译器?
我的VS2008 ,缺省是UNICODE的,应该用
TCHAR *username = L"Administrator";
调用参数为LPWSTR username
改为unicode的试试



zhenghe1234 2011-10-24
  • 打赏
  • 举报
回复
这个都看过了,关键是我函数用的应该没有错误,但是,就是找不到用户
赵4老师 2011-10-24
  • 打赏
  • 举报
回复
开始、运行、lusrmgr.msc
查看本地用户和组
赵4老师 2011-10-24
  • 打赏
  • 举报
回复
以下内容摘自MSDN98:
NetUserGetLocalGroups
The NetUserGetLocalGroups function retrieves a list of local groups to which a specified user belongs.

Security Requirements
Only members of the Administrators or Account Operators local group can successfully execute NetUserGetLocalGroups.

NET_API_STATUS NetUserGetLocalGroups(
LPWSTR servername,
LPWSTR username,
DWORD level,
DWORD flags,
LPBYTE *bufptr,
DWORD prefmaxlen,
LPDWORD entriesread,
LPDWORD totalentries
);

Parameters
servername
Pointer to a Unicode string containing the name of the remote server on which the function is to execute. A NULL pointer or string specifies the local computer.
username
Pointer to a Unicode string containing the name of the user for which to return global group membership. This parameter can be of the form <UserName>, in which case the username is expected to be found on servername. The user name can also be of the form <DomainName>\<UserName> in which case <DomainName> is associated with servername and <UserName> is expected to be to be found on that domain.
level
Level of information required. Only 0 is valid.
flags
Bitmask of flags. Currently, only LG_INCLUDE_INDIRECT is defined. If this bit is set, the function will also return the local groups of which the user is indirectly a member (that is, by the virtue of being in a global group that itself is a member of one or more local groups).
bufptr
On return a pointer to the return information structure is returned in the address pointed to by bufptr. The returned information is an array of LOCALGROUP_USERS_INFO_0 structures. The returned buffer should be deallocated using the NetApiBufferFree function.
prefmaxlen
Preferred maximum length, in 8-bit bytes of returned data.
entriesread
Pointer to a DWORD that contains the actual enumerated element count.
totalentries
Pointer to a DWORD that contains the total number of entries that could have been enumerated.
Return Values
If the function is successful, it returns NERR_SUCCESS.

If the function fails, the return value is one of the following error codes.

Value Meaning
ERROR_ACCESS_DENIED The user does not have access to the requested information.
NERR_InvalidComputer The computer name is invalid.
NERR_UserNotFound The user name could not be found.


QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in lmaccess.h.
Import Library: Use netapi32.lib.

See Also
Networking (Net) Overview, Net Functions, NetApiBufferFree


zhenghe1234 2011-10-24
  • 打赏
  • 举报
回复


补充下,这段代码是运行正确的,
但是我需要的手动的输入用户名,而不是通过
程序得到的一个用户名列表。


LPTSTR pszServerName = NULL;
DWORD level = 0;
DWORD filter = FILTER_NORMAL_ACCOUNT;
LPUSER_INFO_0 bufptr = NULL;
LPUSER_INFO_0 tmpbufptr = NULL;
DWORD prefmaxlen = MAX_PREFERRED_LENGTH;
DWORD entriesread = 0;
DWORD totalentries = 0;
DWORD resume_handle = 0;

NET_API_STATUS nStatus;
do{
nStatus = NetUserEnum((LPCWSTR)pszServerName,level,
filter,(LPBYTE*)&bufptr,prefmaxlen,&entriesread,&totalentries,&resume_handle);
if(nStatus == NERR_Success)
{
tmpbufptr = bufptr;
for(int i=0;i<entriesread;i++)
{
wprintf(L"%s\n", tmpbufptr->usri0_name);
//cout<<tmpbufptr->usri0_name<<endl;

DWORD level = 0;
DWORD flags = LG_INCLUDE_INDIRECT;
LPLOCALGROUP_USERS_INFO_0 groupbufptr = NULL;
LPLOCALGROUP_USERS_INFO_0 tmpgroupbufptr = NULL;
DWORD prefmaxlen = MAX_PREFERRED_LENGTH;
DWORD entriesread = 0;
DWORD totalentries = 0;
NET_API_STATUS tmpnStatus;

tmpnStatus = NetUserGetLocalGroups((LPCWSTR)pszServerName,(LPCWSTR)tmpbufptr->usri0_name,
level,flags,(LPBYTE*)&groupbufptr,prefmaxlen,&entriesread,&totalentries);
if(tmpnStatus == NERR_Success)
{
tmpgroupbufptr = groupbufptr;
for(int j=0;j<entriesread;j++)
{
wprintf(L"--%s\n", tmpgroupbufptr->lgrui0_name);
tmpgroupbufptr++;
}
}
if(groupbufptr != NULL)
{
NetApiBufferFree(groupbufptr);
groupbufptr = NULL;
}
tmpbufptr++;
}
}
if(bufptr != NULL)
{
NetApiBufferFree(bufptr);
bufptr = NULL;
}
}while(nStatus == ERROR_MORE_DATA);
system("PAUSE");
return 0;
zhenghe1234 2011-10-24
  • 打赏
  • 举报
回复
LPTSTR servername = NULL;
定义了 servername 为 NULL 就指定的是本机,
在使用 NetUserEnum 函数时,可以列出所有的用户
其中就有有 Administrator 这个用户,而且可以调用 NetUserGetLocalGroups 函数成功,

主要是使用自己定义的 username 就不成功
柯本 2011-10-24
  • 打赏
  • 举报
回复
你的servername是本机还是远程服务器的?
可能的问题是:servername指定的系统中Administrator用户不存在或被禁用了(通常XP下,Administrator是禁用的,特别是对远程用户)
healer_kx 2011-10-24
  • 打赏
  • 举报
回复
MARK

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧