Repository navigation
Expand file tree
/
Copy pathHandleManipulator.cs
More file actions
134 lines (117 loc) · 5.49 KB
/
Copy pathHandleManipulator.cs
File metadata and controls
134 lines (117 loc) · 5.49 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Process.NET.Native;
using Process.NET.Native.Types;
namespace Process.NET.Utilities
{
/// <summary>
/// Static helper class providing tools for manipulating handles.
/// </summary>
public static class HandleManipulator
{
/// <summary>
/// Closes an open object handle.
/// </summary>
/// <param name="handle">A valid handle to an open object.</param>
public static void CloseHandle(IntPtr handle)
{
// Check if the handle is valid
ValidateAsArgument(handle, "handle");
// Close the handle
if (!Kernel32.CloseHandle(handle))
throw new Win32Exception("Couldn't close the handle correctly.");
}
/// <summary>
/// Converts an handle into a <see cref="Process" /> object assuming this is a process handle.
/// </summary>
/// <param name="processHandle">A valid handle to an opened process.</param>
/// <returns>A <see cref="Process" /> object from the specified handle.</returns>
public static System.Diagnostics.Process HandleToProcess(SafeMemoryHandle processHandle)
{
// Search the process by iterating the processes list
return System.Diagnostics.Process.GetProcesses().First(p => p.Id == HandleToProcessId(processHandle));
}
/// <summary>
/// Converts an handle into a process id assuming this is a process handle.
/// </summary>
/// <param name="processHandle">A valid handle to an opened process.</param>
/// <returns>A process id from the specified handle.</returns>
public static int HandleToProcessId(SafeMemoryHandle processHandle)
{
// Check if the handle is valid
ValidateAsArgument(processHandle, "processHandle");
// Find the process id
var ret = Kernel32.GetProcessId(processHandle);
// If the process id is valid
if (ret != 0)
return ret;
// Else the function failed, throws an exception
throw new Win32Exception("Couldn't find the process id of the specified handle.");
}
/// <summary>
/// Converts an handle into a <see cref="ProcessThread" /> object assuming this is a thread handle.
/// </summary>
/// <param name="threadHandle">A valid handle to an opened thread.</param>
/// <returns>A <see cref="ProcessThread" /> object from the specified handle.</returns>
public static ProcessThread HandleToThread(SafeMemoryHandle threadHandle)
{
// Search the thread by iterating the processes list
foreach (var process in System.Diagnostics.Process.GetProcesses())
{
var ret =
process.Threads.Cast<ProcessThread>().FirstOrDefault(t => t.Id == HandleToThreadId(threadHandle));
if (ret != null)
return ret;
}
// If no thread was found, throws a exception like the First() function with no element
throw new InvalidOperationException("Sequence contains no matching element");
}
/// <summary>
/// Converts an handle into a thread id assuming this is a thread handle.
/// </summary>
/// <param name="threadHandle">A valid handle to an opened thread.</param>
/// <returns>A thread id from the specified handle.</returns>
public static int HandleToThreadId(SafeMemoryHandle threadHandle)
{
// Check if the handle is valid
ValidateAsArgument(threadHandle, "threadHandle");
// Find the thread id
var ret = Kernel32.GetThreadId(threadHandle);
// If the thread id is valid
if (ret != 0)
return ret;
//Else the function failed, throws an exception
throw new Win32Exception("Couldn't find the thread id of the specified handle.");
}
/// <summary>
/// Validates an handle to fit correctly as argument.
/// </summary>
/// <param name="handle">A handle to validate.</param>
/// <param name="argumentName">The name of the argument that represents the handle in its original function.</param>
public static void ValidateAsArgument(IntPtr handle, string argumentName)
{
// Check if the handle is not null
if (handle == null)
throw new ArgumentNullException(argumentName);
// Check if the handle is valid
if (handle == IntPtr.Zero)
throw new ArgumentException("The handle is not valid.", argumentName);
}
/// <summary>
/// Validates an handle to fit correctly as argument.
/// </summary>
/// <param name="handle">A handle to validate.</param>
/// <param name="argumentName">The name of the argument that represents the handle in its original function.</param>
public static void ValidateAsArgument(SafeMemoryHandle handle, string argumentName)
{
// Check if the handle is not null
if (handle == null)
throw new ArgumentNullException(argumentName);
// Check if the handle is valid
if (handle.IsClosed || handle.IsInvalid)
throw new ArgumentException("The handle is not valid or closed.", argumentName);
}
}
}