//
// Copyright © Nick Lowe 2009
//
// Nick Lowe
// nick@int-r.net
// http://processprivileges.codeplex.com/
namespace ProcessPrivileges
{
using System.ComponentModel;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
/// Handle to an access token.
public sealed class AccessTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
{
internal AccessTokenHandle(ProcessHandle processHandle, TokenAccessRights tokenAccessRights)
: base(true)
{
if (!NativeMethods.OpenProcessToken(processHandle, tokenAccessRights, ref handle))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
/// Releases the handle.
/// Value indicating if the handle released successfully.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
protected override bool ReleaseHandle()
{
if (!NativeMethods.CloseHandle(handle))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return true;
}
}
}