Search code examples
c#winapipinvoke

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

Solution

  • 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.