Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace CHX_Framework
- {
- public static class ClockAPI
- {
- #region WINAPI
- public enum MemoryProtectionConstants : uint
- {
- PAGE_EXECUTE = 0x10,
- PAGE_EXECUTE_READ = 0x20,
- PAGE_EXECUTE_READWRITE = 0x40,
- PAGE_EXECUTE_WRITECOPY = 0x80,
- PAGE_NOACCESS = 0x01,
- PAGE_READONLY = 0x02,
- PAGE_READWRITE = 0x04,
- PAGE_WRITECOPY = 0x08,
- PAGE_TARGETS_INVALID = 0x40000000,
- PAGE_TARGETS_NO_UPDATE = 0x40000000,
- PAGE_GUARD = 0x100,
- PAGE_NOCACHE = 0x200,
- PAGE_WRITECOMBINE = 0x400
- }
- internal struct PROCESSENTRY32
- {
- public uint dwSize;
- public uint cntUsage;
- public uint th32ProcessID;
- public UIntPtr th32DefaultHeapID;
- public uint th32ModuleID;
- public uint cntThreads;
- public uint th32ParentProcessID;
- public uint pcPriClassBase;
- public uint dwFlags;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
- public string szExeFile;
- }
- private struct RECT
- {
- public int left;
- public int top;
- public int right;
- public int bottom;
- }
- private struct STARTUPINFO
- {
- public uint cb;
- public string lpReserved;
- public string lpDesktop;
- public string lpTitle;
- public uint dwX;
- public uint dwY;
- public uint dwXSize;
- public uint dwYSize;
- public uint dwXCountChars;
- public uint dwYCountChars;
- public uint dwFillAttribute;
- public uint dwFlags;
- public ushort wShowWindow;
- public ushort cbReserved2;
- public UIntPtr lpReserved2;
- public UIntPtr hStdInput;
- public UIntPtr hStdOutput;
- public UIntPtr hStdError;
- }
- private struct PROCESS_INFORMATION
- {
- public UIntPtr hProcess;
- public UIntPtr hThread;
- public uint dwProcessId;
- public uint dwThreadId;
- }
- private struct MEMORY_BASIC_INFORMATION
- {
- public UIntPtr BaseAddress;
- public UIntPtr AllocationBase;
- public uint AllocationProtect;
- public UIntPtr RegionSize;
- public uint State;
- public uint Protect;
- public uint Type;
- }
- internal struct MODULEENTRY32
- {
- public uint dwSize;
- public uint th32ModuleID;
- public uint th32ProcessID;
- public uint GlblcntUsage;
- public uint ProccntUsage;
- public UIntPtr modBaseAddr;
- public uint modBaseSize;
- public UIntPtr hModule;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
- public string szModule;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
- public string szExePath;
- }
- private struct MODULEINFO
- {
- public UIntPtr lpBaseOfDll;
- public uint SizeOfImage;
- public UIntPtr EntryPoint;
- }
- internal struct SYMBOL_INFO
- {
- public uint SizeOfStruct;
- public uint TypeIndex;
- public ulong Reserved1;
- public ulong Reserved2;
- public uint Index;
- public uint Size;
- public ulong ModBase;
- public uint Flags;
- public ulong Value;
- public ulong Address;
- public uint Register;
- public uint Scope;
- public uint Tag;
- public uint NameLen;
- public uint MaxNameLen;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
- public string Name;
- };
- internal struct THREADENTRY32
- {
- public uint dwSize;
- public uint cntUsage;
- public uint th32ThreadID;
- public uint th32OwnerProcessID;
- public uint tpBasePri;
- public uint tpDeltaPri;
- public uint dwFlags;
- }
- private delegate bool EnumWindowsProc(UIntPtr hwnd, UIntPtr lParam);
- private delegate bool SymEnumSymbolsProc(ref SYMBOL_INFO pSymInfo, uint SymbolSize, UIntPtr UserContext);
- [DllImport("kernel32.dll")]
- private static extern bool CloseHandle(UIntPtr hObject);
- [DllImport("kernel32.dll")]
- private static extern UIntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
- [DllImport("kernel32.dll")]
- private static extern bool Process32First(UIntPtr hSnapshot, ref PROCESSENTRY32 lppe);
- [DllImport("kernel32.dll")]
- private static extern bool Process32Next(UIntPtr hSnapshot, ref PROCESSENTRY32 lppe);
- [DllImport("kernel32.dll")]
- private static extern UIntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
- [DllImport("kernel32.dll")]
- private static extern bool TerminateProcess(UIntPtr hProcess, uint uExitCode);
- [DllImport("kernel32.dll")]
- private static extern uint GetCurrentProcessId();
- [DllImport("kernel32.dll")]
- private static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, UIntPtr lpProcessAttributes, UIntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, UIntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
- [DllImport("kernel32.dll")]
- private static extern bool VirtualProtectEx(UIntPtr hProcess, UIntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);
- [DllImport("kernel32.dll")]
- private static extern bool ReadProcessMemory(UIntPtr hProcess, UIntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out uint lpNumberOfBytesRead);
- [DllImport("kernel32.dll")]
- private static extern bool WriteProcessMemory(UIntPtr hProcess, UIntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out uint lpNumberOfBytesWritten);
- [DllImport("kernel32.dll")]
- private static extern uint VirtualQueryEx(UIntPtr hProcess, UIntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
- [DllImport("kernel32.dll")]
- private static extern UIntPtr VirtualAllocEx(UIntPtr hProcess, UIntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
- [DllImport("kernel32.dll")]
- private static extern UIntPtr VirtualFreeEx(UIntPtr hProcess, UIntPtr lpAddress, uint dwSize, uint dwFreeType);
- [DllImport("kernel32.dll")]
- private static extern bool Module32First(UIntPtr hSnapshot, ref MODULEENTRY32 lpme);
- [DllImport("kernel32.dll")]
- private static extern bool Module32Next(UIntPtr hSnapshot, ref MODULEENTRY32 lpme);
- [DllImport("kernel32.dll")]
- private static extern bool K32GetModuleInformation(UIntPtr hProcess, UIntPtr hModule, out MODULEINFO lpmodinfo, uint cb);
- [DllImport("user32.dll")]
- private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, UIntPtr lParam);
- [DllImport("user32.dll")]
- private static extern int GetWindowText(UIntPtr hWnd, StringBuilder lpString, int nMaxCount);
- [DllImport("user32.dll")]
- private static extern bool SetWindowText(UIntPtr hWnd, string lpString);
- [DllImport("user32.dll")]
- private static extern int GetWindowTextLength(UIntPtr hWnd);
- [DllImport("user32.dll")]
- private static extern bool SetWindowPos(UIntPtr hWnd, UIntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
- [DllImport("user32.dll")]
- private static extern uint GetWindowThreadProcessId(UIntPtr hWnd, out uint lpdwProcessId);
- [DllImport("user32.dll")]
- private static extern UIntPtr GetWindow(UIntPtr hWnd, uint uCmd);
- [DllImport("user32.dll")]
- private static extern UIntPtr ShowWindow(UIntPtr hWnd, int nCmdShow);
- [DllImport("user32.dll")]
- private static extern bool GetWindowRect(UIntPtr hWnd, out RECT lpRect);
- [DllImport("user32.dll")]
- private static extern bool GetClientRect(UIntPtr hWnd, out RECT lpRect);
- [DllImport("user32.dll")]
- private static extern bool IsWindowVisible(UIntPtr hWnd);
- [DllImport("user32.dll")]
- private static extern bool DestroyWindow(UIntPtr hWnd);
- [DllImport("user32.dll")]
- private static extern UIntPtr GetForegroundWindow();
- [DllImport("dbghelp.dll")]
- private static extern bool SymInitialize(UIntPtr hProcess, string UserSearchPath, bool fInvadeProcess);
- [DllImport("dbghelp.dll")]
- private static extern ulong SymLoadModuleEx(UIntPtr hProcess, UIntPtr hFile, string ImageName, string ModuleName, ulong BaseOfDll, uint DllSize, UIntPtr Data, uint Flags);
- [DllImport("dbghelp.dll")]
- private static extern bool SymEnumSymbols(UIntPtr hProcess, ulong BaseOfDll, string Mask, SymEnumSymbolsProc EnumSymbolsCallback, UIntPtr UserContext);
- [DllImport("dbghelp.dll")]
- private static extern bool SymCleanup(UIntPtr hProcess);
- [DllImport("kernel32.dll")]
- private static extern bool Thread32First(UIntPtr hSnapshot, ref THREADENTRY32 lpte);
- [DllImport("kernel32.dll")]
- private static extern bool Thread32Next(UIntPtr hSnapshot, ref THREADENTRY32 lpte);
- [DllImport("kernel32.dll")]
- private static extern UIntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
- [DllImport("ntdll.dll")]
- private static extern uint NtQueryInformationThread(UIntPtr ThreadHandle, uint ThreadInformationClass, IntPtr ThreadInformation, ulong ThreadInformationLength, ulong ReturnLength);
- [DllImport("kernel32.dll")]
- private static extern uint SuspendThread(UIntPtr hThread);
- [DllImport("kernel32.dll")]
- private static extern uint ResumeThread(UIntPtr hThread);
- [DllImport("kernel32.dll")]
- private static extern uint GetCurrentThreadId();
- [DllImport("kernel32.dll")]
- private static extern UIntPtr CreateRemoteThread(UIntPtr hProcess, UIntPtr lpThreadAttributes, uint dwStackSize, UIntPtr lpStartAddress, UIntPtr lpParameter, uint dwCreationFlags, out uint lpThreadId);
- [DllImport("kernel32.dll")]
- private static extern uint GetThreadId(UIntPtr Thread);
- [DllImport("kernel32.dll")]
- private static extern bool TerminateThread(UIntPtr hThread, uint dwExitCode);
- #endregion
- #region Miscellaneous
- public static T TypeOf<T>(byte[] Value)
- {
- if (typeof(T) == typeof(bool))
- return (T)Convert.ChangeType(BitConverter.ToBoolean(Value, 0), typeof(T));
- if (typeof(T) == typeof(bool[]))
- {
- bool[] A = new bool[Value.Length / SizeOf<bool>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<bool>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<bool>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(byte))
- return (T)Convert.ChangeType(Value[0], typeof(T));
- if (typeof(T) == typeof(char))
- return (T)Convert.ChangeType(TypeOf<char>(Value, Encoding.Default), typeof(T));
- if (typeof(T) == typeof(char[]))
- {
- char[] A = new char[Value.Length / SizeOf<char>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<char>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<char>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(double))
- return (T)Convert.ChangeType(BitConverter.ToDouble(Value, 0), typeof(T));
- if (typeof(T) == typeof(double[]))
- {
- double[] A = new double[Value.Length / SizeOf<double>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<double>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<double>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(sbyte))
- return (T)Convert.ChangeType((sbyte)Value[0], typeof(T));
- if (typeof(T) == typeof(sbyte[]))
- {
- sbyte[] A = new sbyte[Value.Length / SizeOf<sbyte>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<sbyte>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<sbyte>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(short))
- return (T)Convert.ChangeType(BitConverter.ToInt16(Value, 0), typeof(T));
- if (typeof(T) == typeof(short[]))
- {
- short[] A = new short[Value.Length / SizeOf<short>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<short>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<short>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(string))
- return (T)Convert.ChangeType(TypeOf<string>(Value, Encoding.Default), typeof(T));
- if (typeof(T) == typeof(int))
- return (T)Convert.ChangeType(BitConverter.ToInt32(Value, 0), typeof(T));
- if (typeof(T) == typeof(int[]))
- {
- int[] A = new int[Value.Length / SizeOf<int>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<int>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<int>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(long))
- return (T)Convert.ChangeType(BitConverter.ToInt64(Value, 0), typeof(T));
- if (typeof(T) == typeof(long[]))
- {
- long[] A = new long[Value.Length / SizeOf<long>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<long>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<long>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(float))
- return (T)Convert.ChangeType(BitConverter.ToSingle(Value, 0), typeof(T));
- if (typeof(T) == typeof(float[]))
- {
- float[] A = new float[Value.Length / SizeOf<float>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<float>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<float>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(ushort))
- return (T)Convert.ChangeType(BitConverter.ToUInt16(Value, 0), typeof(T));
- if (typeof(T) == typeof(ushort[]))
- {
- ushort[] A = new ushort[Value.Length / SizeOf<ushort>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<ushort>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<ushort>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(uint))
- return (T)Convert.ChangeType(BitConverter.ToUInt32(Value, 0), typeof(T));
- if (typeof(T) == typeof(uint[]))
- {
- uint[] A = new uint[Value.Length / SizeOf<uint>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<uint>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<uint>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(ulong))
- return (T)Convert.ChangeType(BitConverter.ToUInt64(Value, 0), typeof(T));
- if (typeof(T) == typeof(ulong[]))
- {
- ulong[] A = new ulong[Value.Length / SizeOf<ulong>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<ulong>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<ulong>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(IntPtr[]))
- {
- IntPtr[] A = new IntPtr[Value.Length / SizeOf<IntPtr>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<IntPtr>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<IntPtr>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(UIntPtr[]))
- {
- UIntPtr[] A = new UIntPtr[Value.Length / SizeOf<UIntPtr>()];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<UIntPtr>();
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<UIntPtr>(B.GetRange(D * C, C).ToArray());
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- else
- {
- T A;
- uint B = SizeOf<T>();
- IntPtr C = Marshal.AllocHGlobal((int)B);
- Marshal.Copy(Value, 0, C, (int)B);
- A = (T)Marshal.PtrToStructure(C, typeof(T));
- Marshal.FreeHGlobal(C);
- return A;
- }
- }
- public static T TypeOf<T>(byte[] Value, Encoding Encoding)
- {
- if (typeof(T) == typeof(char))
- return (T)Convert.ChangeType(Encoding.GetChars(Value)[0], typeof(T));
- if (typeof(T) == typeof(char[]))
- {
- char[] A = new char[Value.Length / SizeOf<char>(Encoding)];
- List<byte> B = new List<byte>(Value);
- int C = (int)SizeOf<char>(Encoding);
- for (int D = 0; D < A.Length; D++)
- {
- A[D] = TypeOf<char>(B.GetRange(D * C, C).ToArray(), Encoding);
- }
- return (T)Convert.ChangeType(A, typeof(T));
- }
- if (typeof(T) == typeof(string))
- return (T)Convert.ChangeType(Encoding.GetString(Value), typeof(T));
- else
- return TypeOf<T>(Value);
- }
- public static byte[] TypeOf<T>(T Value)
- {
- if (typeof(T) == typeof(bool))
- return BitConverter.GetBytes((bool)Convert.ChangeType(Value, typeof(bool)));
- if (typeof(T) == typeof(bool[]))
- {
- bool[] A = (bool[])Convert.ChangeType(Value, typeof(bool[]));
- List<byte> B = new List<byte>();
- foreach (bool C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(byte))
- return new byte[] { (byte)Convert.ChangeType(Value, typeof(byte)) };
- if (typeof(T) == typeof(char))
- return TypeOf((char)Convert.ChangeType(Value, typeof(char)), Encoding.Default);
- if (typeof(T) == typeof(char[]))
- {
- char[] A = (char[])Convert.ChangeType(Value, typeof(char[]));
- List<byte> B = new List<byte>();
- foreach (char C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(double))
- return BitConverter.GetBytes((double)Convert.ChangeType(Value, typeof(double)));
- if (typeof(T) == typeof(double[]))
- {
- double[] A = (double[])Convert.ChangeType(Value, typeof(double[]));
- List<byte> B = new List<byte>();
- foreach (double C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(sbyte))
- return new byte[] { (byte)(sbyte)Convert.ChangeType(Value, typeof(sbyte)) };
- if (typeof(T) == typeof(sbyte[]))
- {
- sbyte[] A = (sbyte[])Convert.ChangeType(Value, typeof(sbyte[]));
- List<byte> B = new List<byte>();
- foreach (sbyte C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(short))
- return BitConverter.GetBytes((short)Convert.ChangeType(Value, typeof(short)));
- if (typeof(T) == typeof(short[]))
- {
- short[] A = (short[])Convert.ChangeType(Value, typeof(short[]));
- List<byte> B = new List<byte>();
- foreach (short C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(string))
- return TypeOf((string)Convert.ChangeType(Value, typeof(string)), Encoding.Default);
- if (typeof(T) == typeof(int))
- return BitConverter.GetBytes((int)Convert.ChangeType(Value, typeof(int)));
- if (typeof(T) == typeof(int[]))
- {
- int[] A = (int[])Convert.ChangeType(Value, typeof(int[]));
- List<byte> B = new List<byte>();
- foreach (int C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(long))
- return BitConverter.GetBytes((long)Convert.ChangeType(Value, typeof(long)));
- if (typeof(T) == typeof(long[]))
- {
- long[] A = (long[])Convert.ChangeType(Value, typeof(long[]));
- List<byte> B = new List<byte>();
- foreach (long C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(float))
- return BitConverter.GetBytes((float)Convert.ChangeType(Value, typeof(float)));
- if (typeof(T) == typeof(float[]))
- {
- float[] A = (float[])Convert.ChangeType(Value, typeof(float[]));
- List<byte> B = new List<byte>();
- foreach (float C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(ushort))
- return BitConverter.GetBytes((ushort)Convert.ChangeType(Value, typeof(ushort)));
- if (typeof(T) == typeof(ushort[]))
- {
- ushort[] A = (ushort[])Convert.ChangeType(Value, typeof(ushort[]));
- List<byte> B = new List<byte>();
- foreach (ushort C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(uint))
- return BitConverter.GetBytes((uint)Convert.ChangeType(Value, typeof(uint)));
- if (typeof(T) == typeof(uint[]))
- {
- uint[] A = (uint[])Convert.ChangeType(Value, typeof(uint[]));
- List<byte> B = new List<byte>();
- foreach (uint C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(ulong))
- return BitConverter.GetBytes((ulong)Convert.ChangeType(Value, typeof(ulong)));
- if (typeof(T) == typeof(ulong[]))
- {
- ulong[] A = (ulong[])Convert.ChangeType(Value, typeof(ulong[]));
- List<byte> B = new List<byte>();
- foreach (ulong C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(IntPtr[]))
- {
- IntPtr[] A = (IntPtr[])Convert.ChangeType(Value, typeof(IntPtr[]));
- List<byte> B = new List<byte>();
- foreach (IntPtr C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(UIntPtr[]))
- {
- UIntPtr[] A = (UIntPtr[])Convert.ChangeType(Value, typeof(UIntPtr[]));
- List<byte> B = new List<byte>();
- foreach (UIntPtr C in A)
- {
- B.AddRange(TypeOf(C));
- }
- return B.ToArray();
- }
- else
- {
- uint A = SizeOf<T>();
- byte[] B = new byte[A];
- IntPtr C = Marshal.AllocHGlobal((int)A);
- Marshal.StructureToPtr(Value, C, true);
- Marshal.Copy(C, B, 0, (int)A);
- Marshal.FreeHGlobal(C);
- return B;
- }
- }
- public static byte[] TypeOf<T>(T Value, Encoding Encoding)
- {
- if (typeof(T) == typeof(char))
- return Encoding.GetBytes(new char[] { (char)Convert.ChangeType(Value, typeof(char)) });
- if (typeof(T) == typeof(char[]))
- {
- char[] A = (char[])Convert.ChangeType(Value, typeof(char[]));
- List<byte> B = new List<byte>();
- foreach (char C in A)
- {
- B.AddRange(TypeOf(C, Encoding));
- }
- return B.ToArray();
- }
- if (typeof(T) == typeof(string))
- return Encoding.GetBytes((string)Convert.ChangeType(Value, typeof(string)));
- else
- return TypeOf(Value);
- }
- public static uint SizeOf<T>()
- {
- if (typeof(T) == typeof(bool))
- return 1;
- if (typeof(T) == typeof(char))
- return SizeOf<char>(Encoding.Default);
- else
- return (uint)Marshal.SizeOf<T>();
- }
- public static uint SizeOf<T>(Encoding Encoding)
- {
- if (typeof(T) == typeof(char))
- return (uint)Encoding.GetMaxByteCount(0);
- else
- return SizeOf<T>();
- }
- #endregion
- #region Process & Memory
- public class Process
- {
- private PROCESSENTRY32 _Information = new PROCESSENTRY32();
- private bool _Exists = false;
- private UIntPtr _Handle = UIntPtr.Zero;
- static Process()
- {
- return;
- }
- public Process()
- {
- _Information = new PROCESSENTRY32();
- _Exists = (_Information.dwSize != 0);
- return;
- }
- internal Process(PROCESSENTRY32 Information)
- {
- _Information = Information;
- _Exists = (_Information.dwSize != 0);
- return;
- }
- public bool Exists
- {
- get { return _Exists; }
- }
- public Module[] Modules
- {
- get { return EnumerateModules(this); }
- }
- public Module MainModule
- {
- get
- {
- MODULEENTRY32 A = new MODULEENTRY32();
- A.dwSize = SizeOf<MODULEENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Id);
- if (Module32First(C, ref A))
- {
- CloseHandle(C);
- return new Module(A);
- }
- CloseHandle(C);
- return new Module();
- }
- }
- public Thread[] Threads
- {
- get { return EnumerateThreads(this); }
- }
- public Window[] Windows
- {
- get { return EnumerateWindows(this); }
- }
- public Window MainWindow
- {
- get { return GetMainWindow(this); }
- }
- public void Suspend()
- {
- foreach(Thread A in Threads)
- {
- A.Open();
- A.Suspend();
- A.Close();
- }
- return;
- }
- public void Resume()
- {
- foreach (Thread A in Threads)
- {
- A.Open();
- A.Resume();
- A.Close();
- }
- return;
- }
- public Process Parent
- {
- get { return FindProcess(_Information.th32ParentProcessID); }
- }
- public Process[] Children
- {
- get
- {
- PROCESSENTRY32 A = new PROCESSENTRY32();
- List<Process> B = new List<Process>();
- A.dwSize = SizeOf<PROCESSENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
- if (Process32First(C, ref A))
- {
- do
- {
- if (Id == A.th32ParentProcessID && Id != A.th32ProcessID)
- B.Add(new Process(A));
- } while (Process32Next(C, ref A));
- }
- CloseHandle(C);
- return B.ToArray();
- }
- }
- public uint Id
- {
- get { return _Information.th32ProcessID; }
- }
- public string Name
- {
- get { return _Information.szExeFile; }
- }
- public string FileName
- {
- get { return MainModule.FileName; }
- }
- public UIntPtr Handle
- {
- get
- {
- if (_Handle != UIntPtr.Zero)
- return _Handle;
- else
- return UIntPtr.Zero;
- }
- }
- public void Open()
- {
- Close();
- _Handle = OpenProcess(0x1FFFFF, false, Id);
- return;
- }
- public void Close()
- {
- if (_Handle != UIntPtr.Zero)
- {
- CloseHandle(Handle);
- _Handle = UIntPtr.Zero;
- }
- return;
- }
- public void Terminate()
- {
- TerminateProcess(Handle, 0xFFFFFFFF);
- return;
- }
- public void TerminateTree()
- {
- foreach (Process A in Children)
- {
- if (A.Children.Length != 0)
- {
- A.TerminateTree();
- }
- A.Terminate();
- }
- Terminate();
- return;
- }
- public byte[] ReadMemory(UIntPtr Address, uint Size)
- {
- byte[] A = new byte[Size];
- uint B;
- ReadProcessMemory(Handle, Address, A, Size, out B);
- return A;
- }
- public void WriteMemory(UIntPtr Address, byte[] Value)
- {
- uint A;
- WriteProcessMemory(Handle, Address, Value, (uint)Value.Length, out A);
- return;
- }
- public UIntPtr[] ScanMemory(UIntPtr StartAddress, byte[] Value, uint Range)
- {
- UIntPtr A = (UIntPtr)(StartAddress.ToUInt64() + Range);
- UIntPtr B = StartAddress;
- MEMORY_BASIC_INFORMATION C = new MEMORY_BASIC_INFORMATION();
- List<UIntPtr> D = new List<UIntPtr>();
- do
- {
- VirtualQueryEx(Handle, B, out C, SizeOf<MEMORY_BASIC_INFORMATION>());
- if ((C.Type == 0x20000 || C.Type == 0x1000000) && (C.State == 0x1000 || C.State == 0x2000))
- {
- byte[] E = ReadMemory(C.BaseAddress, C.RegionSize.ToUInt32());
- for (uint F = 0; F < C.RegionSize.ToUInt32() - Value.Length; F++)
- {
- bool G = true;
- for (uint H = 0; H < Value.Length; H++)
- {
- if (!(Value[H] == E[F + H]) || !(C.BaseAddress.ToUInt64() + F < A.ToUInt64()) || !(C.BaseAddress.ToUInt64() + F >= StartAddress.ToUInt64()))
- {
- G = false;
- break;
- }
- }
- if (G)
- {
- D.Add((UIntPtr)(C.BaseAddress.ToUInt64() + F));
- }
- }
- }
- B = (UIntPtr)(B.ToUInt64() + C.RegionSize.ToUInt32());
- } while (B.ToUInt64() < A.ToUInt64());
- return D.ToArray();
- }
- public UIntPtr[] ScanMemory(UIntPtr StartAddress, byte[] Value, uint Range, byte Ignore)
- {
- UIntPtr A = (UIntPtr)(StartAddress.ToUInt64() + Range);
- UIntPtr B = StartAddress;
- MEMORY_BASIC_INFORMATION C = new MEMORY_BASIC_INFORMATION();
- List<UIntPtr> D = new List<UIntPtr>();
- do
- {
- VirtualQueryEx(Handle, B, out C, SizeOf<MEMORY_BASIC_INFORMATION>());
- if ((C.Type == 0x20000 || C.Type == 0x1000000) && (C.State == 0x1000 || C.State == 0x2000))
- {
- byte[] E = ReadMemory(C.BaseAddress, C.RegionSize.ToUInt32());
- for (uint F = 0; F < C.RegionSize.ToUInt32() - Value.Length; F++)
- {
- bool G = true;
- for (uint H = 0; H < Value.Length; H++)
- {
- if ((!(Value[H] == E[F + H]) || !(C.BaseAddress.ToUInt64() + F < A.ToUInt64()) || !(C.BaseAddress.ToUInt64() + F >= StartAddress.ToUInt64())) && Ignore != Value[H])
- {
- G = false;
- break;
- }
- }
- if (G)
- {
- D.Add((UIntPtr)(C.BaseAddress.ToUInt64() + F));
- }
- }
- }
- B = (UIntPtr)(B.ToUInt64() + C.RegionSize.ToUInt32());
- } while (B.ToUInt64() < A.ToUInt64());
- return D.ToArray();
- }
- public UIntPtr SearchMemory(UIntPtr StartAddress, byte[] Value, uint Range)
- {
- UIntPtr A = (UIntPtr)(StartAddress.ToUInt64() + Range);
- UIntPtr B = StartAddress;
- MEMORY_BASIC_INFORMATION C = new MEMORY_BASIC_INFORMATION();
- do
- {
- VirtualQueryEx(Handle, B, out C, SizeOf<MEMORY_BASIC_INFORMATION>());
- if ((C.Type == 0x20000 || C.Type == 0x1000000) && (C.State == 0x1000 || C.State == 0x2000))
- {
- byte[] E = ReadMemory(C.BaseAddress, C.RegionSize.ToUInt32());
- for (uint F = 0; F < C.RegionSize.ToUInt32() - Value.Length; F++)
- {
- bool G = true;
- for (uint H = 0; H < Value.Length; H++)
- {
- if (!(Value[H] == E[F + H]) || !(C.BaseAddress.ToUInt64() + F < A.ToUInt64()) || !(C.BaseAddress.ToUInt64() + F >= StartAddress.ToUInt64()))
- {
- G = false;
- break;
- }
- }
- if (G)
- {
- return ((UIntPtr)(C.BaseAddress.ToUInt64() + F));
- }
- }
- }
- B = (UIntPtr)(B.ToUInt64() + C.RegionSize.ToUInt32());
- } while (B.ToUInt64() < A.ToUInt64());
- return UIntPtr.Zero;
- }
- public UIntPtr SearchMemory(UIntPtr StartAddress, byte[] Value, uint Range, byte Ignore)
- {
- UIntPtr A = (UIntPtr)(StartAddress.ToUInt64() + Range);
- UIntPtr B = StartAddress;
- MEMORY_BASIC_INFORMATION C = new MEMORY_BASIC_INFORMATION();
- do
- {
- VirtualQueryEx(Handle, B, out C, SizeOf<MEMORY_BASIC_INFORMATION>());
- if ((C.Type == 0x20000 || C.Type == 0x1000000) && (C.State == 0x1000 || C.State == 0x2000))
- {
- byte[] E = ReadMemory(C.BaseAddress, C.RegionSize.ToUInt32());
- for (uint F = 0; F < C.RegionSize.ToUInt32() - Value.Length; F++)
- {
- bool G = true;
- for (uint H = 0; H < Value.Length; H++)
- {
- if ((!(Value[H] == E[F + H]) || !(C.BaseAddress.ToUInt64() + F < A.ToUInt64()) || !(C.BaseAddress.ToUInt64() + F >= StartAddress.ToUInt64())) && Ignore != Value[H])
- {
- G = false;
- break;
- }
- }
- if (G)
- {
- return ((UIntPtr)(C.BaseAddress.ToUInt64() + F));
- }
- }
- }
- B = (UIntPtr)(B.ToUInt64() + C.RegionSize.ToUInt32());
- } while (B.ToUInt64() < A.ToUInt64());
- return UIntPtr.Zero;
- }
- public UIntPtr AllocateMemory(uint Size, MemoryProtectionConstants Protection)
- {
- return AllocateMemory(Size, Protection, UIntPtr.Zero);
- }
- public UIntPtr AllocateMemory(uint Size, MemoryProtectionConstants Protection, UIntPtr Address)
- {
- return VirtualAllocEx(Handle, Address, Size, 0x1000 | 0x2000, (uint)Protection);
- }
- public void FreeMemory(UIntPtr Address)
- {
- VirtualFreeEx(Handle, Address, 0, 0x8000);
- }
- public void FreeMemory(UIntPtr Address, uint Size)
- {
- VirtualFreeEx(Handle, Address, Size, 0x4000);
- }
- public void FillMemory(UIntPtr Address, byte[] Value, uint Times)
- {
- for (uint A = 0; A < Times; A++)
- {
- WriteMemory((UIntPtr)(Address.ToUInt64() + (ulong)(A * Value.Length)), Value);
- }
- }
- public MemoryProtectionConstants ProtectMemory(UIntPtr Address, uint Size, MemoryProtectionConstants Protection)
- {
- uint A;
- VirtualProtectEx(Handle, Address, Size, (uint)Protection, out A);
- return (MemoryProtectionConstants)A;
- }
- }
- public static Process[] EnumerateProcesses()
- {
- PROCESSENTRY32 A = new PROCESSENTRY32();
- List<Process> B = new List<Process>();
- A.dwSize = SizeOf<PROCESSENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
- if (Process32First(C, ref A))
- {
- do
- {
- B.Add(new Process(A));
- } while (Process32Next(C, ref A));
- }
- CloseHandle(C);
- return B.ToArray();
- }
- public static Process[] FindProcesses(string Name)
- {
- PROCESSENTRY32 A = new PROCESSENTRY32();
- List<Process> B = new List<Process>();
- A.dwSize = SizeOf<PROCESSENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
- if (Process32First(C, ref A))
- {
- do
- {
- if (Name.ToLower() == A.szExeFile.ToLower())
- B.Add(new Process(A));
- } while (Process32Next(C, ref A));
- }
- CloseHandle(C);
- return B.ToArray();
- }
- public static Process FindProcess(string Name)
- {
- PROCESSENTRY32 A = new PROCESSENTRY32();
- A.dwSize = SizeOf<PROCESSENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
- if (Process32First(C, ref A))
- {
- do
- {
- if (Name.ToLower() == A.szExeFile.ToLower())
- {
- CloseHandle(C);
- return new Process(A);
- }
- } while (Process32Next(C, ref A));
- }
- CloseHandle(C);
- return new Process();
- }
- public static Process FindProcess(uint Id)
- {
- PROCESSENTRY32 A = new PROCESSENTRY32();
- A.dwSize = SizeOf<PROCESSENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
- if (Process32First(C, ref A))
- {
- do
- {
- if (Id == A.th32ProcessID)
- {
- CloseHandle(C);
- return new Process(A);
- }
- } while (Process32Next(C, ref A));
- }
- CloseHandle(C);
- return new Process();
- }
- public static Process GetCurrentProcess()
- {
- return FindProcess(GetCurrentProcessId());
- }
- public static Process CreateProcess(string FileName)
- {
- return CreateProcess(FileName, null, null);
- }
- public static Process CreateProcess(string FileName, string CommandLine)
- {
- return CreateProcess(FileName, CommandLine, null);
- }
- public static Process CreateProcess(string FileName, string CommandLine, string CurrentDirectory)
- {
- STARTUPINFO A = new STARTUPINFO();
- PROCESS_INFORMATION B = new PROCESS_INFORMATION();
- if (CreateProcess(FileName, CommandLine, UIntPtr.Zero, UIntPtr.Zero, false, 0, UIntPtr.Zero, CurrentDirectory, ref A, out B))
- {
- CloseHandle(B.hProcess);
- CloseHandle(B.hThread);
- return FindProcess(B.dwProcessId);
- }
- return new Process();
- }
- #endregion
- #region Module
- public class Module
- {
- private MODULEENTRY32 _Information = new MODULEENTRY32();
- private bool _Exists = false;
- static Module()
- {
- return;
- }
- public Module()
- {
- _Information = new MODULEENTRY32();
- _Exists = (_Information.dwSize != 0);
- return;
- }
- internal Module(MODULEENTRY32 Information)
- {
- _Information = Information;
- _Exists = (_Information.dwSize != 0);
- return;
- }
- public bool Exists
- {
- get { return _Exists; }
- }
- public Symbol[] Symbols
- {
- get { return EnumerateSymbols(this); }
- }
- public Process Process
- {
- get { return FindProcess(_Information.th32ProcessID); }
- }
- public UIntPtr BaseAddress
- {
- get { return _Information.modBaseAddr; }
- }
- public uint Size
- {
- get { return _Information.modBaseSize; }
- }
- public string Name
- {
- get { return _Information.szModule; }
- }
- public string FileName
- {
- get { return _Information.szExePath; }
- }
- public UIntPtr EntryPoint
- {
- get
- {
- MODULEINFO A = new MODULEINFO();
- UIntPtr B = Process.Handle;
- K32GetModuleInformation(B, _Information.hModule, out A, SizeOf<MODULEINFO>());
- return A.EntryPoint;
- }
- }
- }
- public static Module[] EnumerateModules(Process Process)
- {
- MODULEENTRY32 A = new MODULEENTRY32();
- List<Module> B = new List<Module>();
- A.dwSize = SizeOf<MODULEENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Process.Id);
- if (Module32First(C, ref A))
- {
- do
- {
- B.Add(new Module(A));
- } while (Module32Next(C, ref A));
- }
- CloseHandle(C);
- return B.ToArray();
- }
- public static Module[] FindModules(Process Process, string Name)
- {
- MODULEENTRY32 A = new MODULEENTRY32();
- List<Module> B = new List<Module>();
- A.dwSize = SizeOf<MODULEENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Process.Id);
- if (Module32First(C, ref A))
- {
- do
- {
- if (Name.ToLower() == A.szModule.ToLower())
- B.Add(new Module(A));
- } while (Module32Next(C, ref A));
- }
- CloseHandle(C);
- return B.ToArray();
- }
- public static Module FindModule(Process Process, UIntPtr BaseAddress)
- {
- MODULEENTRY32 A = new MODULEENTRY32();
- A.dwSize = SizeOf<MODULEENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Process.Id);
- if (Module32First(C, ref A))
- {
- do
- {
- if (BaseAddress == A.hModule)
- {
- CloseHandle(C);
- return new Module(A);
- }
- } while (Module32Next(C, ref A));
- }
- CloseHandle(C);
- return new Module();
- }
- public static Module FindModule(Process Process, string Name)
- {
- MODULEENTRY32 A = new MODULEENTRY32();
- A.dwSize = SizeOf<MODULEENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Process.Id);
- if (Module32First(C, ref A))
- {
- do
- {
- if (Name.ToLower() == A.szModule.ToLower())
- {
- CloseHandle(C);
- return new Module(A);
- }
- } while (Module32Next(C, ref A));
- }
- CloseHandle(C);
- return new Module();
- }
- #endregion
- #region Symbol
- public class Symbol
- {
- private SYMBOL_INFO _Information = new SYMBOL_INFO();
- private Module _Module = new Module();
- private bool _Exists = false;
- static Symbol()
- {
- return;
- }
- public Symbol()
- {
- _Information = new SYMBOL_INFO();
- _Module = new Module();
- _Exists = (_Information.SizeOfStruct != 0);
- return;
- }
- internal Symbol(SYMBOL_INFO Information, Module Module)
- {
- _Information = Information;
- _Module = Module;
- _Exists = (_Information.SizeOfStruct != 0);
- return;
- }
- public bool Exists
- {
- get { return _Exists; }
- }
- public string Name
- {
- get { return _Information.Name; }
- }
- public ulong Value
- {
- get { return _Information.Value; }
- }
- public uint Size
- {
- get { return _Information.Size; }
- }
- public Module Module
- {
- get { return _Module; }
- }
- public UIntPtr Address
- {
- get { return new UIntPtr(_Information.Address - _Information.ModBase + Module.BaseAddress.ToUInt64()); }
- }
- public uint Index
- {
- get { return _Information.Index; }
- }
- }
- private static List<Symbol> EnumeratedSymbols = new List<Symbol>();
- private static Symbol ReturnSymbol = new Symbol();
- private static Module ModuleForSymbols = new Module();
- private static bool SymEnumSymbolsProc1(ref SYMBOL_INFO pSymInfo, uint SymbolSize, UIntPtr UserContext)
- {
- EnumeratedSymbols.Add(new Symbol(pSymInfo, ModuleForSymbols));
- return true;
- }
- private static bool SymEnumSymbolsProc2(ref SYMBOL_INFO pSymInfo, uint SymbolSize, UIntPtr UserContext)
- {
- ReturnSymbol = (new Symbol(pSymInfo, ModuleForSymbols));
- return false;
- }
- public static Symbol[] EnumerateSymbols(Module Module)
- {
- EnumeratedSymbols.Clear();
- ModuleForSymbols = Module;
- UIntPtr A = Module.Process.Handle;
- SymInitialize(A, null, false);
- ulong B = SymLoadModuleEx(A, UIntPtr.Zero, Module.FileName, null, 0, 0, UIntPtr.Zero, 0);
- SymEnumSymbols(A, B, "*", new SymEnumSymbolsProc(SymEnumSymbolsProc1), UIntPtr.Zero);
- SymCleanup(A);
- return EnumeratedSymbols.ToArray();
- }
- public static Symbol[] FindSymbols(Module Module, string Name)
- {
- EnumeratedSymbols.Clear();
- ModuleForSymbols = Module;
- UIntPtr A = Module.Process.Handle;
- SymInitialize(A, null, false);
- ulong B = SymLoadModuleEx(A, UIntPtr.Zero, Module.FileName, null, 0, 0, UIntPtr.Zero, 0);
- SymEnumSymbols(A, B, Name, new SymEnumSymbolsProc(SymEnumSymbolsProc1), UIntPtr.Zero);
- SymCleanup(A);
- return EnumeratedSymbols.ToArray();
- }
- public static Symbol FindSymbol(Module Module, string Name)
- {
- ModuleForSymbols = Module;
- UIntPtr A = Module.Process.Handle;
- SymInitialize(A, null, false);
- ulong B = SymLoadModuleEx(A, UIntPtr.Zero, Module.FileName, null, 0, 0, UIntPtr.Zero, 0);
- SymEnumSymbols(A, B, Name, new SymEnumSymbolsProc(SymEnumSymbolsProc2), UIntPtr.Zero);
- SymCleanup(A);
- return ReturnSymbol;
- }
- #endregion
- #region Thread
- public class Thread
- {
- private THREADENTRY32 _Information = new THREADENTRY32();
- private bool _Exists = false;
- private UIntPtr _Handle = UIntPtr.Zero;
- static Thread()
- {
- return;
- }
- public Thread()
- {
- _Information = new THREADENTRY32();
- _Exists = (_Information.dwSize != 0);
- return;
- }
- internal Thread(THREADENTRY32 Information)
- {
- _Information = Information;
- _Exists = (_Information.dwSize != 0);
- return;
- }
- public bool Exists
- {
- get
- { return _Exists; }
- }
- public Process Process
- {
- get { return FindProcess(_Information.th32OwnerProcessID); }
- }
- public Window[] Windows
- {
- get { return EnumerateWindows(this); }
- }
- public Window MainWindow
- {
- get { return GetMainWindow(this); }
- }
- public uint Id
- {
- get { return _Information.th32ThreadID; }
- }
- public uint BasePriority
- {
- get { return _Information.tpBasePri; }
- }
- public UIntPtr Handle
- {
- get
- {
- if (_Handle != UIntPtr.Zero)
- return _Handle;
- else
- return UIntPtr.Zero;
- }
- }
- public void Open()
- {
- Close();
- _Handle = OpenThread(0x1FFFFF, false, Id);
- return;
- }
- public void Close()
- {
- if (_Handle != UIntPtr.Zero)
- {
- CloseHandle(Handle);
- _Handle = UIntPtr.Zero;
- }
- return;
- }
- public UIntPtr StartAddress
- {
- get
- {
- IntPtr A = Marshal.AllocHGlobal(IntPtr.Size);
- try
- {
- NtQueryInformationThread(Handle, 9, A, (ulong)IntPtr.Size, (ulong)IntPtr.Zero);
- return unchecked((UIntPtr)(ulong)(long)Marshal.ReadIntPtr(A));
- }
- finally
- {
- Marshal.FreeHGlobal(A);
- }
- }
- }
- public void Suspend()
- {
- SuspendThread(Handle);
- return;
- }
- public void Resume()
- {
- ResumeThread(Handle);
- return;
- }
- public void Terminate()
- {
- TerminateThread(Handle, 0xffffffff);
- return;
- }
- }
- public static Thread[] EnumerateThreads()
- {
- THREADENTRY32 A = new THREADENTRY32();
- List<Thread> B = new List<Thread>();
- A.dwSize = SizeOf<THREADENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x4, 0);
- if (Thread32First(C, ref A))
- {
- do
- {
- B.Add(new Thread(A));
- } while (Thread32Next(C, ref A));
- }
- CloseHandle(C);
- return B.ToArray();
- }
- public static Thread[] EnumerateThreads(Process Process)
- {
- THREADENTRY32 A = new THREADENTRY32();
- List<Thread> B = new List<Thread>();
- A.dwSize = SizeOf<THREADENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x4, 0);
- if (Thread32First(C, ref A))
- {
- do
- {
- if (Process.Id == A.th32OwnerProcessID)
- B.Add(new Thread(A));
- } while (Thread32Next(C, ref A));
- }
- CloseHandle(C);
- return B.ToArray();
- }
- public static Thread[] FindThreads(UIntPtr StartAddress)
- {
- THREADENTRY32 A = new THREADENTRY32();
- List<Thread> B = new List<Thread>();
- A.dwSize = SizeOf<THREADENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x4, 0);
- if (Thread32First(C, ref A))
- {
- do
- {
- Thread D = new Thread(A);
- D.Open();
- bool E = (StartAddress == D.StartAddress);
- D.Close();
- if (E)
- {
- B.Add(D);
- }
- } while (Thread32Next(C, ref A));
- }
- CloseHandle(C);
- return B.ToArray();
- }
- public static Thread[] FindThreads(Process Process, UIntPtr StartAddress)
- {
- THREADENTRY32 A = new THREADENTRY32();
- List<Thread> B = new List<Thread>();
- A.dwSize = SizeOf<THREADENTRY32>();
- UIntPtr C = CreateToolhelp32Snapshot(0x4, 0);
- if (Thread32First(C, ref A))
- {
- do
- {
- Thread D = new Thread(A);
- D.Open();
- bool E = (StartAddress == D.StartAddress);
- D.Close();
- if (E && Process.Id == A.th32OwnerProcessID)
- {
- B.Add(D);
- }
- } while (Thread32Next(C, ref A));
- }
- CloseHandle(C);
- return B.ToArray();
- }
- public static Thread FindThread(uint Id)
- {
- THREADENTRY32 A = new THREADENTRY32();
- A.dwSize = SizeOf<THREADENTRY32>();
- UIntPtr B = CreateToolhelp32Snapshot(0x4, 0);
- if (Thread32First(B, ref A))
- {
- do
- {
- if (Id == A.th32ThreadID)
- {
- CloseHandle(B);
- return new Thread(A);
- }
- } while (Thread32Next(B, ref A));
- }
- CloseHandle(B);
- return new Thread();
- }
- public static Thread FindThread(UIntPtr StartAddress)
- {
- THREADENTRY32 A = new THREADENTRY32();
- A.dwSize = SizeOf<THREADENTRY32>();
- UIntPtr B = CreateToolhelp32Snapshot(0x4, 0);
- if (Thread32First(B, ref A))
- {
- do
- {
- Thread C = new Thread(A);
- C.Open();
- bool D = (StartAddress == C.StartAddress);
- C.Close();
- if (D)
- {
- CloseHandle(B);
- return C;
- }
- } while (Thread32Next(B, ref A));
- }
- CloseHandle(B);
- return new Thread();
- }
- public static Thread FindThread(Process Process, UIntPtr StartAddress)
- {
- THREADENTRY32 A = new THREADENTRY32();
- A.dwSize = SizeOf<THREADENTRY32>();
- UIntPtr B = CreateToolhelp32Snapshot(0x4, 0);
- if (Thread32First(B, ref A))
- {
- do
- {
- Thread C = new Thread(A);
- C.Open();
- bool D = (StartAddress == C.StartAddress);
- C.Close();
- if (D && Process.Id == A.th32OwnerProcessID)
- {
- CloseHandle(B);
- return C;
- }
- } while (Thread32Next(B, ref A));
- }
- CloseHandle(B);
- return new Thread();
- }
- public static Thread GetCurrentThread()
- {
- return FindThread(GetCurrentThreadId());
- }
- public static Thread CreateThread(Process Process, UIntPtr StartAddress)
- {
- uint A;
- CreateRemoteThread(Process.Handle, UIntPtr.Zero, 0, StartAddress, UIntPtr.Zero, 0, out A);
- return FindThread(A);
- }
- public static Thread CreateThread(Process Process, UIntPtr StartAddress, byte[] Parameter)
- {
- uint A;
- UIntPtr C = Process.AllocateMemory((uint)Parameter.Length, MemoryProtectionConstants.PAGE_EXECUTE_READWRITE);
- Process.WriteMemory(C, Parameter);
- CreateRemoteThread(Process.Handle, UIntPtr.Zero, 0, StartAddress, C, 0, out A);
- Process.FreeMemory(C);
- return FindThread(A);
- }
- #endregion
- #region Window
- public class Window
- {
- UIntPtr _Handle = UIntPtr.Zero;
- private bool _Exists = false;
- static Window()
- {
- return;
- }
- public Window()
- {
- _Handle = UIntPtr.Zero;
- _Exists = (_Handle != UIntPtr.Zero);
- return;
- }
- internal Window(UIntPtr Handle)
- {
- _Handle = Handle;
- _Exists = (_Handle != UIntPtr.Zero);
- return;
- }
- public bool Exists
- {
- get { return _Exists; }
- }
- public UIntPtr Handle
- {
- get { return _Handle; }
- }
- public string Text
- {
- get
- {
- StringBuilder A = new StringBuilder(GetWindowTextLength(Handle));
- GetWindowText(Handle, A, A.Capacity + 1);
- return A.ToString();
- }
- set
- {
- SetWindowText(Handle, value);
- return;
- }
- }
- public Process Process
- {
- get
- {
- uint A;
- GetWindowThreadProcessId(Handle, out A);
- return FindProcess(A);
- }
- }
- public Thread Thread
- {
- get
- {
- uint A;
- return FindThread(GetWindowThreadProcessId(Handle, out A));
- }
- }
- public int Left
- {
- get
- {
- RECT A = new RECT();
- GetWindowRect(Handle, out A);
- return A.left;
- }
- set
- {
- SetWindowPos(Handle, UIntPtr.Zero, value, Top, 0, 0, 0x4 | 0x1);
- return;
- }
- }
- public int ClientLeft
- {
- get
- {
- return Width - ClientWidth + Left;
- }
- }
- public int Top
- {
- get
- {
- RECT A = new RECT();
- GetWindowRect(Handle, out A);
- return A.top;
- }
- set
- {
- SetWindowPos(Handle, UIntPtr.Zero, Left, value, 0, 0, 0x4 | 0x1);
- return;
- }
- }
- public int ClientTop
- {
- get
- {
- return Height - ClientHeight + Top;
- }
- }
- public int Right
- {
- get
- {
- RECT A = new RECT();
- GetWindowRect(Handle, out A);
- return A.right;
- }
- set
- {
- Width = value - Left;
- return;
- }
- }
- public int ClientRight
- {
- get
- {
- return ClientLeft + ClientWidth;
- }
- }
- public int Bottom
- {
- get
- {
- RECT A = new RECT();
- GetWindowRect(Handle, out A);
- return A.bottom;
- }
- set
- {
- Height = value - Top;
- return;
- }
- }
- public int ClientBottom
- {
- get
- {
- return ClientTop + ClientHeight;
- }
- }
- public int Width
- {
- get
- {
- return Right - Left;
- }
- set
- {
- SetWindowPos(Handle, UIntPtr.Zero, 0, 0, value, Bottom - Top, 0x4 | 0x2);
- return;
- }
- }
- public int ClientWidth
- {
- get
- {
- RECT A = new RECT();
- GetClientRect(Handle, out A);
- return A.right;
- }
- /*
- set
- {
- SetWindowPos(Handle, UIntPtr.Zero, 0, 0, Right - Left, Bottom - Top, 0x4 | 0x2);
- return;
- }
- */
- }
- public int Height
- {
- get
- {
- return Bottom - Top;
- }
- set
- {
- SetWindowPos(Handle, UIntPtr.Zero, 0, 0, Width, value, 0x4 | 0x2);
- return;
- }
- }
- public int ClientHeight
- {
- get
- {
- RECT A = new RECT();
- GetClientRect(Handle, out A);
- return A.bottom;
- }
- /*
- set
- {
- SetWindowPos(Handle, UIntPtr.Zero, 0, 0, Right - Left, Bottom - Top, 0x4 | 0x2);
- return;
- }
- */
- }
- public void Terminate()
- {
- DestroyWindow(Handle);
- return;
- }
- public void Show()
- {
- ShowWindow(Handle, 5);
- return;
- }
- public void Hide()
- {
- ShowWindow(Handle, 0);
- return;
- }
- public void Minimize()
- {
- ShowWindow(Handle, 6);
- return;
- }
- public void Maximize()
- {
- ShowWindow(Handle, 3);
- return;
- }
- }
- private static List<Window> EnumeratedWindows = new List<Window>();
- private static UIntPtr WindowFromHandle = UIntPtr.Zero;
- private static string WindowFromText = string.Empty;
- private static Process WindowFromProcess = new Process();
- private static Thread WindowFromThread = new Thread();
- private static Window WindowToReturn = new Window();
- private static bool EnumWindowsProc1(UIntPtr hwnd, UIntPtr lParam)
- {
- EnumeratedWindows.Add(new Window(hwnd));
- return true;
- }
- private static bool EnumWindowsProc2(UIntPtr hwnd, UIntPtr lParam)
- {
- if(WindowFromHandle == hwnd)
- {
- WindowToReturn = new Window(hwnd);
- return false;
- }
- return true;
- }
- private static bool EnumWindowsProc3(UIntPtr hwnd, UIntPtr lParam)
- {
- Window A = new Window(hwnd);
- if (WindowFromText == A.Text)
- {
- WindowToReturn = A;
- return false;
- }
- return true;
- }
- private static bool EnumWindowsProc4(UIntPtr hwnd, UIntPtr lParam)
- {
- Window A = new Window(hwnd);
- if (WindowFromProcess.Id == A.Process.Id && WindowFromText == A.Text)
- {
- WindowToReturn = A;
- return false;
- }
- return true;
- }
- private static bool EnumWindowsProc5(UIntPtr hwnd, UIntPtr lParam)
- {
- Window A = new Window(hwnd);
- if (WindowFromThread.Id == A.Thread.Id && WindowFromText == A.Text)
- {
- WindowToReturn = A;
- return false;
- }
- return true;
- }
- private static bool EnumWindowsProc6(UIntPtr hwnd, UIntPtr lParam)
- {
- Window A = new Window(hwnd);
- if (WindowFromProcess.Id == A.Process.Id)
- {
- EnumeratedWindows.Add(new Window(hwnd));
- return true;
- }
- return true;
- }
- private static bool EnumWindowsProc7(UIntPtr hwnd, UIntPtr lParam)
- {
- Window A = new Window(hwnd);
- if (WindowFromThread.Id == A.Thread.Id)
- {
- EnumeratedWindows.Add(new Window(hwnd));
- return true;
- }
- return true;
- }
- private static bool EnumWindowsProc8(UIntPtr hwnd, UIntPtr lParam)
- {
- Window A = new Window(hwnd);
- if (WindowFromText == A.Text)
- {
- EnumeratedWindows.Add(new Window(hwnd));
- return true;
- }
- return true;
- }
- private static bool EnumWindowsProc9(UIntPtr hwnd, UIntPtr lParam)
- {
- Window A = new Window(hwnd);
- if (WindowFromProcess.Id == A.Process.Id && WindowFromText == A.Text)
- {
- EnumeratedWindows.Add(new Window(hwnd));
- return true;
- }
- return true;
- }
- private static bool EnumWindowsProc10(UIntPtr hwnd, UIntPtr lParam)
- {
- Window A = new Window(hwnd);
- if (WindowFromThread.Id == A.Thread.Id && WindowFromText == A.Text)
- {
- EnumeratedWindows.Add(new Window(hwnd));
- return true;
- }
- return true;
- }
- public static Window[] EnumerateWindows()
- {
- EnumeratedWindows.Clear();
- EnumWindows(new EnumWindowsProc(EnumWindowsProc1), UIntPtr.Zero);
- return EnumeratedWindows.ToArray();
- }
- public static Window[] EnumerateWindows(Process Process)
- {
- EnumeratedWindows.Clear();
- WindowFromProcess = Process;
- EnumWindows(new EnumWindowsProc(EnumWindowsProc6), UIntPtr.Zero);
- return EnumeratedWindows.ToArray();
- }
- public static Window[] EnumerateWindows(Thread Thread)
- {
- EnumeratedWindows.Clear();
- WindowFromThread = Thread;
- EnumWindows(new EnumWindowsProc(EnumWindowsProc7), UIntPtr.Zero);
- return EnumeratedWindows.ToArray();
- }
- public static Window FindWindow(UIntPtr Handle)
- {
- WindowToReturn = new Window();
- WindowFromHandle = Handle;
- EnumWindows(new EnumWindowsProc(EnumWindowsProc2), UIntPtr.Zero);
- return WindowToReturn;
- }
- public static Window FindWindow(string Text)
- {
- WindowToReturn = new Window();
- WindowFromText = Text;
- EnumWindows(new EnumWindowsProc(EnumWindowsProc3), UIntPtr.Zero);
- return WindowToReturn;
- }
- public static Window[] FindWindows(string Text)
- {
- EnumeratedWindows.Clear();
- WindowFromText = Text;
- EnumWindows(new EnumWindowsProc(EnumWindowsProc8), UIntPtr.Zero);
- return EnumeratedWindows.ToArray();
- }
- public static Window[] FindWindows(Process Process, string Text)
- {
- EnumeratedWindows.Clear();
- WindowFromProcess = Process;
- WindowFromText = Text;
- EnumWindows(new EnumWindowsProc(EnumWindowsProc9), UIntPtr.Zero);
- return EnumeratedWindows.ToArray();
- }
- public static Window[] FindWindows(Thread Thread, string Text)
- {
- EnumeratedWindows.Clear();
- WindowFromThread = Thread;
- WindowFromText = Text;
- EnumWindows(new EnumWindowsProc(EnumWindowsProc9), UIntPtr.Zero);
- return EnumeratedWindows.ToArray();
- }
- public static Window FindWindow(Process Process, string Text)
- {
- WindowToReturn = new Window();
- WindowFromProcess = Process;
- WindowFromText = Text;
- EnumWindows(new EnumWindowsProc(EnumWindowsProc4), UIntPtr.Zero);
- return WindowToReturn;
- }
- public static Window FindWindow(Thread Thread, string Text)
- {
- WindowToReturn = new Window();
- WindowFromThread = Thread;
- WindowFromText = Text;
- EnumWindows(new EnumWindowsProc(EnumWindowsProc5), UIntPtr.Zero);
- return WindowToReturn;
- }
- public static Window GetMainWindow(Process Process)
- {
- foreach(Window A in Process.Windows)
- {
- if (GetWindow(A.Handle, 4) == (UIntPtr)0 && IsWindowVisible(A.Handle))
- return A;
- }
- return new Window();
- }
- public static Window GetMainWindow(Thread Thread)
- {
- foreach (Window A in Thread.Windows)
- {
- if (GetWindow(A.Handle, 4) == (UIntPtr)0 && IsWindowVisible(A.Handle))
- return A;
- }
- return new Window();
- }
- public static Window GetCurrentWindow()
- {
- return new Window(GetForegroundWindow());
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement