forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeRegistryHandle.cs
More file actions
54 lines (47 loc) · 1.67 KB
/
SafeRegistryHandle.cs
File metadata and controls
54 lines (47 loc) · 1.67 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// NOTE: A vast majority of this code was copied from BCL in
// Namespace: Microsoft.Win32.SafeHandles
//
/*============================================================
**
**
**
** A wrapper for registry handles
**
**
===========================================================*/
using System;
using System.Management.Automation;
using System.Security;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Microsoft.Win32.SafeHandles;
using System.Runtime.ConstrainedExecution;
using System.Security.Permissions;
namespace Microsoft.PowerShell.Commands.Internal
{
internal sealed class SafeRegistryHandle : SafeHandleZeroOrMinusOneIsInvalid
{
// Note: Officially -1 is the recommended invalid handle value for
// registry keys, but we'll also get back 0 as an invalid handle from
// RegOpenKeyEx.
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
internal SafeRegistryHandle() : base(true) { }
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
internal SafeRegistryHandle(IntPtr preexistingHandle, bool ownsHandle) : base(ownsHandle)
{
SetHandle(preexistingHandle);
}
[DllImport(PinvokeDllNames.RegCloseKeyDllName),
SuppressUnmanagedCodeSecurity,
ResourceExposure(ResourceScope.None),
ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal static extern int RegCloseKey(IntPtr hKey);
protected override bool ReleaseHandle()
{
// Returns a Win32 error code, 0 for success
int r = RegCloseKey(handle);
return r == 0;
}
}
}