2

I need to use win32 NetLocalGroupGetMembers in C#. I found and tested three solutions. All three fail with an FatalExecutionEngineError. The framework is .net 4.0

Here is a full example:

Reference to the api:

static class NetworkAPI
{
    [DllImport("Netapi32.dll")]
    public extern static int NetLocalGroupGetMembers([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string localgroupname, int level, out IntPtr bufptr, int prefmaxlen, out int entriesread, out int totalentries, out int resumehandle);

    [DllImport("Netapi32.dll")]
    public extern static int NetApiBufferFree(IntPtr Buffer);

    // LOCALGROUP_MEMBERS_INFO_1 - Structure for holding members details
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct LOCALGROUP_MEMBERS_INFO_1
    {
        public int lgrmi1_sid;
        public int lgrmi1_sidusage;
        public string lgrmi1_name;
    }
}

calling the function:

static void Main(string[] args)
{
    int EntriesRead;
    int TotalEntries;
    int Resume;
    IntPtr bufPtr;

    string groupName = "Administrators";

    NetworkAPI.NetLocalGroupGetMembers(null, groupName, 1, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);

    if (EntriesRead > 0)
    {
        NetworkAPI.LOCALGROUP_MEMBERS_INFO_1[] Members = new NetworkAPI.LOCALGROUP_MEMBERS_INFO_1[EntriesRead];
        IntPtr iter = bufPtr;

        // EntriesRead has the correct quantity of members of the group, so the group is found
        for (int i = 0; i < EntriesRead; i++)
        {
            // --------------------------------------------------
            // ==> here the FatalExecutionEngineError happens:
            Members[i] = (NetworkAPI.LOCALGROUP_MEMBERS_INFO_1)Marshal.PtrToStructure(iter, typeof(NetworkAPI.LOCALGROUP_MEMBERS_INFO_1));
            //
            // --------------------------------------------------

            iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(NetworkAPI.LOCALGROUP_MEMBERS_INFO_1)));
            Console.WriteLine(Members[i].lgrmi1_name);
        }
        NetworkAPI.NetApiBufferFree(bufPtr);
    }
}
3
  • 1
    You do not need to use a resume handle for local group membership. I believe this is something you will use if you want to enumerate group members on domain controllers where groups can become very large. The P/Invoke signature of the resume handle argument is a bit tricky because you need to change it if you intend to use it. If you do not want to use a resume handle you can simply pass NULL as the argument and to do that you should declare resumehandle as an IntPtr and pass IntPtr.Zero for the value. Nov 26, 2015 at 12:00
  • @MartinLiversage After the changes suggested by David Heffernan it works. Should I nevertheless change the resume parameter? Nov 26, 2015 at 13:39
  • 1
    You can use the resume handle if you intend to read the information in smaller parts. As I said earlier this is probably something you want to do if the group is very big (e.g. on a domain controller) and takes time to retrieve. Otherwise, you just get all the group information in one operation. Your code is not capable of resuming (there is no loop) so you might as well pass NULL for the resumehandle. But passing a reference to a handle initialized to 0 as described in the documentation also works even though you never use the resume handle. Nov 26, 2015 at 14:06

2 Answers 2

3

I see the following errors:

  1. The resume handle is a pointer. Use ref IntPtr resumehandle for that parameter, and pass IntPtr.Zero on the first call. Or if you don't need to use a resume handle declare the parameter as IntPtr resumehandle and pass IntPtr.Zero. Consult the function documentation on MSDN for the full details.
  2. The lgrmi1_sid member of the struct is a pointer. Declare it as such: public IntPtr lgrmi1_sid.
  3. Casting an IntPtr to an int will lead to pointer truncation on 64 bit. Either use arithmetic directly on the IntPtr, or for older C# versions cast to long. The former is better, like so: iter += Marshal.SizeOf(typeof(NetworkAPI.LOCALGROUP_MEMBERS_INFO_1));.
  4. You do not check the return value for errors.

Fix those errors and your program will run correctly.

3
0

For the sake of completeness, here is the code how to pinvoke NetLocalGroupGetMembers.

I corrected the code as David suggested. There is also a suggestion from Martin Liversage which I didn't implement. But it maybe usefull.

If you like it, please do not upvode this answer but upvote Davids answer, who found the errors.

Reference to the api:

public static class NetworkAPI
{
    [DllImport("Netapi32.dll")]
    public extern static uint NetLocalGroupGetMembers([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string localgroupname, int level, out IntPtr bufptr, int prefmaxlen, out int entriesread, out int totalentries, out IntPtr resumehandle);

    [DllImport("Netapi32.dll")]
    public extern static int NetApiBufferFree(IntPtr Buffer);

    // LOCALGROUP_MEMBERS_INFO_1 - Structure for holding members details
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct LOCALGROUP_MEMBERS_INFO_1
    {
        public IntPtr lgrmi1_sid;
        public int lgrmi1_sidusage;
        public string lgrmi1_name;
    }

    // documented in MSDN
    public const uint ERROR_ACCESS_DENIED = 0x0000005;
    public const uint ERROR_MORE_DATA = 0x00000EA;
    public const uint ERROR_NO_SUCH_ALIAS = 0x0000560;
    public const uint NERR_InvalidComputer = 0x000092F;

    // found by testing
    public const uint NERR_GroupNotFound = 0x00008AC;
    public const uint SERVER_UNAVAILABLE = 0x0006BA;
}

Calling the function:

static void Main(string[] args)
{
    int EntriesRead;
    int TotalEntries;
    IntPtr Resume;
    IntPtr bufPtr;

    string groupName = "Administratoren";
    string computerName = null; // null for the local machine

    uint retVal = NetworkAPI.NetLocalGroupGetMembers(computerName, groupName, 1, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);

    if(retVal != 0)
    {
        if (retVal == NetworkAPI.ERROR_ACCESS_DENIED) { Console.WriteLine("Access denied"); return; }
        if (retVal == NetworkAPI.ERROR_MORE_DATA) { Console.WriteLine("ERROR_MORE_DATA"); return; }
        if (retVal == NetworkAPI.ERROR_NO_SUCH_ALIAS) { Console.WriteLine("Group not found"); return; }
        if (retVal == NetworkAPI.NERR_InvalidComputer) { Console.WriteLine("Invalid computer name"); return; }
        if (retVal == NetworkAPI.NERR_GroupNotFound) { Console.WriteLine("Group not found"); return; }
        if (retVal == NetworkAPI.SERVER_UNAVAILABLE) { Console.WriteLine("Server unavailable"); return; }
        Console.WriteLine("Unexpected NET_API_STATUS: " + retVal.ToString()); 
        return;
    }

    if (EntriesRead > 0)
    {
        NetworkAPI.LOCALGROUP_MEMBERS_INFO_1[] Members = new NetworkAPI.LOCALGROUP_MEMBERS_INFO_1[EntriesRead];
        IntPtr iter = bufPtr;

        for (int i = 0; i < EntriesRead; i++)
        {
            Members[i] = (NetworkAPI.LOCALGROUP_MEMBERS_INFO_1)Marshal.PtrToStructure(iter, typeof(NetworkAPI.LOCALGROUP_MEMBERS_INFO_1));

            //x64 safe
            iter += Marshal.SizeOf(typeof(NetworkAPI.LOCALGROUP_MEMBERS_INFO_1));

            Console.WriteLine(Members[i].lgrmi1_name);
        }
        NetworkAPI.NetApiBufferFree(bufPtr);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.