forked from tmthrgd/DLL-Injector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessHandle.cs
45 lines (36 loc) · 1.12 KB
/
ProcessHandle.cs
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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Com.Xenthrax.DllInjector
{
public partial class DllInjector : IDisposable
{
private IntPtr hProc = IntPtr.Zero;
public void AcquireProcessHandle()
{
if (this.hProc != IntPtr.Zero)
return;
this.ThrowIfDisposed();
this.ThrowIfNoProcess();
try
{
Process.EnterDebugMode();
}
catch (Exception e)
{
Utilities.Log("DllInjector may fail if injecting into system process'.", e);
}
this.hProc = Win32.OpenProcess(Win32.ProcessAccess.PROCESS_CREATE_THREAD | Win32.ProcessAccess.PROCESS_VM_OPERATION | Win32.ProcessAccess.PROCESS_VM_WRITE | Win32.ProcessAccess.PROCESS_VM_READ | Win32.ProcessAccess.PROCESS_QUERY_INFORMATION, false, (uint)this.Process.Id);
if (this.hProc == IntPtr.Zero)
throw new Win32Exception();
}
public void ReleaseProcessHandle()
{
this.ThrowIfDisposed();
if (this.hProc != IntPtr.Zero && !Win32.CloseHandle(this.hProc))
throw new Win32Exception();
this.hProc = IntPtr.Zero;
}
}
}