public AccessControlList(AccessControlEntry[] aces) { // parameters validation if (aces == null) throw new ArgumentNullException("aces"); // create aces list int listSize; LocalAllocHandle pAceList = CreateAceList(aces, out listSize); // allocate memory for ACL int aclSize = SecurityNative.ACL.Size + listSize; _pAcl = new LocalAllocHandle(aclSize); // intialize ACL if (!SecurityNative.InitializeAcl(_pAcl, (uint)aclSize, SecurityNative.ACL_REVISION)) throw new Win32Exception(Marshal.GetLastWin32Error()); // add aces to ACL if (!SecurityNative.AddAce(_pAcl, SecurityNative.ACL_REVISION, 0, pAceList, (uint)listSize)) throw new Win32Exception(Marshal.GetLastWin32Error()); _revision = (int)SecurityNative.ACL_REVISION; _size = aclSize; _count = aces.Length; _aces = aces; }
/// <summary> /// Read ACEs from ACL in memory /// </summary> private static AccessControlEntry[] GetAces(IntPtr pAcl, int count) { AccessControlEntry[] aces = new AccessControlEntry[count]; IntPtr pAce; for (int i = 0; i < count; ++i) { if (!SecurityNative.GetAce(pAcl, (uint)i, out pAce)) throw new Win32Exception(Marshal.GetLastWin32Error()); aces[i] = new AccessControlEntry(pAce); } return aces; }
/// <summary> /// Creates ACE list in memory /// </summary> private static LocalAllocHandle CreateAceList(AccessControlEntry[] aces, out int listSize) { // parameters validation if (aces == null) throw new ArgumentNullException("aces"); // calculate list size listSize = 0; for (int i = 0; i < aces.Length; ++i) { listSize += aces[i].Size; } // allocate buffer for aces LocalAllocHandle pAceList = new LocalAllocHandle(listSize); // write aces to buffer IntPtr pAce = pAceList; for (int i = 0; i < aces.Length; ++i) { aces[i].UnsafeWrite(pAce); pAce = IntPtrHelper.Add(pAce, aces[i].Size); } return pAceList; }