C#使用winAPI获取windows用户组成员

C# Signature:

        /// <summary>
        /// The NetLocalGroupGetMembers function retrieves a list of the members of a particular local group in
        /// the security database, which is the security accounts manager (SAM) database or, in the case
        /// of domain controllers, the Active Directory. Local group members can be users or global groups.
        /// </summary>
        /// <param name="servername"></param>
        /// <param name="localgroupname"></param>
        /// <param name="level"></param>
        /// <param name="bufptr"></param>
        /// <param name="prefmaxlen"></param>
        /// <param name="entriesread"></param>
        /// <param name="totalentries"></param>
        /// <param name="resume_handle"></param>
        /// <returns></returns>
        [DllImport("NetAPI32.dll", CharSet=CharSet.Unicode)]
        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,
            ref int resume_handle);

VB Signature:

Declare Function NetLocalGroupGetMembers Lib "netapi32.dll" (TODO) As TODO

User-Defined Types:

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
            public struct LOCALGROUP_MEMBERS_INFO_2
        {
            public int lgrmi2_sid;
            public int lgrmi2_sidusage;
            public string lgrmi2_domainandname;
        }

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

        //Example code for a class file or dll( I used a dll)
        public static ArrayList GetLocalGroupMembers(string ServerName,string GroupName)
        {
            ArrayList myList = new ArrayList();
            int EntriesRead;
            int TotalEntries;
            int Resume;
            IntPtr bufPtr;
            val = NetLocalGroupGetMembers(ServerName, GroupName, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);
            if(EntriesRead> 0)
            {
                LOCALGROUP_MEMBERS_INFO_2[] Members = new LOCALGROUP_MEMBERS_INFO_2[EntriesRead];
                IntPtr iter = bufPtr;
                for(int i=0; i < EntriesRead; i++)
                {
                    Members[i] = (LOCALGROUP_MEMBERS_INFO_2)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_MEMBERS_INFO_2));
                    iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_MEMBERS_INFO_2)));
                    myList.Add(Members[i].lgrmi2_domainandname + "," + Members[i].lgrmi2_sidusage);
                }
                NetApiBufferFree(bufPtr);
            }
            return myList;
        }

        //Example code for calling application
        ArrayList RetGroups = new ArrayList();
        RetGroups =DsDomain.GetLocalGroupMembers(null,"administrators");        
        string delimStr = ",";
        char [] delimiter = delimStr.ToCharArray();    
        foreach(string str in RetGroups)
    {        //I used this to retrieve the type of group so I could use an icon in a treeview if desired.
            //split[0] is the "domain/group or user", split[1] is the type, 1 for user, 2 for group.                
        string [] split = null;
        split = str.Split(delimiter);
        Console.WriteLine(split[0] + split[1]);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huajian2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值