Let's assume you have Unicode arrays declared like this:
WCHAR wcUser [UNLEN]; WCHAR wcAdmGroup [64];
In one you put the name of the user (use GetUserName to fetch the current user. If you're an ANSI app you'll need to use MultiByteToWideChar to convert to Unicode). Similarly get the server name and convert that to Unicode, then put it in wcServer. Now we could use code like that below to determine if wcUser is a member of wcAdmGroup. I've removed the error handling code for clarity.
#include "lm.h" #define MAX_GROUPS 30 NET_API_STATUS nasReturn; WCHAR * pwcBuffer ; GROUP_USERS_INFO_0 UserInfo [MAX_GROUPS]; GROUP_USERS_INFO_0 * pUserInfo = UserInfo; DWORD dwEntriesRead ; DWORD dwTotalEntries ; DWORD dwBufferSize = GNLEN*MAX_GROUPS*2; DWORD dwCount ; bResult = FALSE; // Allocate memory for group list buffer. nasReturn = NetApiBufferAllocate (dwBufferSize, (void**)&pwcBuffer); // Set the structure pointers to point at our buffer. for (dwCount=0; dwCount<MAX_GROUPS; dwCount++) { UserInfo [dwCount].grui0_name = &(pwcBuffer[dwCount*GNLEN]); } nasReturn = NetUserGetGroups (wcServer, wcUser, 0, (LPBYTE*) &pUserInfo, dwBufferSize, &dwEntriesRead, &dwTotalEntries); switch (nasReturn) { case ERROR_MORE_DATA: // Need a bigger buffer !! break; case NERR_Success : // Loop through the available entries, comparing // the contents of grui0_name with the pre-defined // group. for (dwCount=0; dwCount<dwEntriesRead; dwCount++) { if (_wcsicmp (pUserInfo[dwCount].grui0_name,wcAdmGroup) == 0) { bResult = TRUE; break; } } break ; default: // handle error code in nasReturn break ; }
bResult is now TRUE if the user is in the group. If you need to convert to Unicode, here's a sample call to MultiByteToWideChar:
MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, szBuffer, -1, wcAdmGroup, sizeof (wcAdmGroup));
That takes the contents of szBuffer and converts it into Unicode in the WCHAR array wcAdmGroup.
Download