public void RemoveFromLocalGroup(string groupName) { var sidBytes = new byte[Sid.BinaryLength]; Sid.GetBinaryForm(sidBytes, 0); var info3 = new LOCALGROUP_MEMBERS_INFO_0 { pSID = Marshal.AllocHGlobal(sidBytes.Length) }; try { Marshal.Copy(sidBytes, 0, info3.pSID, sidBytes.Length); var result = NetLocalGroupDelMembers(null, groupName, 0, ref info3, 1); if (result == Win32ErrorCodes.NERR_Success || result == Win32ErrorCodes.MemberNotInAlias) { return; } throw new Win32Exception(result); } finally { Marshal.FreeHGlobal(info3.pSID); } }
public override byte[] Encode() { if (UpnOffset == null) { UpnOffset = 16; } if (UpnLength == null) { UpnLength = (short)(Upn.Length * 2); } if (DnsDomainNameLen == null) { DnsDomainNameLen = (short)(DnsDomainName.Length * 2); } if (DnsDomainNameOffset == null) { DnsDomainNameOffset = (short)(UpnOffset + UpnLength); } BinaryWriter bw = new BinaryWriter(new MemoryStream()); bw.Write((short)UpnLength); bw.Write((short)UpnOffset); bw.Write((short)DnsDomainNameLen); bw.Write((short)DnsDomainNameOffset); bw.Write((int)Flags); if (Flags.HasFlag(Interop.UpnDnsFlags.EXTENDED)) { bw.Write((short)SamNameLength); bw.Write((short)SamNameOffset); bw.Write((short)SidLength); bw.Write((short)SidOffset); } bw.BaseStream.Position = (long)UpnOffset; bw.Write(Encoding.Unicode.GetBytes(Upn)); bw.BaseStream.Position = (long)DnsDomainNameOffset; bw.Write(Encoding.Unicode.GetBytes(DnsDomainName)); if (Flags.HasFlag(Interop.UpnDnsFlags.EXTENDED)) { bw.BaseStream.Position = (long)SamNameOffset; bw.Write(Encoding.Unicode.GetBytes(SamName)); bw.BaseStream.Position = (long)SidOffset; byte[] sidBytes = new byte[Sid.BinaryLength]; Sid.GetBinaryForm(sidBytes, 0); bw.Write(sidBytes); } if (Junk != null) { bw.Write(Junk); } byte[] data = ((MemoryStream)bw.BaseStream).ToArray(); return(data); }