public static int TryGetSidsOfLocalGroupMembers(string serverName, string localGroupName, out List <SecurityIdentifier> sids) { if (string.IsNullOrEmpty(localGroupName)) { throw new ArgumentNullException("localGroupName"); } sids = new List <SecurityIdentifier>(); // Note: serverName can be (and usually is) null. uint entriesRead; uint totalEntries; IntPtr resumeHandle = IntPtr.Zero; IntPtr bufPtr = IntPtr.Zero; try { int returnCode = Win32GroupInterop.NetLocalGroupGetMembers(serverName, localGroupName, 0, // level 0. return the security identifier (SID) associated with the local group member. The bufptr parameter points to an array of LOCALGROUP_MEMBERS_INFO_0 structures out bufPtr, uint.MaxValue, // maximum preferred length. The method MUST allocate as much space as the data requires. out entriesRead, out totalEntries, out resumeHandle); if (returnCode != Win32GroupInterop.ReturnCode.S_OK) { return(returnCode); //if (returnCode == Win32GroupInterop.ReturnCode.ERROR_ACCESS_DENIED) //{ // //throw new UnauthorizedAccessException(AdminResources.AccessDenied()); //} //else if (returnCode == Win32GroupInterop.ReturnCode.NERR_GroupNotFound || // returnCode == Win32GroupInterop.ReturnCode.ERROR_NO_SUCH_ALIAS) //{ // //throw new ArgumentException(AdminResources.GroupNotExist(), "localGroupName"); //} ////throw new ConfigurationException(AdminResources.ErrorOperationWithReturnCode("NetLocalGroupGetMembers", returnCode.ToString(CultureInfo.CurrentCulture))); } for (int index = 0; index < entriesRead; ++index) { IntPtr ptr = new IntPtr((long)bufPtr + Marshal.SizeOf(typeof(Win32GroupInterop.LocalGroupMemberInfo0)) * index); Win32GroupInterop.LocalGroupMemberInfo0 groupMemberInfo = (Win32GroupInterop.LocalGroupMemberInfo0)Marshal.PtrToStructure(ptr, typeof(Win32GroupInterop.LocalGroupMemberInfo0)); SecurityIdentifier sid = new SecurityIdentifier(groupMemberInfo.Sid); sids.Add(sid); } return(Win32GroupInterop.ReturnCode.S_OK); } finally { if (bufPtr != IntPtr.Zero) { int rc = Win32GroupInterop.NetApiBufferFree(bufPtr); if (rc != Win32GroupInterop.ReturnCode.S_OK) { TraceManager.TraceError("Failed to free buffer returned by NetLocalGroupGetMembers(). Error: {0}", rc); } } } }