marsh-wiggle
marsh-wiggle

Reputation: 2803

PInvoke NetLocalGroupGetMembers runs into FatalExecutionEngineError

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);
    }
}

Upvotes: 2

Views: 1220

Answers (2)

marsh-wiggle
marsh-wiggle

Reputation: 2803

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);
    }

}

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612844

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.

Upvotes: 3

Related Questions