0

I want to get users of a group using NetGroupGetUsers function.

DWORD dwError = 0;
NET_API_STATUS nStatus;
LPDWORD entriesread=0;
LPDWORD totalentries=0;
LPBYTE *buff;
nStatus=NetGroupGetUsers(NULL,L"Users",0,buff,MAX_PREFERRED_LENGTH,
                          entriesread,totalentries,NULL);

When i use this i get this error;

xC0000005: Access violation reading location 0xffffffffffffffff.

And how can i read buff when it works? And also i tried this;

GROUP_USERS_INFO_0 *buff;
nStatus=NetGroupGetUsers(NULL,L"Users",0,(LPBYTE*)&buff,MAX_PREFERRED_LENGTH,
entriesread,totalentries,NULL);

but same error occurred.

EDIT: nStatus value is NERR_GroupNotFound i think the reason of the access violation is trying to read buff which isn't actually set.

EDIT 2: i used this function NetLocalGroupGetMembers. now it gives success but the buff->grui0_name is meaningless. there is a user named "ali" but the value of the buff->grui0_name is just "d". WHAT AM I DOING WRONG?

The Last Code;

LPCWSTR TargetGroup = L"group1";

DWORD dwError = 0;
NET_API_STATUS stat;

GROUP_USERS_INFO_0 *buff;
LPDWORD entriesread=new DWORD;
LPDWORD totalentries=new DWORD;

stat=NetGroupGetUsers(NULL,TargetGroup,0,(LPBYTE *)&buff,MAX_PREFERRED_LENGTH,
                          entriesread,totalentries,NULL);

for EDIT 2;

stat=NetLocalGroupGetMembers(NULL,TargetGroup,0,(LPBYTE *)&buff,
                  MAX_PREFERRED_LENGTH,entriesread,totalentries,NULL);

Please help...

iskorum
  • 1,137
  • 1
  • 16
  • 27
  • I suspect you need to pass non-null values for entriesread and totalentries. – Alan Stokes Nov 02 '11 at 09:56
  • i tried like this; LPDWORD entriesread=new DWORD; is it true? it still doesn't work. – iskorum Nov 02 '11 at 10:24
  • @isokorum You're probably better off with `DWORD entriesread=0, totalentries=0; NetGroupGetUsers(..., &entriesread, &totalentries, ...);` – Alan Stokes Nov 02 '11 at 12:24
  • yeah thanks its worked in two ways too. i figured out my problem. i controlled nStatus variable it is NERR_GroupNotFound. Now i am getting crazy with this. i also add a new group as group1 and add one user. but it couldn't find the group again.i don't know what to do. – iskorum Nov 02 '11 at 12:34
  • i mean the reason of access violation is the buff isn't have anything. – iskorum Nov 02 '11 at 13:28
  • Please show us your new code. – Harry Johnston Nov 02 '11 at 21:13
  • 1
    You're passing level 0 to NetLocalGroupGetMembers, so you're getting an array of SID structures, not an array of strings. Pass level 1 and make buff a pointer to LOCALGROUP_MEMBERS_INFO_1. – Harry Johnston Nov 02 '11 at 23:15
  • Wow i am such an idiot. its Worked. Thanks a lot. Thanks to everybody. – iskorum Nov 03 '11 at 13:59

3 Answers3

1

Here is the version without new:

LPCWSTR TargetGroup = L"group1";

NET_API_STATUS stat;

LOCALGROUP_MEMBERS_INFO_1 *buff;
DWORD entriesread;
DWORD totalentries;

stat = NetLocalGroupGetMembers(NULL,TargetGroup,1,(LPBYTE *)&buff,
    MAX_PREFERRED_LENGTH,&entriesread,&totalentries,NULL);

wprintf(buff->lgrmi1_name);
Tobias Wollgam
  • 761
  • 2
  • 8
  • 25
1

You haven't allocated any memory for buff. You should be writing

GROUP_USERS_INFO_0 *buff;
... NetGroupGetUsers(..., (LPBYTE*)&buff, ...);

otherwise you're telling NetGroupGetUsers to write the results to a garbage location. Note that the bufptr parameter is documented as [out]. That means that it is the caller's responsibility to specify where the result should go. There's more to calling a function than just getting the types to match.

I'm surprised you didn't get an "use of initialized variable" warning from the compiler.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • Yeah i tried this too. Now there is no access violation but status `NetGroupGetUsers` function returned is `NERR_GroupNotFound`. Thanks. i think i should edit it. – iskorum Nov 02 '11 at 13:49
  • And in the MSDN reference says "The system allocates the memory for this buffer". – iskorum Nov 02 '11 at 13:56
  • 1
    Right, the system allocates the memory, and it uses the `bufptr` parameter to tell you where that memory is. You in turn have to allocate memory to hold that pointer, so that you can receive the answer. That's why it's a pointer to a pointer. (Remember, in C, all parameters are passed by value, not reference.) – Raymond Chen Nov 02 '11 at 14:16
  • Yes thanks and Sorry i am trying to solve this meaningless problem for hours so i am tired. – iskorum Nov 02 '11 at 14:21
0
LPCWSTR TargetGroup = L"group1";

NET_API_STATUS stat;

LOCALGROUP_MEMBERS_INFO_1 *buff;
LPDWORD entriesread=new DWORD;
LPDWORD totalentries=new DWORD;

stat=NetLocalGroupGetMembers(NULL,TargetGroup,1,(LPBYTE *)&buff,
                        MAX_PREFERRED_LENGTH,entriesread,totalentries,NULL);



wprintf(buff->lgrmi1_name);

This code is working great now. Thanks to everybody.

iskorum
  • 1,137
  • 1
  • 16
  • 27
  • This code should not be copied, because it leaks and the `new DWORD` is not neccessary. I will add a fixed version as answer. – Tobias Wollgam Jun 11 '19 at 11:48