public Privilege( string privilegeName ) { if ( privilegeName == null ) { throw new ArgumentNullException( "privilegeName" ); } this.luid = LuidFromPrivilege( privilegeName ); }
// // This routine is a wrapper around a hashtable containing mappings // of privilege names to luids // private static Luid LuidFromPrivilege( string privilege ) { Luid luid; luid.LowPart = 0; luid.HighPart = 0; // // Look up the privilege LUID inside the cache // try { privilegeLock.AcquireReaderLock(Timeout.Infinite); if ( luids.Contains( privilege )) { luid = ( Luid )luids[ privilege ]; privilegeLock.ReleaseReaderLock(); } else { privilegeLock.ReleaseReaderLock(); if ( false == NativeMethods.LookupPrivilegeValue( null, privilege, ref luid )) { int error = Marshal.GetLastWin32Error(); if ( error == NativeMethods.ERROR_NOT_ENOUGH_MEMORY ) { throw new OutOfMemoryException(); } else if ( error == NativeMethods.ERROR_ACCESS_DENIED ) { throw new UnauthorizedAccessException( "Caller does not have the rights to look up privilege local unique identifier" ); } else if ( error == NativeMethods.ERROR_NO_SUCH_PRIVILEGE ) { throw new ArgumentException( string.Format( "{0} is not a valid privilege name", privilege ), "privilege" ); } else { throw new Win32Exception( error ); } } privilegeLock.AcquireWriterLock(Timeout.Infinite); } } finally { if ( privilegeLock.IsReaderLockHeld ) { privilegeLock.ReleaseReaderLock(); } if ( privilegeLock.IsWriterLockHeld ) { if ( !luids.Contains( privilege )) { luids[ privilege ] = luid; privileges[ luid ] = privilege; } privilegeLock.ReleaseWriterLock(); } } return luid; }