-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAccessTokenHandle.cs
More file actions
40 lines (36 loc) · 1.33 KB
/
AccessTokenHandle.cs
File metadata and controls
40 lines (36 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// <copyright file="AccessTokenHandle.cs" company="Nick Lowe">
// Copyright © Nick Lowe 2009
// </copyright>
// <author>Nick Lowe</author>
// <email>nick@int-r.net</email>
// <url>http://processprivileges.codeplex.com/</url>
namespace ProcessPrivileges
{
using System.ComponentModel;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
/// <summary>Handle to an access token.</summary>
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());
}
}
/// <summary>Releases the handle.</summary>
/// <returns>Value indicating if the handle released successfully.</returns>
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
protected override bool ReleaseHandle()
{
if (!NativeMethods.CloseHandle(handle))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return true;
}
}
}