从本地管理员组打印条目 [英] Print entries from local admin group
问题描述
大家好,
我编写了这段代码来获取系统管理员组中的对象列表。我能够从系统中获取数据,但我无法弄清楚如何单独打印每个条目。
#include < iostream >
#include < windows.h >
#include < lm.h >
使用 命名空间标准;
int main( int argc, char ** argv)
{
wchar_t LOCALGRP [ 100 ] = L 管理员;
DWORD bfSize = MAX_COMPUTERNAME_LENGTH + 1;
DWORD LEVEL = 1 ;
LPBYTE bufptr;
bool RET;
DWORD prefmaxlength = MAX_PREFERRED_LENGTH;
NET_API_STATUS状态;
DWORD条目;
DWORD totalentries;
STATUS = NetLocalGroupGetMembers(NULL,LOCALGRP,LEVEL,& bufptr,prefmaxlength,& entriesread,& totalentries, 0 );
cout<< entriesread<< <<为totalEntries<< ENDL;
system( PAUSE);
return 0 ;
}
我的尝试:
函数NetLocalGroupGetMembers返回的对象采用结构LOCALGROUP_MEMBERS_INFO_1的格式。根据Microsoft文档,bufptr指向存储返回对象的地址。无法弄清楚如何从这个结构中打印信息。
MSDN文档准确解释了每个字段包含的内容: LOCALGROUP_MEMBERS_INFO_1结构(Windows) [ ^ ]。您应该将bufptr
声明为PLOCALGROUP_MEMBERS_INFO_1
类型,而不是LPBYTE,这样您就可以轻松访问每个字段。
让代码正常工作。
# include < iostream >
#include < windows.h >
#include < lm.h >
使用 命名空间标准;
/ * 使用控制台暂停运行此程序或添加您自己的getch,system(暂停)或输入循环* /
int main( int argc, char ** argv)
{
wchar_t LOCALGRP [< span class =code-digit> 100 ] = L 管理员;
DWORD LEVEL = 1 ,prefmaxlength = MAX_PREFERRED_LENGTH,entriesread,totalentries;
PLOCALGROUP_MEMBERS_INFO_1 bufptr;
system( cls);
NetLocalGroupGetMembers(NULL,LOCALGRP,LEVEL,(LPBYTE *)& bufptr,prefmaxlength,& entriesread,& totalentries, 0 );
for ( int i = 0 ; i< entriesread; i ++)<! - = 换行符= - = < span class =code-string>> {
if (bufptr [i] .lgrmi1_sidusage == SidTypeUser)
{
wcout<< bufptr [i] .lgrmi1_name< ;< endl;<! - newline = - = > }
}
NetApiBufferFree(& bufptr);
system( PAUSE);
return 0 ;
}
< / bufptr [i] .lgrmi1_name<< endl;<! - >< / entriesread;>< /lm.h>< /windows.h></iostream>
Hi Guys,
I have written this code to get the list of objects from Administrators group of the system. I'm able to fetch the data from the system but I'm not able to figure out how to print each entry separately.
#include <iostream>
#include <windows.h>
#include <lm.h>
using namespace std;
int main(int argc, char** argv)
{
wchar_t LOCALGRP[100]= L"Administrators";
DWORD bfSize = MAX_COMPUTERNAME_LENGTH+1;
DWORD LEVEL=1;
LPBYTE bufptr;
bool RET;
DWORD prefmaxlength = MAX_PREFERRED_LENGTH;
NET_API_STATUS STATUS;
DWORD entriesread;
DWORD totalentries;
STATUS = NetLocalGroupGetMembers(NULL,LOCALGRP,LEVEL,&bufptr,prefmaxlength,&entriesread,&totalentries,0);
cout<<entriesread<<" "<<totalentries<<endl;
system("PAUSE");
return 0;
}
What I have tried:
The object returned by function NetLocalGroupGetMembers is in the format of structure LOCALGROUP_MEMBERS_INFO_1. As per Microsoft documentation bufptr points to the address where returned object is stored. Not able to figure out how to print information from this structure.
The MSDN documentation explains exactly what each field contains: LOCALGROUP_MEMBERS_INFO_1 structure (Windows)[^]. You should declarebufptr
as aPLOCALGROUP_MEMBERS_INFO_1
type, rather than LPBYTE, which will allow you to easily access each field.
Got the code working.
#include <iostream> #include <windows.h> #include <lm.h> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { wchar_t LOCALGRP[100]= L"Administrators"; DWORD LEVEL=1, prefmaxlength=MAX_PREFERRED_LENGTH, entriesread, totalentries; PLOCALGROUP_MEMBERS_INFO_1 bufptr; system("cls"); NetLocalGroupGetMembers(NULL,LOCALGRP,LEVEL,(LPBYTE *)&bufptr,prefmaxlength,&entriesread,&totalentries,0); for(int i=0; i<entriesread; i++)<!--="" newline="" --=""> { if(bufptr[i].lgrmi1_sidusage == SidTypeUser) { wcout<<bufptr[i].lgrmi1_name<<endl;<!-- newline="" --=""> } } NetApiBufferFree(&bufptr); system("PAUSE"); return 0; } </bufptr[i].lgrmi1_name<<endl;<!--></entriesread;></lm.h></windows.h></iostream>
这篇关于从本地管理员组打印条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!