How do I get the local groups users info from Windows Server 2012 R2?

94 Views Asked by At

I am able to get the local users in Windows 7 using the below code. But when I try the same code on Windows 2012 server it doesn't work. Please help with a solution.

private static String[] GetUserLocalGroups(string ServerName, string Username, int Flags)
{
    List<String> myList = new List<String>();
    int EntriesRead;
    int TotalEntries;
    IntPtr bufPtr;

    int nErrorCode = NetUserGetLocalGroups(ServerName, Username, 0, Flags, out bufPtr, 1024, out EntriesRead, out TotalEntries);
    if (nErrorCode != 0)
    {
        String errmsg = DllImportsCommon.GetErrorMessage(nErrorCode);
        throw new Exception(errmsg);
    }

    if (EntriesRead > 0)
    {
        LOCALGROUP_USERS_INFO_0[] RetGroups = new LOCALGROUP_USERS_INFO_0[EntriesRead];
        IntPtr iter = bufPtr;
        for (int i = 0; i < EntriesRead; i++)
        {
            RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0));
            iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0)));
            myList.Add(RetGroups[i].name);
        }
        NetApiBufferFree(bufPtr);
    }
    return myList.ToArray();
}

It fails in the below the following line:

RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0));
0

There are 0 best solutions below