Repository navigation
Expand file tree
/
Copy pathSafeMemoryHandle.cs
More file actions
44 lines (41 loc) · 1.65 KB
/
Copy pathSafeMemoryHandle.cs
File metadata and controls
44 lines (41 loc) · 1.65 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
using System;
using System.Runtime.ConstrainedExecution;
using System.Security.Permissions;
using Microsoft.Win32.SafeHandles;
namespace Process.NET.Native.Types
{
/// <summary>
/// Represents a Win32 handle safely managed.
/// </summary>
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
public sealed class SafeMemoryHandle : SafeHandleZeroOrMinusOneIsInvalid
{
/// <summary>
/// Parameterless constructor for handles built by the system (like <see cref="NativeMethods.OpenProcess" />).
/// </summary>
public SafeMemoryHandle() : base(true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SafeMemoryHandle" /> class, specifying the handle to keep in safe.
/// </summary>
/// <param name="handle">The handle to keep in safe.</param>
public SafeMemoryHandle(IntPtr handle) : base(true)
{
SetHandle(handle);
}
/// <summary>
/// Executes the code required to free the handle.
/// </summary>
/// <returns>
/// True if the handle is released successfully; otherwise, in the event of a catastrophic failure, false. In this
/// case, it generates a releaseHandleFailed MDA Managed Debugging Assistant.
/// </returns>
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
protected override bool ReleaseHandle()
{
// Check whether the handle is set AND whether the handle has been successfully closed
return handle != IntPtr.Zero && Kernel32.CloseHandle(handle);
}
}
}