-
Notifications
You must be signed in to change notification settings - Fork 9
/
Utilities.cs
150 lines (125 loc) · 3.5 KB
/
Utilities.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Com.Xenthrax.DllInjector
{
public partial class DllInjector : IDisposable
{
public int GetLastError()
{
this.ThrowIfDisposed();
this.ThrowIfNoHandle();
return this.Kernel32.GetLastError();
}
public static T StructToStruct<T>(object data)
{
return (T)StructToStruct(data, typeof(T));
}
public static object StructToStruct(object data, Type t)
{
if (data is SafeHandle)
data = ((SafeHandle)data).Value;
IntPtr ptr = Marshal.AllocHGlobal(Math.Max(Marshal.SizeOf(data), Marshal.SizeOf(t)));
try
{
Marshal.StructureToPtr(data, ptr, false);
return Marshal.PtrToStructure(ptr, t);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
}
private static class Utilities
{
// Dodgy
public static void Log(string msg)
{
ConsoleColor ForegroundColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(msg);
Console.ForegroundColor = ForegroundColor;
}
public static void Log(string format, params object[] args)
{
Log(string.Format(format, args));
}
public static void Log(string msg, Exception ex)
{
Log(string.Format("{0}\r\n{1}", msg, ex));
}
public static void Log(Exception ex)
{
Log(ex.ToString());
}
// Dodgy
public static void PreserveStackTrace(Exception exception)
{
typeof(Exception)
.GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(exception, null);
}
public static int Align(int i, int alignment)
{
return i + (alignment - (i % alignment)) % alignment;
}
public static bool DoesWin32MethodExist(string moduleName, string methodName)
{
IntPtr moduleHandle = Win32.GetModuleHandle(moduleName);
return moduleHandle != IntPtr.Zero
&& Win32.GetProcAddress(moduleHandle, methodName) != IntPtr.Zero;
}
public static bool IsWow64Process(Process process)
{
if (process == null)
throw new ArgumentNullException("process");
if (!Utilities.DoesWin32MethodExist("Kernel32.dll", "IsWow64Process"))
return false;
bool Wow64Process;
if (!Win32.IsWow64Process(process.Handle, out Wow64Process))
throw new Win32Exception();
return Wow64Process;
}
public static bool Is64BitProcess(Process process)
{
return Utilities.Is64BitOperatingSystem
&& !Utilities.IsWow64Process(process);
}
public static IntPtr OffsetOf<T>(IntPtr p, string s)
where T : struct
{
return Utilities.OffsetOf(p, typeof(T), s);
}
public static IntPtr OffsetOf(IntPtr p, Type o, string s)
{
return new IntPtr(p.ToInt64() + Marshal.OffsetOf(o, s).ToInt64());
}
private static bool? _Is64BitOperatingSystem;
public static bool Is64BitOperatingSystem
{
get
{
#if DOTNET_40
return Environment.Is64BitOperatingSystem;
#else
if (IntPtr.Size == 8)
return true;
if (!_Is64BitOperatingSystem.HasValue)
_Is64BitOperatingSystem = Utilities.IsWow64Process(Process.GetCurrentProcess());
return _Is64BitOperatingSystem.Value;
#endif
}
}
public static bool IsNt
{
get
{
return Environment.OSVersion.Platform == PlatformID.Win32NT;
}
}
}
}
}