private void MakeWriteableHandle(AccessControlSections sectionsToWrite) { StandardRights rightsRequired = m_HandleRights; bool newHandleRequired = false; if ((sectionsToWrite & AccessControlSections.Access) != 0 || (sectionsToWrite & AccessControlSections.Group) != 0) { if ((m_HandleRights & StandardRights.WritePermissions) == 0) { rightsRequired |= StandardRights.WritePermissions; newHandleRequired = true; } } if ((sectionsToWrite & AccessControlSections.Owner) != 0) { if ((m_HandleRights & StandardRights.TakeOwnership) == 0) { rightsRequired |= StandardRights.TakeOwnership; newHandleRequired = true; } } if ((sectionsToWrite & AccessControlSections.Audit) != 0) { if ((m_HandleRights & (StandardRights)NativeConstants.ACCESS_SYSTEM_SECURITY) == 0) { rightsRequired |= (StandardRights)NativeConstants.ACCESS_SYSTEM_SECURITY; newHandleRequired = true; } } if (newHandleRequired) { IntPtr writeHandle = NativeMethods.DuplicateHandle(m_Handle.DangerousGetHandle(), (int)rightsRequired); if (writeHandle == IntPtr.Zero) { int err = Marshal.GetLastWin32Error(); switch (err) { case NativeConstants.ERROR_ACCESS_DENIED: throw new UnauthorizedAccessException(); default: throw new System.ComponentModel.Win32Exception(err); } } try { m_Handle = new GenericSafeHandle(writeHandle, m_Handle); m_HandleRights = rightsRequired; } catch { //Should only happen if out of memory. Release new handle and rethrow. NativeMethods.CloseHandle(writeHandle); throw; } } }
/// <summary> /// Returns whether this handle holds at least given standard rights /// </summary> /// <param name="rights">The minimum access rights required</param> /// <returns>A boolean indicating whether this handle holds at minimum given access rights</returns> public Boolean HasRights(StandardRights rights) { return(HasRights((uint)rights)); }