public Privilege(string privilegeName) { if (privilegeName == null) { throw new ArgumentNullException("privilegeName"); } this.luid = LuidFromPrivilege(privilegeName); }
private static Win32Native.LUID LuidFromPrivilege(string privilege) { Win32Native.LUID luid; luid.LowPart = 0; luid.HighPart = 0; RuntimeHelpers.PrepareConstrainedRegions(); try { privilegeLock.AcquireReaderLock(-1); if (luids.Contains(privilege)) { luid = (Win32Native.LUID) luids[privilege]; privilegeLock.ReleaseReaderLock(); return luid; } privilegeLock.ReleaseReaderLock(); if (!Win32Native.LookupPrivilegeValue(null, privilege, ref luid)) { switch (Marshal.GetLastWin32Error()) { case 8: throw new OutOfMemoryException(); case 5: throw new UnauthorizedAccessException(); case 0x521: throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPrivilegeName", new object[] { privilege })); } throw new InvalidOperationException(); } privilegeLock.AcquireWriterLock(-1); } finally { if (privilegeLock.IsReaderLockHeld) { privilegeLock.ReleaseReaderLock(); } if (privilegeLock.IsWriterLockHeld) { if (!luids.Contains(privilege)) { luids[privilege] = luid; privileges[luid] = privilege; } privilegeLock.ReleaseWriterLock(); } } return luid; }