Advertisement
Guest User

CHX Framework

a guest
Jun 18th, 2017
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 76.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5.  
  6. namespace CHX_Framework
  7. {
  8.  
  9.     public static class ClockAPI
  10.     {
  11.  
  12.         #region WINAPI
  13.  
  14.         public enum MemoryProtectionConstants : uint
  15.         {
  16.             PAGE_EXECUTE = 0x10,
  17.             PAGE_EXECUTE_READ = 0x20,
  18.             PAGE_EXECUTE_READWRITE = 0x40,
  19.             PAGE_EXECUTE_WRITECOPY = 0x80,
  20.             PAGE_NOACCESS = 0x01,
  21.             PAGE_READONLY = 0x02,
  22.             PAGE_READWRITE = 0x04,
  23.             PAGE_WRITECOPY = 0x08,
  24.             PAGE_TARGETS_INVALID = 0x40000000,
  25.             PAGE_TARGETS_NO_UPDATE = 0x40000000,
  26.             PAGE_GUARD = 0x100,
  27.             PAGE_NOCACHE = 0x200,
  28.             PAGE_WRITECOMBINE = 0x400
  29.         }
  30.  
  31.         internal struct PROCESSENTRY32
  32.         {
  33.             public uint dwSize;
  34.             public uint cntUsage;
  35.             public uint th32ProcessID;
  36.             public UIntPtr th32DefaultHeapID;
  37.             public uint th32ModuleID;
  38.             public uint cntThreads;
  39.             public uint th32ParentProcessID;
  40.             public uint pcPriClassBase;
  41.             public uint dwFlags;
  42.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
  43.             public string szExeFile;
  44.         }
  45.  
  46.         private struct RECT
  47.         {
  48.             public int left;
  49.             public int top;
  50.             public int right;
  51.             public int bottom;
  52.         }
  53.  
  54.         private struct STARTUPINFO
  55.         {
  56.             public uint cb;
  57.             public string lpReserved;
  58.             public string lpDesktop;
  59.             public string lpTitle;
  60.             public uint dwX;
  61.             public uint dwY;
  62.             public uint dwXSize;
  63.             public uint dwYSize;
  64.             public uint dwXCountChars;
  65.             public uint dwYCountChars;
  66.             public uint dwFillAttribute;
  67.             public uint dwFlags;
  68.             public ushort wShowWindow;
  69.             public ushort cbReserved2;
  70.             public UIntPtr lpReserved2;
  71.             public UIntPtr hStdInput;
  72.             public UIntPtr hStdOutput;
  73.             public UIntPtr hStdError;
  74.         }
  75.  
  76.         private struct PROCESS_INFORMATION
  77.         {
  78.             public UIntPtr hProcess;
  79.             public UIntPtr hThread;
  80.             public uint dwProcessId;
  81.             public uint dwThreadId;
  82.         }
  83.  
  84.         private struct MEMORY_BASIC_INFORMATION
  85.         {
  86.             public UIntPtr BaseAddress;
  87.             public UIntPtr AllocationBase;
  88.             public uint AllocationProtect;
  89.             public UIntPtr RegionSize;
  90.             public uint State;
  91.             public uint Protect;
  92.             public uint Type;
  93.         }
  94.  
  95.         internal struct MODULEENTRY32
  96.         {
  97.             public uint dwSize;
  98.             public uint th32ModuleID;
  99.             public uint th32ProcessID;
  100.             public uint GlblcntUsage;
  101.             public uint ProccntUsage;
  102.             public UIntPtr modBaseAddr;
  103.             public uint modBaseSize;
  104.             public UIntPtr hModule;
  105.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  106.             public string szModule;
  107.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
  108.             public string szExePath;
  109.         }
  110.  
  111.         private struct MODULEINFO
  112.         {
  113.             public UIntPtr lpBaseOfDll;
  114.             public uint SizeOfImage;
  115.             public UIntPtr EntryPoint;
  116.         }
  117.  
  118.         internal struct SYMBOL_INFO
  119.         {
  120.             public uint SizeOfStruct;
  121.             public uint TypeIndex;
  122.             public ulong Reserved1;
  123.             public ulong Reserved2;
  124.             public uint Index;
  125.             public uint Size;
  126.             public ulong ModBase;
  127.             public uint Flags;
  128.             public ulong Value;
  129.             public ulong Address;
  130.             public uint Register;
  131.             public uint Scope;
  132.             public uint Tag;
  133.             public uint NameLen;
  134.             public uint MaxNameLen;
  135.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
  136.             public string Name;
  137.         };
  138.  
  139.         internal struct THREADENTRY32
  140.         {
  141.             public uint dwSize;
  142.             public uint cntUsage;
  143.             public uint th32ThreadID;
  144.             public uint th32OwnerProcessID;
  145.             public uint tpBasePri;
  146.             public uint tpDeltaPri;
  147.             public uint dwFlags;
  148.         }
  149.  
  150.         private delegate bool EnumWindowsProc(UIntPtr hwnd, UIntPtr lParam);
  151.  
  152.         private delegate bool SymEnumSymbolsProc(ref SYMBOL_INFO pSymInfo, uint SymbolSize, UIntPtr UserContext);
  153.  
  154.         [DllImport("kernel32.dll")]
  155.         private static extern bool CloseHandle(UIntPtr hObject);
  156.  
  157.         [DllImport("kernel32.dll")]
  158.         private static extern UIntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
  159.  
  160.         [DllImport("kernel32.dll")]
  161.         private static extern bool Process32First(UIntPtr hSnapshot, ref PROCESSENTRY32 lppe);
  162.  
  163.         [DllImport("kernel32.dll")]
  164.         private static extern bool Process32Next(UIntPtr hSnapshot, ref PROCESSENTRY32 lppe);
  165.  
  166.         [DllImport("kernel32.dll")]
  167.         private static extern UIntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
  168.  
  169.         [DllImport("kernel32.dll")]
  170.         private static extern bool TerminateProcess(UIntPtr hProcess, uint uExitCode);
  171.  
  172.         [DllImport("kernel32.dll")]
  173.         private static extern uint GetCurrentProcessId();
  174.  
  175.         [DllImport("kernel32.dll")]
  176.         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);
  177.  
  178.         [DllImport("kernel32.dll")]
  179.         private static extern bool VirtualProtectEx(UIntPtr hProcess, UIntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);
  180.  
  181.         [DllImport("kernel32.dll")]
  182.         private static extern bool ReadProcessMemory(UIntPtr hProcess, UIntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out uint lpNumberOfBytesRead);
  183.  
  184.         [DllImport("kernel32.dll")]
  185.         private static extern bool WriteProcessMemory(UIntPtr hProcess, UIntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out uint lpNumberOfBytesWritten);
  186.  
  187.         [DllImport("kernel32.dll")]
  188.         private static extern uint VirtualQueryEx(UIntPtr hProcess, UIntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
  189.  
  190.         [DllImport("kernel32.dll")]
  191.         private static extern UIntPtr VirtualAllocEx(UIntPtr hProcess, UIntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
  192.  
  193.         [DllImport("kernel32.dll")]
  194.         private static extern UIntPtr VirtualFreeEx(UIntPtr hProcess, UIntPtr lpAddress, uint dwSize, uint dwFreeType);
  195.  
  196.         [DllImport("kernel32.dll")]
  197.         private static extern bool Module32First(UIntPtr hSnapshot, ref MODULEENTRY32 lpme);
  198.  
  199.         [DllImport("kernel32.dll")]
  200.         private static extern bool Module32Next(UIntPtr hSnapshot, ref MODULEENTRY32 lpme);
  201.  
  202.         [DllImport("kernel32.dll")]
  203.         private static extern bool K32GetModuleInformation(UIntPtr hProcess, UIntPtr hModule, out MODULEINFO lpmodinfo, uint cb);
  204.  
  205.         [DllImport("user32.dll")]
  206.         private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, UIntPtr lParam);
  207.  
  208.         [DllImport("user32.dll")]
  209.         private static extern int GetWindowText(UIntPtr hWnd, StringBuilder lpString, int nMaxCount);
  210.  
  211.         [DllImport("user32.dll")]
  212.         private static extern bool SetWindowText(UIntPtr hWnd, string lpString);
  213.  
  214.         [DllImport("user32.dll")]
  215.         private static extern int GetWindowTextLength(UIntPtr hWnd);
  216.  
  217.         [DllImport("user32.dll")]
  218.         private static extern bool SetWindowPos(UIntPtr hWnd, UIntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
  219.  
  220.         [DllImport("user32.dll")]
  221.         private static extern uint GetWindowThreadProcessId(UIntPtr hWnd, out uint lpdwProcessId);
  222.  
  223.         [DllImport("user32.dll")]
  224.         private static extern UIntPtr GetWindow(UIntPtr hWnd, uint uCmd);
  225.  
  226.         [DllImport("user32.dll")]
  227.         private static extern UIntPtr ShowWindow(UIntPtr hWnd, int nCmdShow);
  228.  
  229.         [DllImport("user32.dll")]
  230.         private static extern bool GetWindowRect(UIntPtr hWnd, out RECT lpRect);
  231.  
  232.         [DllImport("user32.dll")]
  233.         private static extern bool GetClientRect(UIntPtr hWnd, out RECT lpRect);
  234.  
  235.         [DllImport("user32.dll")]
  236.         private static extern bool IsWindowVisible(UIntPtr hWnd);
  237.  
  238.         [DllImport("user32.dll")]
  239.         private static extern bool DestroyWindow(UIntPtr hWnd);
  240.  
  241.         [DllImport("user32.dll")]
  242.         private static extern UIntPtr GetForegroundWindow();
  243.  
  244.         [DllImport("dbghelp.dll")]
  245.         private static extern bool SymInitialize(UIntPtr hProcess, string UserSearchPath, bool fInvadeProcess);
  246.  
  247.         [DllImport("dbghelp.dll")]
  248.         private static extern ulong SymLoadModuleEx(UIntPtr hProcess, UIntPtr hFile, string ImageName, string ModuleName, ulong BaseOfDll, uint DllSize, UIntPtr Data, uint Flags);
  249.  
  250.         [DllImport("dbghelp.dll")]
  251.         private static extern bool SymEnumSymbols(UIntPtr hProcess, ulong BaseOfDll, string Mask, SymEnumSymbolsProc EnumSymbolsCallback, UIntPtr UserContext);
  252.  
  253.         [DllImport("dbghelp.dll")]
  254.         private static extern bool SymCleanup(UIntPtr hProcess);
  255.  
  256.         [DllImport("kernel32.dll")]
  257.         private static extern bool Thread32First(UIntPtr hSnapshot, ref THREADENTRY32 lpte);
  258.  
  259.         [DllImport("kernel32.dll")]
  260.         private static extern bool Thread32Next(UIntPtr hSnapshot, ref THREADENTRY32 lpte);
  261.  
  262.         [DllImport("kernel32.dll")]
  263.         private static extern UIntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
  264.  
  265.         [DllImport("ntdll.dll")]
  266.         private static extern uint NtQueryInformationThread(UIntPtr ThreadHandle, uint ThreadInformationClass, IntPtr ThreadInformation, ulong ThreadInformationLength, ulong ReturnLength);
  267.  
  268.         [DllImport("kernel32.dll")]
  269.         private static extern uint SuspendThread(UIntPtr hThread);
  270.  
  271.         [DllImport("kernel32.dll")]
  272.         private static extern uint ResumeThread(UIntPtr hThread);
  273.  
  274.         [DllImport("kernel32.dll")]
  275.         private static extern uint GetCurrentThreadId();
  276.  
  277.         [DllImport("kernel32.dll")]
  278.         private static extern UIntPtr CreateRemoteThread(UIntPtr hProcess, UIntPtr lpThreadAttributes, uint dwStackSize, UIntPtr lpStartAddress, UIntPtr lpParameter, uint dwCreationFlags, out uint lpThreadId);
  279.  
  280.         [DllImport("kernel32.dll")]
  281.         private static extern uint GetThreadId(UIntPtr Thread);
  282.  
  283.         [DllImport("kernel32.dll")]
  284.         private static extern bool TerminateThread(UIntPtr hThread, uint dwExitCode);
  285.  
  286.         #endregion
  287.  
  288.         #region Miscellaneous
  289.  
  290.         public static T TypeOf<T>(byte[] Value)
  291.         {
  292.             if (typeof(T) == typeof(bool))
  293.                 return (T)Convert.ChangeType(BitConverter.ToBoolean(Value, 0), typeof(T));
  294.  
  295.             if (typeof(T) == typeof(bool[]))
  296.             {
  297.                 bool[] A = new bool[Value.Length / SizeOf<bool>()];
  298.                 List<byte> B = new List<byte>(Value);
  299.                 int C = (int)SizeOf<bool>();
  300.  
  301.                 for (int D = 0; D < A.Length; D++)
  302.                 {
  303.                     A[D] = TypeOf<bool>(B.GetRange(D * C, C).ToArray());
  304.                 }
  305.  
  306.                 return (T)Convert.ChangeType(A, typeof(T));
  307.             }
  308.  
  309.             if (typeof(T) == typeof(byte))
  310.                 return (T)Convert.ChangeType(Value[0], typeof(T));
  311.  
  312.             if (typeof(T) == typeof(char))
  313.                 return (T)Convert.ChangeType(TypeOf<char>(Value, Encoding.Default), typeof(T));
  314.  
  315.             if (typeof(T) == typeof(char[]))
  316.             {
  317.                 char[] A = new char[Value.Length / SizeOf<char>()];
  318.                 List<byte> B = new List<byte>(Value);
  319.                 int C = (int)SizeOf<char>();
  320.  
  321.                 for (int D = 0; D < A.Length; D++)
  322.                 {
  323.                     A[D] = TypeOf<char>(B.GetRange(D * C, C).ToArray());
  324.                 }
  325.  
  326.                 return (T)Convert.ChangeType(A, typeof(T));
  327.             }
  328.  
  329.             if (typeof(T) == typeof(double))
  330.                 return (T)Convert.ChangeType(BitConverter.ToDouble(Value, 0), typeof(T));
  331.  
  332.             if (typeof(T) == typeof(double[]))
  333.             {
  334.                 double[] A = new double[Value.Length / SizeOf<double>()];
  335.                 List<byte> B = new List<byte>(Value);
  336.                 int C = (int)SizeOf<double>();
  337.  
  338.                 for (int D = 0; D < A.Length; D++)
  339.                 {
  340.                     A[D] = TypeOf<double>(B.GetRange(D * C, C).ToArray());
  341.                 }
  342.  
  343.                 return (T)Convert.ChangeType(A, typeof(T));
  344.             }
  345.  
  346.             if (typeof(T) == typeof(sbyte))
  347.                 return (T)Convert.ChangeType((sbyte)Value[0], typeof(T));
  348.  
  349.             if (typeof(T) == typeof(sbyte[]))
  350.             {
  351.                 sbyte[] A = new sbyte[Value.Length / SizeOf<sbyte>()];
  352.                 List<byte> B = new List<byte>(Value);
  353.                 int C = (int)SizeOf<sbyte>();
  354.  
  355.                 for (int D = 0; D < A.Length; D++)
  356.                 {
  357.                     A[D] = TypeOf<sbyte>(B.GetRange(D * C, C).ToArray());
  358.                 }
  359.  
  360.                 return (T)Convert.ChangeType(A, typeof(T));
  361.             }
  362.  
  363.             if (typeof(T) == typeof(short))
  364.                 return (T)Convert.ChangeType(BitConverter.ToInt16(Value, 0), typeof(T));
  365.  
  366.             if (typeof(T) == typeof(short[]))
  367.             {
  368.                 short[] A = new short[Value.Length / SizeOf<short>()];
  369.                 List<byte> B = new List<byte>(Value);
  370.                 int C = (int)SizeOf<short>();
  371.  
  372.                 for (int D = 0; D < A.Length; D++)
  373.                 {
  374.                     A[D] = TypeOf<short>(B.GetRange(D * C, C).ToArray());
  375.                 }
  376.  
  377.                 return (T)Convert.ChangeType(A, typeof(T));
  378.             }
  379.  
  380.             if (typeof(T) == typeof(string))
  381.                 return (T)Convert.ChangeType(TypeOf<string>(Value, Encoding.Default), typeof(T));
  382.  
  383.             if (typeof(T) == typeof(int))
  384.                 return (T)Convert.ChangeType(BitConverter.ToInt32(Value, 0), typeof(T));
  385.  
  386.             if (typeof(T) == typeof(int[]))
  387.             {
  388.                 int[] A = new int[Value.Length / SizeOf<int>()];
  389.                 List<byte> B = new List<byte>(Value);
  390.                 int C = (int)SizeOf<int>();
  391.  
  392.                 for (int D = 0; D < A.Length; D++)
  393.                 {
  394.                     A[D] = TypeOf<int>(B.GetRange(D * C, C).ToArray());
  395.                 }
  396.  
  397.                 return (T)Convert.ChangeType(A, typeof(T));
  398.             }
  399.  
  400.             if (typeof(T) == typeof(long))
  401.                 return (T)Convert.ChangeType(BitConverter.ToInt64(Value, 0), typeof(T));
  402.  
  403.             if (typeof(T) == typeof(long[]))
  404.             {
  405.                 long[] A = new long[Value.Length / SizeOf<long>()];
  406.                 List<byte> B = new List<byte>(Value);
  407.                 int C = (int)SizeOf<long>();
  408.  
  409.                 for (int D = 0; D < A.Length; D++)
  410.                 {
  411.                     A[D] = TypeOf<long>(B.GetRange(D * C, C).ToArray());
  412.                 }
  413.  
  414.                 return (T)Convert.ChangeType(A, typeof(T));
  415.             }
  416.  
  417.             if (typeof(T) == typeof(float))
  418.                 return (T)Convert.ChangeType(BitConverter.ToSingle(Value, 0), typeof(T));
  419.  
  420.             if (typeof(T) == typeof(float[]))
  421.             {
  422.                 float[] A = new float[Value.Length / SizeOf<float>()];
  423.                 List<byte> B = new List<byte>(Value);
  424.                 int C = (int)SizeOf<float>();
  425.  
  426.                 for (int D = 0; D < A.Length; D++)
  427.                 {
  428.                     A[D] = TypeOf<float>(B.GetRange(D * C, C).ToArray());
  429.                 }
  430.  
  431.                 return (T)Convert.ChangeType(A, typeof(T));
  432.             }
  433.  
  434.             if (typeof(T) == typeof(ushort))
  435.                 return (T)Convert.ChangeType(BitConverter.ToUInt16(Value, 0), typeof(T));
  436.  
  437.             if (typeof(T) == typeof(ushort[]))
  438.             {
  439.                 ushort[] A = new ushort[Value.Length / SizeOf<ushort>()];
  440.                 List<byte> B = new List<byte>(Value);
  441.                 int C = (int)SizeOf<ushort>();
  442.  
  443.                 for (int D = 0; D < A.Length; D++)
  444.                 {
  445.                     A[D] = TypeOf<ushort>(B.GetRange(D * C, C).ToArray());
  446.                 }
  447.  
  448.                 return (T)Convert.ChangeType(A, typeof(T));
  449.             }
  450.  
  451.             if (typeof(T) == typeof(uint))
  452.                 return (T)Convert.ChangeType(BitConverter.ToUInt32(Value, 0), typeof(T));
  453.  
  454.             if (typeof(T) == typeof(uint[]))
  455.             {
  456.                 uint[] A = new uint[Value.Length / SizeOf<uint>()];
  457.                 List<byte> B = new List<byte>(Value);
  458.                 int C = (int)SizeOf<uint>();
  459.  
  460.                 for (int D = 0; D < A.Length; D++)
  461.                 {
  462.                     A[D] = TypeOf<uint>(B.GetRange(D * C, C).ToArray());
  463.                 }
  464.  
  465.                 return (T)Convert.ChangeType(A, typeof(T));
  466.             }
  467.  
  468.             if (typeof(T) == typeof(ulong))
  469.                 return (T)Convert.ChangeType(BitConverter.ToUInt64(Value, 0), typeof(T));
  470.  
  471.             if (typeof(T) == typeof(ulong[]))
  472.             {
  473.                 ulong[] A = new ulong[Value.Length / SizeOf<ulong>()];
  474.                 List<byte> B = new List<byte>(Value);
  475.                 int C = (int)SizeOf<ulong>();
  476.  
  477.                 for (int D = 0; D < A.Length; D++)
  478.                 {
  479.                     A[D] = TypeOf<ulong>(B.GetRange(D * C, C).ToArray());
  480.                 }
  481.  
  482.                 return (T)Convert.ChangeType(A, typeof(T));
  483.             }
  484.  
  485.             if (typeof(T) == typeof(IntPtr[]))
  486.             {
  487.                 IntPtr[] A = new IntPtr[Value.Length / SizeOf<IntPtr>()];
  488.                 List<byte> B = new List<byte>(Value);
  489.                 int C = (int)SizeOf<IntPtr>();
  490.  
  491.                 for (int D = 0; D < A.Length; D++)
  492.                 {
  493.                     A[D] = TypeOf<IntPtr>(B.GetRange(D * C, C).ToArray());
  494.                 }
  495.  
  496.                 return (T)Convert.ChangeType(A, typeof(T));
  497.             }
  498.  
  499.             if (typeof(T) == typeof(UIntPtr[]))
  500.             {
  501.                 UIntPtr[] A = new UIntPtr[Value.Length / SizeOf<UIntPtr>()];
  502.                 List<byte> B = new List<byte>(Value);
  503.                 int C = (int)SizeOf<UIntPtr>();
  504.  
  505.                 for (int D = 0; D < A.Length; D++)
  506.                 {
  507.                     A[D] = TypeOf<UIntPtr>(B.GetRange(D * C, C).ToArray());
  508.                 }
  509.  
  510.                 return (T)Convert.ChangeType(A, typeof(T));
  511.             }
  512.  
  513.             else
  514.             {
  515.                 T A;
  516.                 uint B = SizeOf<T>();
  517.                 IntPtr C = Marshal.AllocHGlobal((int)B);
  518.  
  519.                 Marshal.Copy(Value, 0, C, (int)B);
  520.  
  521.                 A = (T)Marshal.PtrToStructure(C, typeof(T));
  522.  
  523.                 Marshal.FreeHGlobal(C);
  524.  
  525.                 return A;
  526.             }
  527.         }
  528.  
  529.         public static T TypeOf<T>(byte[] Value, Encoding Encoding)
  530.         {
  531.             if (typeof(T) == typeof(char))
  532.                 return (T)Convert.ChangeType(Encoding.GetChars(Value)[0], typeof(T));
  533.  
  534.             if (typeof(T) == typeof(char[]))
  535.             {
  536.                 char[] A = new char[Value.Length / SizeOf<char>(Encoding)];
  537.                 List<byte> B = new List<byte>(Value);
  538.                 int C = (int)SizeOf<char>(Encoding);
  539.  
  540.                 for (int D = 0; D < A.Length; D++)
  541.                 {
  542.                     A[D] = TypeOf<char>(B.GetRange(D * C, C).ToArray(), Encoding);
  543.                 }
  544.  
  545.                 return (T)Convert.ChangeType(A, typeof(T));
  546.             }
  547.  
  548.             if (typeof(T) == typeof(string))
  549.                 return (T)Convert.ChangeType(Encoding.GetString(Value), typeof(T));
  550.  
  551.             else
  552.                 return TypeOf<T>(Value);
  553.         }
  554.  
  555.         public static byte[] TypeOf<T>(T Value)
  556.         {
  557.             if (typeof(T) == typeof(bool))
  558.                 return BitConverter.GetBytes((bool)Convert.ChangeType(Value, typeof(bool)));
  559.  
  560.             if (typeof(T) == typeof(bool[]))
  561.             {
  562.                 bool[] A = (bool[])Convert.ChangeType(Value, typeof(bool[]));
  563.                 List<byte> B = new List<byte>();
  564.  
  565.                 foreach (bool C in A)
  566.                 {
  567.                     B.AddRange(TypeOf(C));
  568.                 }
  569.  
  570.                 return B.ToArray();
  571.             }
  572.  
  573.             if (typeof(T) == typeof(byte))
  574.                 return new byte[] { (byte)Convert.ChangeType(Value, typeof(byte)) };
  575.  
  576.             if (typeof(T) == typeof(char))
  577.                 return TypeOf((char)Convert.ChangeType(Value, typeof(char)), Encoding.Default);
  578.  
  579.             if (typeof(T) == typeof(char[]))
  580.             {
  581.                 char[] A = (char[])Convert.ChangeType(Value, typeof(char[]));
  582.                 List<byte> B = new List<byte>();
  583.  
  584.                 foreach (char C in A)
  585.                 {
  586.                     B.AddRange(TypeOf(C));
  587.                 }
  588.  
  589.                 return B.ToArray();
  590.             }
  591.  
  592.             if (typeof(T) == typeof(double))
  593.                 return BitConverter.GetBytes((double)Convert.ChangeType(Value, typeof(double)));
  594.  
  595.             if (typeof(T) == typeof(double[]))
  596.             {
  597.                 double[] A = (double[])Convert.ChangeType(Value, typeof(double[]));
  598.                 List<byte> B = new List<byte>();
  599.  
  600.                 foreach (double C in A)
  601.                 {
  602.                     B.AddRange(TypeOf(C));
  603.                 }
  604.  
  605.                 return B.ToArray();
  606.             }
  607.  
  608.             if (typeof(T) == typeof(sbyte))
  609.                 return new byte[] { (byte)(sbyte)Convert.ChangeType(Value, typeof(sbyte)) };
  610.  
  611.             if (typeof(T) == typeof(sbyte[]))
  612.             {
  613.                 sbyte[] A = (sbyte[])Convert.ChangeType(Value, typeof(sbyte[]));
  614.                 List<byte> B = new List<byte>();
  615.  
  616.                 foreach (sbyte C in A)
  617.                 {
  618.                     B.AddRange(TypeOf(C));
  619.                 }
  620.  
  621.                 return B.ToArray();
  622.             }
  623.  
  624.             if (typeof(T) == typeof(short))
  625.                 return BitConverter.GetBytes((short)Convert.ChangeType(Value, typeof(short)));
  626.  
  627.             if (typeof(T) == typeof(short[]))
  628.             {
  629.                 short[] A = (short[])Convert.ChangeType(Value, typeof(short[]));
  630.                 List<byte> B = new List<byte>();
  631.  
  632.                 foreach (short C in A)
  633.                 {
  634.                     B.AddRange(TypeOf(C));
  635.                 }
  636.  
  637.                 return B.ToArray();
  638.             }
  639.  
  640.             if (typeof(T) == typeof(string))
  641.                 return TypeOf((string)Convert.ChangeType(Value, typeof(string)), Encoding.Default);
  642.  
  643.             if (typeof(T) == typeof(int))
  644.                 return BitConverter.GetBytes((int)Convert.ChangeType(Value, typeof(int)));
  645.  
  646.             if (typeof(T) == typeof(int[]))
  647.             {
  648.                 int[] A = (int[])Convert.ChangeType(Value, typeof(int[]));
  649.                 List<byte> B = new List<byte>();
  650.  
  651.                 foreach (int C in A)
  652.                 {
  653.                     B.AddRange(TypeOf(C));
  654.                 }
  655.  
  656.                 return B.ToArray();
  657.             }
  658.  
  659.             if (typeof(T) == typeof(long))
  660.                 return BitConverter.GetBytes((long)Convert.ChangeType(Value, typeof(long)));
  661.  
  662.             if (typeof(T) == typeof(long[]))
  663.             {
  664.                 long[] A = (long[])Convert.ChangeType(Value, typeof(long[]));
  665.                 List<byte> B = new List<byte>();
  666.  
  667.                 foreach (long C in A)
  668.                 {
  669.                     B.AddRange(TypeOf(C));
  670.                 }
  671.  
  672.                 return B.ToArray();
  673.             }
  674.  
  675.             if (typeof(T) == typeof(float))
  676.                 return BitConverter.GetBytes((float)Convert.ChangeType(Value, typeof(float)));
  677.  
  678.             if (typeof(T) == typeof(float[]))
  679.             {
  680.                 float[] A = (float[])Convert.ChangeType(Value, typeof(float[]));
  681.                 List<byte> B = new List<byte>();
  682.  
  683.                 foreach (float C in A)
  684.                 {
  685.                     B.AddRange(TypeOf(C));
  686.                 }
  687.  
  688.                 return B.ToArray();
  689.             }
  690.  
  691.             if (typeof(T) == typeof(ushort))
  692.                 return BitConverter.GetBytes((ushort)Convert.ChangeType(Value, typeof(ushort)));
  693.  
  694.             if (typeof(T) == typeof(ushort[]))
  695.             {
  696.                 ushort[] A = (ushort[])Convert.ChangeType(Value, typeof(ushort[]));
  697.                 List<byte> B = new List<byte>();
  698.  
  699.                 foreach (ushort C in A)
  700.                 {
  701.                     B.AddRange(TypeOf(C));
  702.                 }
  703.  
  704.                 return B.ToArray();
  705.             }
  706.  
  707.             if (typeof(T) == typeof(uint))
  708.                 return BitConverter.GetBytes((uint)Convert.ChangeType(Value, typeof(uint)));
  709.  
  710.             if (typeof(T) == typeof(uint[]))
  711.             {
  712.                 uint[] A = (uint[])Convert.ChangeType(Value, typeof(uint[]));
  713.                 List<byte> B = new List<byte>();
  714.  
  715.                 foreach (uint C in A)
  716.                 {
  717.                     B.AddRange(TypeOf(C));
  718.                 }
  719.  
  720.                 return B.ToArray();
  721.             }
  722.  
  723.             if (typeof(T) == typeof(ulong))
  724.                 return BitConverter.GetBytes((ulong)Convert.ChangeType(Value, typeof(ulong)));
  725.  
  726.             if (typeof(T) == typeof(ulong[]))
  727.             {
  728.                 ulong[] A = (ulong[])Convert.ChangeType(Value, typeof(ulong[]));
  729.                 List<byte> B = new List<byte>();
  730.  
  731.                 foreach (ulong C in A)
  732.                 {
  733.                     B.AddRange(TypeOf(C));
  734.                 }
  735.  
  736.                 return B.ToArray();
  737.             }
  738.  
  739.             if (typeof(T) == typeof(IntPtr[]))
  740.             {
  741.                 IntPtr[] A = (IntPtr[])Convert.ChangeType(Value, typeof(IntPtr[]));
  742.                 List<byte> B = new List<byte>();
  743.  
  744.                 foreach (IntPtr C in A)
  745.                 {
  746.                     B.AddRange(TypeOf(C));
  747.                 }
  748.  
  749.                 return B.ToArray();
  750.             }
  751.  
  752.             if (typeof(T) == typeof(UIntPtr[]))
  753.             {
  754.                 UIntPtr[] A = (UIntPtr[])Convert.ChangeType(Value, typeof(UIntPtr[]));
  755.                 List<byte> B = new List<byte>();
  756.  
  757.                 foreach (UIntPtr C in A)
  758.                 {
  759.                     B.AddRange(TypeOf(C));
  760.                 }
  761.  
  762.                 return B.ToArray();
  763.             }
  764.  
  765.             else
  766.             {
  767.                 uint A = SizeOf<T>();
  768.                 byte[] B = new byte[A];
  769.                 IntPtr C = Marshal.AllocHGlobal((int)A);
  770.  
  771.                 Marshal.StructureToPtr(Value, C, true);
  772.                 Marshal.Copy(C, B, 0, (int)A);
  773.                 Marshal.FreeHGlobal(C);
  774.  
  775.                 return B;
  776.             }
  777.         }
  778.  
  779.         public static byte[] TypeOf<T>(T Value, Encoding Encoding)
  780.         {
  781.             if (typeof(T) == typeof(char))
  782.                 return Encoding.GetBytes(new char[] { (char)Convert.ChangeType(Value, typeof(char)) });
  783.  
  784.             if (typeof(T) == typeof(char[]))
  785.             {
  786.                 char[] A = (char[])Convert.ChangeType(Value, typeof(char[]));
  787.                 List<byte> B = new List<byte>();
  788.  
  789.                 foreach (char C in A)
  790.                 {
  791.                     B.AddRange(TypeOf(C, Encoding));
  792.                 }
  793.  
  794.                 return B.ToArray();
  795.             }
  796.  
  797.             if (typeof(T) == typeof(string))
  798.                 return Encoding.GetBytes((string)Convert.ChangeType(Value, typeof(string)));
  799.  
  800.             else
  801.                 return TypeOf(Value);
  802.         }
  803.  
  804.         public static uint SizeOf<T>()
  805.         {
  806.             if (typeof(T) == typeof(bool))
  807.                 return 1;
  808.  
  809.             if (typeof(T) == typeof(char))
  810.                 return SizeOf<char>(Encoding.Default);
  811.  
  812.             else
  813.                 return (uint)Marshal.SizeOf<T>();
  814.         }
  815.  
  816.         public static uint SizeOf<T>(Encoding Encoding)
  817.         {
  818.             if (typeof(T) == typeof(char))
  819.                 return (uint)Encoding.GetMaxByteCount(0);
  820.  
  821.             else
  822.                 return SizeOf<T>();
  823.         }
  824.  
  825.         #endregion
  826.  
  827.         #region Process & Memory
  828.  
  829.         public class Process
  830.         {
  831.  
  832.             private PROCESSENTRY32 _Information = new PROCESSENTRY32();
  833.             private bool _Exists = false;
  834.             private UIntPtr _Handle = UIntPtr.Zero;
  835.  
  836.             static Process()
  837.             {
  838.                 return;
  839.             }
  840.  
  841.             public Process()
  842.             {
  843.                 _Information = new PROCESSENTRY32();
  844.                 _Exists = (_Information.dwSize != 0);
  845.  
  846.                 return;
  847.             }
  848.  
  849.             internal Process(PROCESSENTRY32 Information)
  850.             {
  851.                 _Information = Information;
  852.                 _Exists = (_Information.dwSize != 0);
  853.  
  854.                 return;
  855.             }
  856.  
  857.             public bool Exists
  858.             {
  859.                 get { return _Exists; }
  860.             }
  861.  
  862.             public Module[] Modules
  863.             {
  864.                 get { return EnumerateModules(this); }
  865.             }
  866.  
  867.             public Module MainModule
  868.             {
  869.                 get
  870.                 {
  871.                     MODULEENTRY32 A = new MODULEENTRY32();
  872.  
  873.                     A.dwSize = SizeOf<MODULEENTRY32>();
  874.  
  875.                     UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Id);
  876.  
  877.                     if (Module32First(C, ref A))
  878.                     {
  879.                         CloseHandle(C);
  880.                         return new Module(A);
  881.                     }
  882.  
  883.                     CloseHandle(C);
  884.                     return new Module();
  885.                 }
  886.             }
  887.  
  888.             public Thread[] Threads
  889.             {
  890.                 get { return EnumerateThreads(this); }
  891.             }
  892.  
  893.             public Window[] Windows
  894.             {
  895.                 get { return EnumerateWindows(this); }
  896.             }
  897.  
  898.             public Window MainWindow
  899.             {
  900.                 get { return GetMainWindow(this); }
  901.             }
  902.  
  903.             public void Suspend()
  904.             {
  905.                 foreach(Thread A in Threads)
  906.                 {
  907.                     A.Open();
  908.                     A.Suspend();
  909.                     A.Close();
  910.                 }
  911.  
  912.                 return;
  913.             }
  914.  
  915.             public void Resume()
  916.             {
  917.                 foreach (Thread A in Threads)
  918.                 {
  919.                     A.Open();
  920.                     A.Resume();
  921.                     A.Close();
  922.                 }
  923.  
  924.                 return;
  925.             }
  926.  
  927.             public Process Parent
  928.             {
  929.                 get { return FindProcess(_Information.th32ParentProcessID); }
  930.             }
  931.  
  932.             public Process[] Children
  933.             {
  934.                 get
  935.                 {
  936.                     PROCESSENTRY32 A = new PROCESSENTRY32();
  937.                     List<Process> B = new List<Process>();
  938.  
  939.                     A.dwSize = SizeOf<PROCESSENTRY32>();
  940.  
  941.                     UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
  942.  
  943.                     if (Process32First(C, ref A))
  944.                     {
  945.                         do
  946.                         {
  947.                             if (Id == A.th32ParentProcessID && Id != A.th32ProcessID)
  948.                                 B.Add(new Process(A));
  949.                         } while (Process32Next(C, ref A));
  950.                     }
  951.  
  952.                     CloseHandle(C);
  953.  
  954.                     return B.ToArray();
  955.                 }
  956.             }
  957.  
  958.             public uint Id
  959.             {
  960.                 get { return _Information.th32ProcessID; }
  961.             }
  962.  
  963.             public string Name
  964.             {
  965.                 get { return _Information.szExeFile; }
  966.             }
  967.  
  968.             public string FileName
  969.             {
  970.                 get { return MainModule.FileName; }
  971.             }
  972.  
  973.             public UIntPtr Handle
  974.             {
  975.                 get
  976.                 {
  977.                     if (_Handle != UIntPtr.Zero)
  978.                         return _Handle;
  979.  
  980.                     else
  981.                         return UIntPtr.Zero;
  982.                 }
  983.             }
  984.  
  985.             public void Open()
  986.             {
  987.                 Close();
  988.                 _Handle = OpenProcess(0x1FFFFF, false, Id);
  989.                 return;
  990.             }
  991.  
  992.             public void Close()
  993.             {
  994.                 if (_Handle != UIntPtr.Zero)
  995.                 {
  996.                     CloseHandle(Handle);
  997.                     _Handle = UIntPtr.Zero;
  998.                 }
  999.  
  1000.                 return;
  1001.             }
  1002.  
  1003.             public void Terminate()
  1004.             {
  1005.                 TerminateProcess(Handle, 0xFFFFFFFF);
  1006.                 return;
  1007.             }
  1008.  
  1009.             public void TerminateTree()
  1010.             {
  1011.                 foreach (Process A in Children)
  1012.                 {
  1013.                     if (A.Children.Length != 0)
  1014.                     {
  1015.                         A.TerminateTree();
  1016.                     }
  1017.  
  1018.                     A.Terminate();
  1019.                 }
  1020.  
  1021.                 Terminate();
  1022.                 return;
  1023.             }
  1024.  
  1025.             public byte[] ReadMemory(UIntPtr Address, uint Size)
  1026.             {
  1027.                 byte[] A = new byte[Size];
  1028.                 uint B;
  1029.  
  1030.                 ReadProcessMemory(Handle, Address, A, Size, out B);
  1031.  
  1032.                 return A;
  1033.             }
  1034.  
  1035.             public void WriteMemory(UIntPtr Address, byte[] Value)
  1036.             {
  1037.                 uint A;
  1038.  
  1039.                 WriteProcessMemory(Handle, Address, Value, (uint)Value.Length, out A);
  1040.                 return;
  1041.             }
  1042.  
  1043.             public UIntPtr[] ScanMemory(UIntPtr StartAddress, byte[] Value, uint Range)
  1044.             {
  1045.                 UIntPtr A = (UIntPtr)(StartAddress.ToUInt64() + Range);
  1046.                 UIntPtr B = StartAddress;
  1047.                 MEMORY_BASIC_INFORMATION C = new MEMORY_BASIC_INFORMATION();
  1048.                 List<UIntPtr> D = new List<UIntPtr>();
  1049.  
  1050.                 do
  1051.                 {
  1052.                     VirtualQueryEx(Handle, B, out C, SizeOf<MEMORY_BASIC_INFORMATION>());
  1053.  
  1054.                     if ((C.Type == 0x20000 || C.Type == 0x1000000) && (C.State == 0x1000 || C.State == 0x2000))
  1055.                     {
  1056.                         byte[] E = ReadMemory(C.BaseAddress, C.RegionSize.ToUInt32());
  1057.  
  1058.                         for (uint F = 0; F < C.RegionSize.ToUInt32() - Value.Length; F++)
  1059.                         {
  1060.                             bool G = true;
  1061.  
  1062.                             for (uint H = 0; H < Value.Length; H++)
  1063.                             {
  1064.                                 if (!(Value[H] == E[F + H]) || !(C.BaseAddress.ToUInt64() + F < A.ToUInt64()) || !(C.BaseAddress.ToUInt64() + F >= StartAddress.ToUInt64()))
  1065.                                 {
  1066.                                     G = false;
  1067.  
  1068.                                     break;
  1069.                                 }
  1070.                             }
  1071.  
  1072.                             if (G)
  1073.                             {
  1074.                                 D.Add((UIntPtr)(C.BaseAddress.ToUInt64() + F));
  1075.                             }
  1076.                         }
  1077.                     }
  1078.  
  1079.                     B = (UIntPtr)(B.ToUInt64() + C.RegionSize.ToUInt32());
  1080.                 } while (B.ToUInt64() < A.ToUInt64());
  1081.  
  1082.                 return D.ToArray();
  1083.             }
  1084.  
  1085.             public UIntPtr[] ScanMemory(UIntPtr StartAddress, byte[] Value, uint Range, byte Ignore)
  1086.             {
  1087.                 UIntPtr A = (UIntPtr)(StartAddress.ToUInt64() + Range);
  1088.                 UIntPtr B = StartAddress;
  1089.                 MEMORY_BASIC_INFORMATION C = new MEMORY_BASIC_INFORMATION();
  1090.                 List<UIntPtr> D = new List<UIntPtr>();
  1091.  
  1092.                 do
  1093.                 {
  1094.                     VirtualQueryEx(Handle, B, out C, SizeOf<MEMORY_BASIC_INFORMATION>());
  1095.  
  1096.                     if ((C.Type == 0x20000 || C.Type == 0x1000000) && (C.State == 0x1000 || C.State == 0x2000))
  1097.                     {
  1098.                         byte[] E = ReadMemory(C.BaseAddress, C.RegionSize.ToUInt32());
  1099.  
  1100.                         for (uint F = 0; F < C.RegionSize.ToUInt32() - Value.Length; F++)
  1101.                         {
  1102.                             bool G = true;
  1103.  
  1104.                             for (uint H = 0; H < Value.Length; H++)
  1105.                             {
  1106.                                 if ((!(Value[H] == E[F + H]) || !(C.BaseAddress.ToUInt64() + F < A.ToUInt64()) || !(C.BaseAddress.ToUInt64() + F >= StartAddress.ToUInt64())) && Ignore != Value[H])
  1107.                                 {
  1108.                                     G = false;
  1109.  
  1110.                                     break;
  1111.                                 }
  1112.                             }
  1113.  
  1114.                             if (G)
  1115.                             {
  1116.                                 D.Add((UIntPtr)(C.BaseAddress.ToUInt64() + F));
  1117.                             }
  1118.                         }
  1119.                     }
  1120.  
  1121.                     B = (UIntPtr)(B.ToUInt64() + C.RegionSize.ToUInt32());
  1122.                 } while (B.ToUInt64() < A.ToUInt64());
  1123.  
  1124.                 return D.ToArray();
  1125.             }
  1126.  
  1127.             public UIntPtr SearchMemory(UIntPtr StartAddress, byte[] Value, uint Range)
  1128.             {
  1129.                 UIntPtr A = (UIntPtr)(StartAddress.ToUInt64() + Range);
  1130.                 UIntPtr B = StartAddress;
  1131.                 MEMORY_BASIC_INFORMATION C = new MEMORY_BASIC_INFORMATION();
  1132.  
  1133.                 do
  1134.                 {
  1135.                     VirtualQueryEx(Handle, B, out C, SizeOf<MEMORY_BASIC_INFORMATION>());
  1136.  
  1137.                     if ((C.Type == 0x20000 || C.Type == 0x1000000) && (C.State == 0x1000 || C.State == 0x2000))
  1138.                     {
  1139.                         byte[] E = ReadMemory(C.BaseAddress, C.RegionSize.ToUInt32());
  1140.  
  1141.                         for (uint F = 0; F < C.RegionSize.ToUInt32() - Value.Length; F++)
  1142.                         {
  1143.                             bool G = true;
  1144.  
  1145.                             for (uint H = 0; H < Value.Length; H++)
  1146.                             {
  1147.                                 if (!(Value[H] == E[F + H]) || !(C.BaseAddress.ToUInt64() + F < A.ToUInt64()) || !(C.BaseAddress.ToUInt64() + F >= StartAddress.ToUInt64()))
  1148.                                 {
  1149.                                     G = false;
  1150.  
  1151.                                     break;
  1152.                                 }
  1153.                             }
  1154.  
  1155.                             if (G)
  1156.                             {
  1157.                                 return ((UIntPtr)(C.BaseAddress.ToUInt64() + F));
  1158.                             }
  1159.                         }
  1160.                     }
  1161.  
  1162.                     B = (UIntPtr)(B.ToUInt64() + C.RegionSize.ToUInt32());
  1163.                 } while (B.ToUInt64() < A.ToUInt64());
  1164.  
  1165.                 return UIntPtr.Zero;
  1166.             }
  1167.  
  1168.             public UIntPtr SearchMemory(UIntPtr StartAddress, byte[] Value, uint Range, byte Ignore)
  1169.             {
  1170.                 UIntPtr A = (UIntPtr)(StartAddress.ToUInt64() + Range);
  1171.                 UIntPtr B = StartAddress;
  1172.                 MEMORY_BASIC_INFORMATION C = new MEMORY_BASIC_INFORMATION();
  1173.  
  1174.                 do
  1175.                 {
  1176.                     VirtualQueryEx(Handle, B, out C, SizeOf<MEMORY_BASIC_INFORMATION>());
  1177.  
  1178.                     if ((C.Type == 0x20000 || C.Type == 0x1000000) && (C.State == 0x1000 || C.State == 0x2000))
  1179.                     {
  1180.                         byte[] E = ReadMemory(C.BaseAddress, C.RegionSize.ToUInt32());
  1181.  
  1182.                         for (uint F = 0; F < C.RegionSize.ToUInt32() - Value.Length; F++)
  1183.                         {
  1184.                             bool G = true;
  1185.  
  1186.                             for (uint H = 0; H < Value.Length; H++)
  1187.                             {
  1188.                                 if ((!(Value[H] == E[F + H]) || !(C.BaseAddress.ToUInt64() + F < A.ToUInt64()) || !(C.BaseAddress.ToUInt64() + F >= StartAddress.ToUInt64())) && Ignore != Value[H])
  1189.                                 {
  1190.                                     G = false;
  1191.  
  1192.                                     break;
  1193.                                 }
  1194.                             }
  1195.  
  1196.                             if (G)
  1197.                             {
  1198.                                 return ((UIntPtr)(C.BaseAddress.ToUInt64() + F));
  1199.                             }
  1200.                         }
  1201.                     }
  1202.  
  1203.                     B = (UIntPtr)(B.ToUInt64() + C.RegionSize.ToUInt32());
  1204.                 } while (B.ToUInt64() < A.ToUInt64());
  1205.  
  1206.                 return UIntPtr.Zero;
  1207.             }
  1208.  
  1209.             public UIntPtr AllocateMemory(uint Size, MemoryProtectionConstants Protection)
  1210.             {
  1211.                 return AllocateMemory(Size, Protection, UIntPtr.Zero);
  1212.             }
  1213.  
  1214.             public UIntPtr AllocateMemory(uint Size, MemoryProtectionConstants Protection, UIntPtr Address)
  1215.             {
  1216.                 return VirtualAllocEx(Handle, Address, Size, 0x1000 | 0x2000, (uint)Protection);
  1217.             }
  1218.  
  1219.             public void FreeMemory(UIntPtr Address)
  1220.             {
  1221.                 VirtualFreeEx(Handle, Address, 0, 0x8000);
  1222.             }
  1223.  
  1224.             public void FreeMemory(UIntPtr Address, uint Size)
  1225.             {
  1226.                 VirtualFreeEx(Handle, Address, Size, 0x4000);
  1227.             }
  1228.  
  1229.             public void FillMemory(UIntPtr Address, byte[] Value, uint Times)
  1230.             {
  1231.                 for (uint A = 0; A < Times; A++)
  1232.                 {
  1233.                     WriteMemory((UIntPtr)(Address.ToUInt64() + (ulong)(A * Value.Length)), Value);
  1234.                 }
  1235.             }
  1236.  
  1237.             public MemoryProtectionConstants ProtectMemory(UIntPtr Address, uint Size, MemoryProtectionConstants Protection)
  1238.             {
  1239.                 uint A;
  1240.  
  1241.                 VirtualProtectEx(Handle, Address, Size, (uint)Protection, out A);
  1242.  
  1243.                 return (MemoryProtectionConstants)A;
  1244.             }
  1245.  
  1246.         }
  1247.  
  1248.         public static Process[] EnumerateProcesses()
  1249.         {
  1250.             PROCESSENTRY32 A = new PROCESSENTRY32();
  1251.             List<Process> B = new List<Process>();
  1252.  
  1253.             A.dwSize = SizeOf<PROCESSENTRY32>();
  1254.  
  1255.             UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
  1256.  
  1257.             if (Process32First(C, ref A))
  1258.             {
  1259.                 do
  1260.                 {
  1261.                     B.Add(new Process(A));
  1262.                 } while (Process32Next(C, ref A));
  1263.             }
  1264.  
  1265.             CloseHandle(C);
  1266.  
  1267.             return B.ToArray();
  1268.         }
  1269.  
  1270.         public static Process[] FindProcesses(string Name)
  1271.         {
  1272.             PROCESSENTRY32 A = new PROCESSENTRY32();
  1273.             List<Process> B = new List<Process>();
  1274.  
  1275.             A.dwSize = SizeOf<PROCESSENTRY32>();
  1276.  
  1277.             UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
  1278.  
  1279.             if (Process32First(C, ref A))
  1280.             {
  1281.                 do
  1282.                 {
  1283.                     if (Name.ToLower() == A.szExeFile.ToLower())
  1284.                         B.Add(new Process(A));
  1285.                 } while (Process32Next(C, ref A));
  1286.             }
  1287.  
  1288.             CloseHandle(C);
  1289.             return B.ToArray();
  1290.         }
  1291.  
  1292.         public static Process FindProcess(string Name)
  1293.         {
  1294.             PROCESSENTRY32 A = new PROCESSENTRY32();
  1295.  
  1296.             A.dwSize = SizeOf<PROCESSENTRY32>();
  1297.  
  1298.             UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
  1299.  
  1300.             if (Process32First(C, ref A))
  1301.             {
  1302.                 do
  1303.                 {
  1304.                     if (Name.ToLower() == A.szExeFile.ToLower())
  1305.                     {
  1306.                         CloseHandle(C);
  1307.                         return new Process(A);
  1308.                     }
  1309.                 } while (Process32Next(C, ref A));
  1310.             }
  1311.  
  1312.             CloseHandle(C);
  1313.             return new Process();
  1314.         }
  1315.  
  1316.         public static Process FindProcess(uint Id)
  1317.         {
  1318.             PROCESSENTRY32 A = new PROCESSENTRY32();
  1319.  
  1320.             A.dwSize = SizeOf<PROCESSENTRY32>();
  1321.  
  1322.             UIntPtr C = CreateToolhelp32Snapshot(0x2, 0);
  1323.  
  1324.             if (Process32First(C, ref A))
  1325.             {
  1326.                 do
  1327.                 {
  1328.                     if (Id == A.th32ProcessID)
  1329.                     {
  1330.                         CloseHandle(C);
  1331.                         return new Process(A);
  1332.                     }
  1333.                 } while (Process32Next(C, ref A));
  1334.             }
  1335.  
  1336.             CloseHandle(C);
  1337.             return new Process();
  1338.         }
  1339.  
  1340.         public static Process GetCurrentProcess()
  1341.         {
  1342.             return FindProcess(GetCurrentProcessId());
  1343.         }
  1344.  
  1345.         public static Process CreateProcess(string FileName)
  1346.         {
  1347.             return CreateProcess(FileName, null, null);
  1348.         }
  1349.  
  1350.         public static Process CreateProcess(string FileName, string CommandLine)
  1351.         {
  1352.             return CreateProcess(FileName, CommandLine, null);
  1353.         }
  1354.  
  1355.         public static Process CreateProcess(string FileName, string CommandLine, string CurrentDirectory)
  1356.         {
  1357.             STARTUPINFO A = new STARTUPINFO();
  1358.             PROCESS_INFORMATION B = new PROCESS_INFORMATION();
  1359.  
  1360.             if (CreateProcess(FileName, CommandLine, UIntPtr.Zero, UIntPtr.Zero, false, 0, UIntPtr.Zero, CurrentDirectory, ref A, out B))
  1361.             {
  1362.                 CloseHandle(B.hProcess);
  1363.                 CloseHandle(B.hThread);
  1364.  
  1365.                 return FindProcess(B.dwProcessId);
  1366.             }
  1367.  
  1368.             return new Process();
  1369.         }
  1370.  
  1371.         #endregion
  1372.  
  1373.         #region Module
  1374.  
  1375.         public class Module
  1376.         {
  1377.  
  1378.             private MODULEENTRY32 _Information = new MODULEENTRY32();
  1379.             private bool _Exists = false;
  1380.  
  1381.             static Module()
  1382.             {
  1383.                 return;
  1384.             }
  1385.  
  1386.             public Module()
  1387.             {
  1388.                 _Information = new MODULEENTRY32();
  1389.                 _Exists = (_Information.dwSize != 0);
  1390.  
  1391.                 return;
  1392.             }
  1393.  
  1394.             internal Module(MODULEENTRY32 Information)
  1395.             {
  1396.                 _Information = Information;
  1397.                 _Exists = (_Information.dwSize != 0);
  1398.  
  1399.                 return;
  1400.             }
  1401.  
  1402.             public bool Exists
  1403.             {
  1404.                 get { return _Exists; }
  1405.             }
  1406.  
  1407.             public Symbol[] Symbols
  1408.             {
  1409.                 get { return EnumerateSymbols(this); }
  1410.             }
  1411.  
  1412.             public Process Process
  1413.             {
  1414.                 get { return FindProcess(_Information.th32ProcessID); }
  1415.             }
  1416.  
  1417.             public UIntPtr BaseAddress
  1418.             {
  1419.                 get { return _Information.modBaseAddr; }
  1420.             }
  1421.  
  1422.             public uint Size
  1423.             {
  1424.                 get { return _Information.modBaseSize; }
  1425.             }
  1426.  
  1427.             public string Name
  1428.             {
  1429.                 get { return _Information.szModule; }
  1430.             }
  1431.  
  1432.             public string FileName
  1433.             {
  1434.                 get { return _Information.szExePath; }
  1435.             }
  1436.  
  1437.             public UIntPtr EntryPoint
  1438.             {
  1439.                 get
  1440.                 {
  1441.                     MODULEINFO A = new MODULEINFO();
  1442.                     UIntPtr B = Process.Handle;
  1443.  
  1444.                     K32GetModuleInformation(B, _Information.hModule, out A, SizeOf<MODULEINFO>());
  1445.  
  1446.                     return A.EntryPoint;
  1447.                 }
  1448.             }
  1449.  
  1450.         }
  1451.  
  1452.         public static Module[] EnumerateModules(Process Process)
  1453.         {
  1454.             MODULEENTRY32 A = new MODULEENTRY32();
  1455.             List<Module> B = new List<Module>();
  1456.  
  1457.             A.dwSize = SizeOf<MODULEENTRY32>();
  1458.  
  1459.             UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Process.Id);
  1460.  
  1461.             if (Module32First(C, ref A))
  1462.             {
  1463.                 do
  1464.                 {
  1465.                     B.Add(new Module(A));
  1466.                 } while (Module32Next(C, ref A));
  1467.             }
  1468.  
  1469.             CloseHandle(C);
  1470.  
  1471.             return B.ToArray();
  1472.         }
  1473.  
  1474.         public static Module[] FindModules(Process Process, string Name)
  1475.         {
  1476.             MODULEENTRY32 A = new MODULEENTRY32();
  1477.             List<Module> B = new List<Module>();
  1478.  
  1479.             A.dwSize = SizeOf<MODULEENTRY32>();
  1480.  
  1481.             UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Process.Id);
  1482.  
  1483.             if (Module32First(C, ref A))
  1484.             {
  1485.                 do
  1486.                 {
  1487.                     if (Name.ToLower() == A.szModule.ToLower())
  1488.                         B.Add(new Module(A));
  1489.                 } while (Module32Next(C, ref A));
  1490.             }
  1491.  
  1492.             CloseHandle(C);
  1493.  
  1494.             return B.ToArray();
  1495.         }
  1496.  
  1497.         public static Module FindModule(Process Process, UIntPtr BaseAddress)
  1498.         {
  1499.             MODULEENTRY32 A = new MODULEENTRY32();
  1500.  
  1501.             A.dwSize = SizeOf<MODULEENTRY32>();
  1502.  
  1503.             UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Process.Id);
  1504.  
  1505.             if (Module32First(C, ref A))
  1506.             {
  1507.                 do
  1508.                 {
  1509.                     if (BaseAddress == A.hModule)
  1510.                     {
  1511.                         CloseHandle(C);
  1512.  
  1513.                         return new Module(A);
  1514.                     }
  1515.                 } while (Module32Next(C, ref A));
  1516.             }
  1517.  
  1518.             CloseHandle(C);
  1519.  
  1520.             return new Module();
  1521.         }
  1522.  
  1523.         public static Module FindModule(Process Process, string Name)
  1524.         {
  1525.             MODULEENTRY32 A = new MODULEENTRY32();
  1526.  
  1527.             A.dwSize = SizeOf<MODULEENTRY32>();
  1528.  
  1529.             UIntPtr C = CreateToolhelp32Snapshot(0x8 | 0x10, Process.Id);
  1530.  
  1531.             if (Module32First(C, ref A))
  1532.             {
  1533.                 do
  1534.                 {
  1535.                     if (Name.ToLower() == A.szModule.ToLower())
  1536.                     {
  1537.                         CloseHandle(C);
  1538.  
  1539.                         return new Module(A);
  1540.                     }
  1541.                 } while (Module32Next(C, ref A));
  1542.             }
  1543.  
  1544.             CloseHandle(C);
  1545.  
  1546.             return new Module();
  1547.         }
  1548.  
  1549.         #endregion
  1550.  
  1551.         #region Symbol
  1552.  
  1553.         public class Symbol
  1554.         {
  1555.  
  1556.             private SYMBOL_INFO _Information = new SYMBOL_INFO();
  1557.             private Module _Module = new Module();
  1558.             private bool _Exists = false;
  1559.  
  1560.             static Symbol()
  1561.             {
  1562.                 return;
  1563.             }
  1564.  
  1565.             public Symbol()
  1566.             {
  1567.                 _Information = new SYMBOL_INFO();
  1568.                 _Module = new Module();
  1569.                 _Exists = (_Information.SizeOfStruct != 0);
  1570.  
  1571.                 return;
  1572.             }
  1573.  
  1574.             internal Symbol(SYMBOL_INFO Information, Module Module)
  1575.             {
  1576.                 _Information = Information;
  1577.                 _Module = Module;
  1578.                 _Exists = (_Information.SizeOfStruct != 0);
  1579.  
  1580.                 return;
  1581.             }
  1582.  
  1583.             public bool Exists
  1584.             {
  1585.                 get { return _Exists; }
  1586.             }
  1587.  
  1588.             public string Name
  1589.             {
  1590.                 get { return _Information.Name; }
  1591.             }
  1592.  
  1593.             public ulong Value
  1594.             {
  1595.                 get { return _Information.Value; }
  1596.             }
  1597.  
  1598.             public uint Size
  1599.             {
  1600.                 get { return _Information.Size; }
  1601.             }
  1602.  
  1603.             public Module Module
  1604.             {
  1605.                 get { return _Module; }
  1606.             }
  1607.  
  1608.             public UIntPtr Address
  1609.             {
  1610.                 get { return new UIntPtr(_Information.Address - _Information.ModBase + Module.BaseAddress.ToUInt64()); }
  1611.             }
  1612.  
  1613.             public uint Index
  1614.             {
  1615.                 get { return _Information.Index; }
  1616.             }
  1617.  
  1618.         }
  1619.  
  1620.         private static List<Symbol> EnumeratedSymbols = new List<Symbol>();
  1621.  
  1622.         private static Symbol ReturnSymbol = new Symbol();
  1623.  
  1624.         private static Module ModuleForSymbols = new Module();
  1625.  
  1626.         private static bool SymEnumSymbolsProc1(ref SYMBOL_INFO pSymInfo, uint SymbolSize, UIntPtr UserContext)
  1627.         {
  1628.             EnumeratedSymbols.Add(new Symbol(pSymInfo, ModuleForSymbols));
  1629.             return true;
  1630.         }
  1631.  
  1632.         private static bool SymEnumSymbolsProc2(ref SYMBOL_INFO pSymInfo, uint SymbolSize, UIntPtr UserContext)
  1633.         {
  1634.             ReturnSymbol = (new Symbol(pSymInfo, ModuleForSymbols));
  1635.             return false;
  1636.         }
  1637.  
  1638.         public static Symbol[] EnumerateSymbols(Module Module)
  1639.         {
  1640.             EnumeratedSymbols.Clear();
  1641.             ModuleForSymbols = Module;
  1642.             UIntPtr A = Module.Process.Handle;
  1643.  
  1644.             SymInitialize(A, null, false);
  1645.  
  1646.             ulong B = SymLoadModuleEx(A, UIntPtr.Zero, Module.FileName, null, 0, 0, UIntPtr.Zero, 0);
  1647.  
  1648.             SymEnumSymbols(A, B, "*", new SymEnumSymbolsProc(SymEnumSymbolsProc1), UIntPtr.Zero);
  1649.             SymCleanup(A);
  1650.  
  1651.             return EnumeratedSymbols.ToArray();
  1652.         }
  1653.  
  1654.         public static Symbol[] FindSymbols(Module Module, string Name)
  1655.         {
  1656.             EnumeratedSymbols.Clear();
  1657.             ModuleForSymbols = Module;
  1658.             UIntPtr A = Module.Process.Handle;
  1659.  
  1660.             SymInitialize(A, null, false);
  1661.  
  1662.             ulong B = SymLoadModuleEx(A, UIntPtr.Zero, Module.FileName, null, 0, 0, UIntPtr.Zero, 0);
  1663.  
  1664.             SymEnumSymbols(A, B, Name, new SymEnumSymbolsProc(SymEnumSymbolsProc1), UIntPtr.Zero);
  1665.             SymCleanup(A);
  1666.  
  1667.             return EnumeratedSymbols.ToArray();
  1668.         }
  1669.  
  1670.         public static Symbol FindSymbol(Module Module, string Name)
  1671.         {
  1672.             ModuleForSymbols = Module;
  1673.             UIntPtr A = Module.Process.Handle;
  1674.  
  1675.             SymInitialize(A, null, false);
  1676.  
  1677.             ulong B = SymLoadModuleEx(A, UIntPtr.Zero, Module.FileName, null, 0, 0, UIntPtr.Zero, 0);
  1678.  
  1679.             SymEnumSymbols(A, B, Name, new SymEnumSymbolsProc(SymEnumSymbolsProc2), UIntPtr.Zero);
  1680.             SymCleanup(A);
  1681.  
  1682.             return ReturnSymbol;
  1683.         }
  1684.  
  1685.         #endregion
  1686.  
  1687.         #region Thread
  1688.  
  1689.         public class Thread
  1690.         {
  1691.  
  1692.             private THREADENTRY32 _Information = new THREADENTRY32();
  1693.             private bool _Exists = false;
  1694.             private UIntPtr _Handle = UIntPtr.Zero;
  1695.  
  1696.             static Thread()
  1697.             {
  1698.                 return;
  1699.             }
  1700.  
  1701.             public Thread()
  1702.             {
  1703.                 _Information = new THREADENTRY32();
  1704.                 _Exists = (_Information.dwSize != 0);
  1705.  
  1706.                 return;
  1707.             }
  1708.  
  1709.             internal Thread(THREADENTRY32 Information)
  1710.             {
  1711.                 _Information = Information;
  1712.                 _Exists = (_Information.dwSize != 0);
  1713.  
  1714.                 return;
  1715.             }
  1716.  
  1717.             public bool Exists
  1718.             {
  1719.                 get
  1720.                 { return _Exists; }
  1721.             }
  1722.  
  1723.             public Process Process
  1724.             {
  1725.                 get { return FindProcess(_Information.th32OwnerProcessID); }
  1726.             }
  1727.  
  1728.             public Window[] Windows
  1729.             {
  1730.                 get { return EnumerateWindows(this); }
  1731.             }
  1732.  
  1733.             public Window MainWindow
  1734.             {
  1735.                 get { return GetMainWindow(this); }
  1736.             }
  1737.  
  1738.             public uint Id
  1739.             {
  1740.                 get { return _Information.th32ThreadID; }
  1741.             }
  1742.  
  1743.             public uint BasePriority
  1744.             {
  1745.                 get { return _Information.tpBasePri; }
  1746.             }
  1747.  
  1748.             public UIntPtr Handle
  1749.             {
  1750.                 get
  1751.                 {
  1752.                     if (_Handle != UIntPtr.Zero)
  1753.                         return _Handle;
  1754.  
  1755.                     else
  1756.                         return UIntPtr.Zero;
  1757.                 }
  1758.             }
  1759.  
  1760.             public void Open()
  1761.             {
  1762.                 Close();
  1763.                 _Handle = OpenThread(0x1FFFFF, false, Id);
  1764.                 return;
  1765.             }
  1766.  
  1767.             public void Close()
  1768.             {
  1769.                 if (_Handle != UIntPtr.Zero)
  1770.                 {
  1771.                     CloseHandle(Handle);
  1772.                     _Handle = UIntPtr.Zero;
  1773.                 }
  1774.  
  1775.                 return;
  1776.             }
  1777.  
  1778.             public UIntPtr StartAddress
  1779.             {
  1780.                 get
  1781.                 {
  1782.                     IntPtr A = Marshal.AllocHGlobal(IntPtr.Size);
  1783.  
  1784.                     try
  1785.                     {
  1786.                         NtQueryInformationThread(Handle, 9, A, (ulong)IntPtr.Size, (ulong)IntPtr.Zero);
  1787.  
  1788.                         return unchecked((UIntPtr)(ulong)(long)Marshal.ReadIntPtr(A));
  1789.                     }
  1790.                     finally
  1791.                     {
  1792.                         Marshal.FreeHGlobal(A);
  1793.                     }
  1794.                 }
  1795.             }
  1796.  
  1797.             public void Suspend()
  1798.             {
  1799.                 SuspendThread(Handle);
  1800.                 return;
  1801.             }
  1802.  
  1803.             public void Resume()
  1804.             {
  1805.                 ResumeThread(Handle);
  1806.                 return;
  1807.             }
  1808.  
  1809.             public void Terminate()
  1810.             {
  1811.                 TerminateThread(Handle, 0xffffffff);
  1812.                 return;
  1813.             }
  1814.  
  1815.         }
  1816.  
  1817.         public static Thread[] EnumerateThreads()
  1818.         {
  1819.             THREADENTRY32 A = new THREADENTRY32();
  1820.             List<Thread> B = new List<Thread>();
  1821.  
  1822.             A.dwSize = SizeOf<THREADENTRY32>();
  1823.  
  1824.             UIntPtr C = CreateToolhelp32Snapshot(0x4, 0);
  1825.  
  1826.             if (Thread32First(C, ref A))
  1827.             {
  1828.                 do
  1829.                 {
  1830.                     B.Add(new Thread(A));
  1831.                 } while (Thread32Next(C, ref A));
  1832.             }
  1833.  
  1834.             CloseHandle(C);
  1835.  
  1836.             return B.ToArray();
  1837.         }
  1838.  
  1839.         public static Thread[] EnumerateThreads(Process Process)
  1840.         {
  1841.             THREADENTRY32 A = new THREADENTRY32();
  1842.             List<Thread> B = new List<Thread>();
  1843.  
  1844.             A.dwSize = SizeOf<THREADENTRY32>();
  1845.  
  1846.             UIntPtr C = CreateToolhelp32Snapshot(0x4, 0);
  1847.  
  1848.             if (Thread32First(C, ref A))
  1849.             {
  1850.                 do
  1851.                 {
  1852.                     if (Process.Id == A.th32OwnerProcessID)
  1853.                         B.Add(new Thread(A));
  1854.                 } while (Thread32Next(C, ref A));
  1855.             }
  1856.  
  1857.             CloseHandle(C);
  1858.  
  1859.             return B.ToArray();
  1860.         }
  1861.  
  1862.         public static Thread[] FindThreads(UIntPtr StartAddress)
  1863.         {
  1864.             THREADENTRY32 A = new THREADENTRY32();
  1865.             List<Thread> B = new List<Thread>();
  1866.  
  1867.             A.dwSize = SizeOf<THREADENTRY32>();
  1868.  
  1869.             UIntPtr C = CreateToolhelp32Snapshot(0x4, 0);
  1870.  
  1871.             if (Thread32First(C, ref A))
  1872.             {
  1873.                 do
  1874.                 {
  1875.                     Thread D = new Thread(A);
  1876.                     D.Open();
  1877.                     bool E = (StartAddress == D.StartAddress);
  1878.                     D.Close();
  1879.  
  1880.                     if (E)
  1881.                     {
  1882.                         B.Add(D);
  1883.                     }
  1884.                 } while (Thread32Next(C, ref A));
  1885.             }
  1886.  
  1887.             CloseHandle(C);
  1888.  
  1889.             return B.ToArray();
  1890.         }
  1891.  
  1892.         public static Thread[] FindThreads(Process Process, UIntPtr StartAddress)
  1893.         {
  1894.             THREADENTRY32 A = new THREADENTRY32();
  1895.             List<Thread> B = new List<Thread>();
  1896.  
  1897.             A.dwSize = SizeOf<THREADENTRY32>();
  1898.  
  1899.             UIntPtr C = CreateToolhelp32Snapshot(0x4, 0);
  1900.  
  1901.             if (Thread32First(C, ref A))
  1902.             {
  1903.                 do
  1904.                 {
  1905.                     Thread D = new Thread(A);
  1906.                     D.Open();
  1907.                     bool E = (StartAddress == D.StartAddress);
  1908.                     D.Close();
  1909.  
  1910.                     if (E && Process.Id == A.th32OwnerProcessID)
  1911.                     {
  1912.                         B.Add(D);
  1913.                     }
  1914.                 } while (Thread32Next(C, ref A));
  1915.             }
  1916.  
  1917.             CloseHandle(C);
  1918.  
  1919.             return B.ToArray();
  1920.         }
  1921.  
  1922.         public static Thread FindThread(uint Id)
  1923.         {
  1924.             THREADENTRY32 A = new THREADENTRY32();
  1925.  
  1926.             A.dwSize = SizeOf<THREADENTRY32>();
  1927.  
  1928.             UIntPtr B = CreateToolhelp32Snapshot(0x4, 0);
  1929.  
  1930.             if (Thread32First(B, ref A))
  1931.             {
  1932.                 do
  1933.                 {
  1934.                     if (Id == A.th32ThreadID)
  1935.                     {
  1936.                         CloseHandle(B);
  1937.  
  1938.                         return new Thread(A);
  1939.                     }
  1940.                 } while (Thread32Next(B, ref A));
  1941.  
  1942.             }
  1943.  
  1944.             CloseHandle(B);
  1945.  
  1946.             return new Thread();
  1947.         }
  1948.  
  1949.         public static Thread FindThread(UIntPtr StartAddress)
  1950.         {
  1951.             THREADENTRY32 A = new THREADENTRY32();
  1952.  
  1953.             A.dwSize = SizeOf<THREADENTRY32>();
  1954.  
  1955.             UIntPtr B = CreateToolhelp32Snapshot(0x4, 0);
  1956.  
  1957.             if (Thread32First(B, ref A))
  1958.             {
  1959.                 do
  1960.                 {
  1961.                     Thread C = new Thread(A);
  1962.                     C.Open();
  1963.                     bool D = (StartAddress == C.StartAddress);
  1964.                     C.Close();
  1965.  
  1966.                     if (D)
  1967.                     {
  1968.                         CloseHandle(B);
  1969.  
  1970.                         return C;
  1971.                     }
  1972.                 } while (Thread32Next(B, ref A));
  1973.  
  1974.             }
  1975.  
  1976.             CloseHandle(B);
  1977.  
  1978.             return new Thread();
  1979.         }
  1980.  
  1981.         public static Thread FindThread(Process Process, UIntPtr StartAddress)
  1982.         {
  1983.             THREADENTRY32 A = new THREADENTRY32();
  1984.  
  1985.             A.dwSize = SizeOf<THREADENTRY32>();
  1986.  
  1987.             UIntPtr B = CreateToolhelp32Snapshot(0x4, 0);
  1988.  
  1989.             if (Thread32First(B, ref A))
  1990.             {
  1991.                 do
  1992.                 {
  1993.                     Thread C = new Thread(A);
  1994.                     C.Open();
  1995.                     bool D = (StartAddress == C.StartAddress);
  1996.                     C.Close();
  1997.  
  1998.                     if (D && Process.Id == A.th32OwnerProcessID)
  1999.                     {
  2000.                         CloseHandle(B);
  2001.  
  2002.                         return C;
  2003.                     }
  2004.                 } while (Thread32Next(B, ref A));
  2005.  
  2006.             }
  2007.  
  2008.             CloseHandle(B);
  2009.  
  2010.             return new Thread();
  2011.         }
  2012.  
  2013.         public static Thread GetCurrentThread()
  2014.         {
  2015.             return FindThread(GetCurrentThreadId());
  2016.         }
  2017.  
  2018.         public static Thread CreateThread(Process Process, UIntPtr StartAddress)
  2019.         {
  2020.             uint A;
  2021.             CreateRemoteThread(Process.Handle, UIntPtr.Zero, 0, StartAddress, UIntPtr.Zero, 0, out A);
  2022.             return FindThread(A);
  2023.         }
  2024.  
  2025.         public static Thread CreateThread(Process Process, UIntPtr StartAddress, byte[] Parameter)
  2026.         {
  2027.                     uint A;
  2028.                     UIntPtr C = Process.AllocateMemory((uint)Parameter.Length, MemoryProtectionConstants.PAGE_EXECUTE_READWRITE);
  2029.  
  2030.                     Process.WriteMemory(C, Parameter);
  2031.                     CreateRemoteThread(Process.Handle, UIntPtr.Zero, 0, StartAddress, C, 0, out A);
  2032.                     Process.FreeMemory(C);
  2033.  
  2034.                     return FindThread(A);
  2035.         }
  2036.  
  2037.         #endregion
  2038.  
  2039.         #region Window
  2040.  
  2041.         public class Window
  2042.         {
  2043.             UIntPtr _Handle = UIntPtr.Zero;
  2044.             private bool _Exists = false;
  2045.  
  2046.             static Window()
  2047.             {
  2048.                 return;
  2049.             }
  2050.  
  2051.             public Window()
  2052.             {
  2053.                 _Handle = UIntPtr.Zero;
  2054.                 _Exists = (_Handle != UIntPtr.Zero);
  2055.                 return;
  2056.             }
  2057.  
  2058.             internal Window(UIntPtr Handle)
  2059.             {
  2060.                 _Handle = Handle;
  2061.                 _Exists = (_Handle != UIntPtr.Zero);
  2062.                 return;
  2063.             }
  2064.  
  2065.             public bool Exists
  2066.             {
  2067.                 get { return _Exists; }
  2068.             }
  2069.  
  2070.             public UIntPtr Handle
  2071.             {
  2072.                 get { return _Handle; }
  2073.             }
  2074.  
  2075.             public string Text
  2076.             {
  2077.                 get
  2078.                 {
  2079.                     StringBuilder A = new StringBuilder(GetWindowTextLength(Handle));
  2080.                     GetWindowText(Handle, A, A.Capacity + 1);
  2081.                     return A.ToString();
  2082.                 }
  2083.                 set
  2084.                 {
  2085.                     SetWindowText(Handle, value);
  2086.                     return;
  2087.                 }
  2088.             }
  2089.  
  2090.             public Process Process
  2091.             {
  2092.                 get
  2093.                 {
  2094.                     uint A;
  2095.  
  2096.                     GetWindowThreadProcessId(Handle, out A);
  2097.  
  2098.                     return FindProcess(A);
  2099.                 }
  2100.             }
  2101.  
  2102.             public Thread Thread
  2103.             {
  2104.                 get
  2105.                 {
  2106.                     uint A;
  2107.                     return FindThread(GetWindowThreadProcessId(Handle, out A));
  2108.                 }
  2109.             }
  2110.  
  2111.             public int Left
  2112.             {
  2113.                 get
  2114.                 {
  2115.                     RECT A = new RECT();
  2116.  
  2117.                     GetWindowRect(Handle, out A);
  2118.  
  2119.                     return A.left;
  2120.                 }
  2121.                 set
  2122.                 {
  2123.                     SetWindowPos(Handle, UIntPtr.Zero, value, Top, 0, 0, 0x4 | 0x1);
  2124.                     return;
  2125.                 }
  2126.             }
  2127.  
  2128.             public int ClientLeft
  2129.             {
  2130.                 get
  2131.                 {
  2132.                     return Width - ClientWidth + Left;
  2133.                 }
  2134.             }
  2135.  
  2136.             public int Top
  2137.             {
  2138.                 get
  2139.                 {
  2140.                     RECT A = new RECT();
  2141.  
  2142.                     GetWindowRect(Handle, out A);
  2143.  
  2144.                     return A.top;
  2145.                 }
  2146.                 set
  2147.                 {
  2148.                     SetWindowPos(Handle, UIntPtr.Zero, Left, value, 0, 0, 0x4 | 0x1);
  2149.                     return;
  2150.                 }
  2151.             }
  2152.  
  2153.             public int ClientTop
  2154.             {
  2155.                 get
  2156.                 {
  2157.                     return Height - ClientHeight + Top;
  2158.                 }
  2159.             }
  2160.  
  2161.             public int Right
  2162.             {
  2163.                 get
  2164.                 {
  2165.                     RECT A = new RECT();
  2166.  
  2167.                     GetWindowRect(Handle, out A);
  2168.  
  2169.                     return A.right;
  2170.                 }
  2171.                 set
  2172.                 {
  2173.                     Width = value - Left;
  2174.                     return;
  2175.                 }
  2176.             }
  2177.  
  2178.             public int ClientRight
  2179.             {
  2180.                 get
  2181.                 {
  2182.                     return ClientLeft + ClientWidth;
  2183.                 }
  2184.             }
  2185.  
  2186.             public int Bottom
  2187.             {
  2188.                 get
  2189.                 {
  2190.                     RECT A = new RECT();
  2191.  
  2192.                     GetWindowRect(Handle, out A);
  2193.  
  2194.                     return A.bottom;
  2195.                 }
  2196.                 set
  2197.                 {
  2198.                     Height = value - Top;
  2199.                     return;
  2200.                 }
  2201.             }
  2202.  
  2203.             public int ClientBottom
  2204.             {
  2205.                 get
  2206.                 {
  2207.                     return ClientTop + ClientHeight;
  2208.                 }
  2209.             }
  2210.  
  2211.             public int Width
  2212.             {
  2213.                 get
  2214.                 {
  2215.                     return Right - Left;
  2216.                 }
  2217.                 set
  2218.                 {
  2219.                     SetWindowPos(Handle, UIntPtr.Zero, 0, 0, value, Bottom - Top, 0x4 | 0x2);
  2220.                     return;
  2221.                 }
  2222.             }
  2223.  
  2224.             public int ClientWidth
  2225.             {
  2226.                 get
  2227.                 {
  2228.                     RECT A = new RECT();
  2229.  
  2230.                     GetClientRect(Handle, out A);
  2231.  
  2232.                     return A.right;
  2233.                 }
  2234.                 /*
  2235.                 set
  2236.                 {
  2237.                     SetWindowPos(Handle, UIntPtr.Zero, 0, 0, Right - Left, Bottom - Top, 0x4 | 0x2);
  2238.                     return;
  2239.                 }
  2240.                 */
  2241.             }
  2242.  
  2243.             public int Height
  2244.             {
  2245.                 get
  2246.                 {
  2247.                     return Bottom - Top;
  2248.                 }
  2249.                 set
  2250.                 {
  2251.                     SetWindowPos(Handle, UIntPtr.Zero, 0, 0, Width, value, 0x4 | 0x2);
  2252.                     return;
  2253.                 }
  2254.             }
  2255.  
  2256.             public int ClientHeight
  2257.             {
  2258.                 get
  2259.                 {
  2260.                     RECT A = new RECT();
  2261.  
  2262.                     GetClientRect(Handle, out A);
  2263.  
  2264.                     return A.bottom;
  2265.                 }
  2266.                 /*
  2267.                 set
  2268.                 {
  2269.                     SetWindowPos(Handle, UIntPtr.Zero, 0, 0, Right - Left, Bottom - Top, 0x4 | 0x2);
  2270.                     return;
  2271.                 }
  2272.                 */
  2273.             }
  2274.  
  2275.             public void Terminate()
  2276.             {
  2277.                 DestroyWindow(Handle);
  2278.                 return;
  2279.             }
  2280.  
  2281.             public void Show()
  2282.             {
  2283.                 ShowWindow(Handle, 5);
  2284.                 return;
  2285.             }
  2286.  
  2287.             public void Hide()
  2288.             {
  2289.                 ShowWindow(Handle, 0);
  2290.                 return;
  2291.             }
  2292.  
  2293.             public void Minimize()
  2294.             {
  2295.                 ShowWindow(Handle, 6);
  2296.                 return;
  2297.             }
  2298.  
  2299.             public void Maximize()
  2300.             {
  2301.                 ShowWindow(Handle, 3);
  2302.                 return;
  2303.             }
  2304.  
  2305.         }
  2306.  
  2307.         private static List<Window> EnumeratedWindows = new List<Window>();
  2308.  
  2309.         private static UIntPtr WindowFromHandle = UIntPtr.Zero;
  2310.  
  2311.         private static string WindowFromText = string.Empty;
  2312.  
  2313.         private static Process WindowFromProcess = new Process();
  2314.  
  2315.         private static Thread WindowFromThread = new Thread();
  2316.  
  2317.         private static Window WindowToReturn = new Window();
  2318.  
  2319.         private static bool EnumWindowsProc1(UIntPtr hwnd, UIntPtr lParam)
  2320.         {
  2321.             EnumeratedWindows.Add(new Window(hwnd));
  2322.             return true;
  2323.         }
  2324.  
  2325.         private static bool EnumWindowsProc2(UIntPtr hwnd, UIntPtr lParam)
  2326.         {
  2327.             if(WindowFromHandle == hwnd)
  2328.             {
  2329.                 WindowToReturn = new Window(hwnd);
  2330.                 return false;
  2331.             }
  2332.  
  2333.             return true;
  2334.         }
  2335.  
  2336.         private static bool EnumWindowsProc3(UIntPtr hwnd, UIntPtr lParam)
  2337.         {
  2338.             Window A = new Window(hwnd);
  2339.  
  2340.             if (WindowFromText == A.Text)
  2341.             {
  2342.                 WindowToReturn = A;
  2343.                 return false;
  2344.             }
  2345.  
  2346.             return true;
  2347.         }
  2348.  
  2349.         private static bool EnumWindowsProc4(UIntPtr hwnd, UIntPtr lParam)
  2350.         {
  2351.             Window A = new Window(hwnd);
  2352.  
  2353.             if (WindowFromProcess.Id == A.Process.Id && WindowFromText == A.Text)
  2354.             {
  2355.                 WindowToReturn = A;
  2356.                 return false;
  2357.             }
  2358.  
  2359.             return true;
  2360.         }
  2361.  
  2362.         private static bool EnumWindowsProc5(UIntPtr hwnd, UIntPtr lParam)
  2363.         {
  2364.             Window A = new Window(hwnd);
  2365.  
  2366.             if (WindowFromThread.Id == A.Thread.Id && WindowFromText == A.Text)
  2367.             {
  2368.                 WindowToReturn = A;
  2369.                 return false;
  2370.             }
  2371.  
  2372.             return true;
  2373.         }
  2374.  
  2375.         private static bool EnumWindowsProc6(UIntPtr hwnd, UIntPtr lParam)
  2376.         {
  2377.             Window A = new Window(hwnd);
  2378.  
  2379.             if (WindowFromProcess.Id == A.Process.Id)
  2380.             {
  2381.                 EnumeratedWindows.Add(new Window(hwnd));
  2382.                 return true;
  2383.             }
  2384.  
  2385.             return true;
  2386.         }
  2387.  
  2388.         private static bool EnumWindowsProc7(UIntPtr hwnd, UIntPtr lParam)
  2389.         {
  2390.             Window A = new Window(hwnd);
  2391.  
  2392.             if (WindowFromThread.Id == A.Thread.Id)
  2393.             {
  2394.                 EnumeratedWindows.Add(new Window(hwnd));
  2395.                 return true;
  2396.             }
  2397.  
  2398.             return true;
  2399.         }
  2400.  
  2401.         private static bool EnumWindowsProc8(UIntPtr hwnd, UIntPtr lParam)
  2402.         {
  2403.             Window A = new Window(hwnd);
  2404.  
  2405.             if (WindowFromText == A.Text)
  2406.             {
  2407.                 EnumeratedWindows.Add(new Window(hwnd));
  2408.                 return true;
  2409.             }
  2410.  
  2411.             return true;
  2412.         }
  2413.  
  2414.         private static bool EnumWindowsProc9(UIntPtr hwnd, UIntPtr lParam)
  2415.         {
  2416.             Window A = new Window(hwnd);
  2417.  
  2418.             if (WindowFromProcess.Id == A.Process.Id && WindowFromText == A.Text)
  2419.             {
  2420.                 EnumeratedWindows.Add(new Window(hwnd));
  2421.                 return true;
  2422.             }
  2423.  
  2424.             return true;
  2425.         }
  2426.  
  2427.         private static bool EnumWindowsProc10(UIntPtr hwnd, UIntPtr lParam)
  2428.         {
  2429.             Window A = new Window(hwnd);
  2430.  
  2431.             if (WindowFromThread.Id == A.Thread.Id && WindowFromText == A.Text)
  2432.             {
  2433.                 EnumeratedWindows.Add(new Window(hwnd));
  2434.                 return true;
  2435.             }
  2436.  
  2437.             return true;
  2438.         }
  2439.  
  2440.         public static Window[] EnumerateWindows()
  2441.         {
  2442.             EnumeratedWindows.Clear();
  2443.             EnumWindows(new EnumWindowsProc(EnumWindowsProc1), UIntPtr.Zero);
  2444.  
  2445.             return EnumeratedWindows.ToArray();
  2446.         }
  2447.  
  2448.         public static Window[] EnumerateWindows(Process Process)
  2449.         {
  2450.             EnumeratedWindows.Clear();
  2451.             WindowFromProcess = Process;
  2452.             EnumWindows(new EnumWindowsProc(EnumWindowsProc6), UIntPtr.Zero);
  2453.  
  2454.             return EnumeratedWindows.ToArray();
  2455.         }
  2456.  
  2457.         public static Window[] EnumerateWindows(Thread Thread)
  2458.         {
  2459.             EnumeratedWindows.Clear();
  2460.             WindowFromThread = Thread;
  2461.             EnumWindows(new EnumWindowsProc(EnumWindowsProc7), UIntPtr.Zero);
  2462.  
  2463.             return EnumeratedWindows.ToArray();
  2464.         }
  2465.  
  2466.         public static Window FindWindow(UIntPtr Handle)
  2467.         {
  2468.             WindowToReturn = new Window();
  2469.             WindowFromHandle = Handle;
  2470.             EnumWindows(new EnumWindowsProc(EnumWindowsProc2), UIntPtr.Zero);
  2471.  
  2472.             return WindowToReturn;
  2473.         }
  2474.  
  2475.         public static Window FindWindow(string Text)
  2476.         {
  2477.             WindowToReturn = new Window();
  2478.             WindowFromText = Text;
  2479.             EnumWindows(new EnumWindowsProc(EnumWindowsProc3), UIntPtr.Zero);
  2480.  
  2481.             return WindowToReturn;
  2482.         }
  2483.  
  2484.         public static Window[] FindWindows(string Text)
  2485.         {
  2486.             EnumeratedWindows.Clear();
  2487.             WindowFromText = Text;
  2488.             EnumWindows(new EnumWindowsProc(EnumWindowsProc8), UIntPtr.Zero);
  2489.  
  2490.             return EnumeratedWindows.ToArray();
  2491.         }
  2492.  
  2493.         public static Window[] FindWindows(Process Process, string Text)
  2494.         {
  2495.             EnumeratedWindows.Clear();
  2496.             WindowFromProcess = Process;
  2497.             WindowFromText = Text;
  2498.             EnumWindows(new EnumWindowsProc(EnumWindowsProc9), UIntPtr.Zero);
  2499.  
  2500.             return EnumeratedWindows.ToArray();
  2501.         }
  2502.  
  2503.         public static Window[] FindWindows(Thread Thread, string Text)
  2504.         {
  2505.             EnumeratedWindows.Clear();
  2506.             WindowFromThread = Thread;
  2507.             WindowFromText = Text;
  2508.             EnumWindows(new EnumWindowsProc(EnumWindowsProc9), UIntPtr.Zero);
  2509.  
  2510.             return EnumeratedWindows.ToArray();
  2511.         }
  2512.  
  2513.         public static Window FindWindow(Process Process, string Text)
  2514.         {
  2515.             WindowToReturn = new Window();
  2516.             WindowFromProcess = Process;
  2517.             WindowFromText = Text;
  2518.             EnumWindows(new EnumWindowsProc(EnumWindowsProc4), UIntPtr.Zero);
  2519.  
  2520.             return WindowToReturn;
  2521.         }
  2522.  
  2523.         public static Window FindWindow(Thread Thread, string Text)
  2524.         {
  2525.             WindowToReturn = new Window();
  2526.             WindowFromThread = Thread;
  2527.             WindowFromText = Text;
  2528.             EnumWindows(new EnumWindowsProc(EnumWindowsProc5), UIntPtr.Zero);
  2529.  
  2530.             return WindowToReturn;
  2531.         }
  2532.  
  2533.         public static Window GetMainWindow(Process Process)
  2534.         {
  2535.             foreach(Window A in Process.Windows)
  2536.             {
  2537.                 if (GetWindow(A.Handle, 4) == (UIntPtr)0 && IsWindowVisible(A.Handle))
  2538.                     return A;
  2539.             }
  2540.  
  2541.             return new Window();
  2542.         }
  2543.  
  2544.         public static Window GetMainWindow(Thread Thread)
  2545.         {
  2546.             foreach (Window A in Thread.Windows)
  2547.             {
  2548.                 if (GetWindow(A.Handle, 4) == (UIntPtr)0 && IsWindowVisible(A.Handle))
  2549.                     return A;
  2550.             }
  2551.  
  2552.             return new Window();
  2553.         }
  2554.  
  2555.         public static Window GetCurrentWindow()
  2556.         {
  2557.             return new Window(GetForegroundWindow());
  2558.         }
  2559.  
  2560.         #endregion
  2561.  
  2562.     }
  2563.  
  2564. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement