/// <summary> /// Suspends all of the threads of the current game except for the thread /// that is currently running (i.e. of the calling mod). /// Note: If you run this from a hook, the game thread you are executing this on /// will not be suspended, you should manually in your program. /// </summary> public static void SuspendAllThreads(this ReloadedProcess reloadedProcess) { // Get current thread (do not affect self) int currentThreadId = (int)GetCurrentThreadId(); // Ignore self and suspend all other threads. if (reloadedProcess.Is64Bit) { // For each thread, Ignore self and suspend all other threads. foreach (RemoteThread <ThreadContext64> processThread in reloadedProcess.GetRemoteThreads <ThreadContext64>()) { if (processThread.Id != currentThreadId) { processThread.Suspend(); } } } else { // For each thread, Ignore self and suspend all other threads. foreach (RemoteThread <ThreadContext32> processThread in reloadedProcess.GetRemoteThreads <ThreadContext32>()) { if (processThread.Id != currentThreadId) { processThread.Suspend(); } } } }
/// <summary> /// Creates an instance of ReloadedProcess from a supplied process name. /// </summary> /// <param name="processName">The process name to find obtain Reloaded process from.</param> public static ReloadedProcess GetProcessByName(string processName) { try { // Create new ReloadedProcess ReloadedProcess reloadedProcess = new ReloadedProcess(); // Get Process by Name var process = SystemProcess.GetProcessesByName(processName)[0]; // Set Process ID reloadedProcess.ProcessId = (IntPtr)process.Id; // Get Process Handle reloadedProcess.ProcessHandle = Native.Native.OpenProcess(Native.Native.PROCESS_ALL_ACCESS, false, (int)reloadedProcess.ProcessId); // Set thread id and handle to be that of first thread. reloadedProcess.ThreadId = (IntPtr)process.Threads[0].Id; // Set thread handle to be that of the first thread. reloadedProcess.ThreadHandle = Native.Native.OpenThread(Native.Native.THREAD_ALL_ACCESS, false, (int)reloadedProcess.ThreadId); // Retrun Reloaded Process return(reloadedProcess); } catch { return(null); } }
/// <summary> /// Initializes the DLL Injector of Reloaded Mod Loader. /// Once the DLL Injector is initialted, DLL injection may be performed by calling /// InjectDLL(); /// </summary> /// <param name="process"> /// The process object that is to be used. /// The handle can be obtained by Process.Handle or found via various other means. /// </param> public DllInjector(ReloadedProcess process) { // Set the Location and Handle to the Process to be Injected. Process = process; // This should automatically resolve to kernel32.dll as it is already registered by Windows. // The handle should return from already loaded library in memory, following the standard search strategy. Kernel32Handle = Native.Native.LoadLibraryW("kernel32"); // Retrieves the address to the LoadLibraryW function. // We will later call LoadLibraryW inside the target process using CreateRemoteThread, // which will load our own wanted DLL (game modification) inside of the target process. LoadLibraryAddress = Native.Native.GetProcAddress(Kernel32Handle, "LoadLibraryW"); }
/// <summary> /// CallLibrary /// Calls a function at a specified address with a singular parameter at the /// specified pointer within the virtual address space of a target process. /// Generally used to call singular Windows API functions such as /// LoadLibraryA inside a target process while not executing in the same process. /// </summary> /// <param name="process">The process object of the game, Process.GetCurrentProcess() if injected into the game.</param> /// <param name="address">Address to the starting point of the library, module or library method to be executed.</param> /// <param name="parameteraddress">Address of a singular parameter for the method to be called.</param> /// <returns>An exit code.</returns> public static int CallLibrary(this ReloadedProcess process, IntPtr address, IntPtr parameteraddress) { // Create and initialize a thread at our address (which may be a C/C++ function) and parameter address. // hThread is a handle to the new thread. IntPtr hThread = Native.Native.CreateRemoteThread(process.ProcessHandle, IntPtr.Zero, 0, address, parameteraddress, 0, out IntPtr threadId); // Wait for the thread to finish. Native.Native.WaitForSingleObject(hThread, unchecked ((uint)-1)); // Store and retrieve the exit code for the thread. Native.Native.GetExitCodeThread(hThread, out uint exitCode); // Return the exit code. return((int)exitCode); }
/// <summary> /// Resumes all of the threads of the current game except for the thread /// that is currently running (i.e. of the calling mod). /// </summary> public static void ResumeAllThreads(this ReloadedProcess reloadedProcess) { if (reloadedProcess.Is64Bit) { foreach (RemoteThread <ThreadContext64> processThread in reloadedProcess.GetRemoteThreads <ThreadContext64>()) { processThread.Resume(); } } else { foreach (RemoteThread <ThreadContext32> processThread in reloadedProcess.GetRemoteThreads <ThreadContext32>()) { processThread.Resume(); } } }
/// <summary> /// Suspends all of the threads of the current game except for the thread /// that is currently running (i.e. of the calling mod). /// Note: If you run this from a hook, the game thread you are executing this on /// will not be suspended, you should manually in your program. /// </summary> public static void SuspendAllThreads(this ReloadedProcess reloadedProcess) { // Get Process from Current Process System.Diagnostics.Process gameProcess = reloadedProcess.GetProcessFromReloadedProcess(); // Get current thread (do not affect self) int currentThreadId = Thread.CurrentThread.ManagedThreadId; // For each thread. foreach (ProcessThread processThread in gameProcess.Threads) { // Ignore self if (processThread.Id != currentThreadId) { // Get thread handle IntPtr suspendThreadHandle = Native.Native.OpenThread(Native.Native.THREAD_ALL_ACCESS, false, processThread.Id); // Suspend Thread SuspendThread(suspendThreadHandle); } } }
/// <summary> /// See <see cref="CallLibrary"/>, it is the asynchronous version of CallLibrary that does not wait for the thread to finish. /// </summary> /// <param name="process">The process object of the game, Process.GetCurrentProcess() if injected into the game.</param> /// <param name="address">Address to the starting point of the library, module or library method to be executed.</param> /// <param name="parameteraddress">Address of parameters for the method to be called.</param> /// <returns>An exit code</returns> public static void CallLibraryAsync(this ReloadedProcess process, IntPtr address, IntPtr parameteraddress) { // Create and initialize a thread at our address (which may be a C/C++ function) and parameter address. // hThread is a handle to the new thread. Native.Native.CreateRemoteThread(process.ProcessHandle, IntPtr.Zero, 0, address, parameteraddress, 0, out IntPtr threadId); }
/// <summary> /// Initializes the alternate DLL Injector of Reloaded Mod Loader. /// Once the DLL Injector is initialted, DLL injection may be performed by calling /// InjectDLL(); /// </summary> /// <param name="process"> /// The process object that is to be used. /// The handle can be obtained by Process.Handle or found via various other means. /// </param> public AlternateDllInjector(ReloadedProcess process) { // Set the Location and Handle to the Process to be Injected. Process = process; }
/// <summary> /// Suspends the first/main/primary thread assigned to the Reloaded Process object. /// Note: It's not recommended to run this from a hook, first thread might be your program's current thread. /// </summary> public static void SuspendFirstThread(this ReloadedProcess reloadedProcess) { SuspendThread(reloadedProcess.ThreadHandle); }
/// <summary> /// Resumes the first/main/primary thread assigned to the Reloaded Process object. /// Note: It's not recommended to run this from a hook, first thread might be your program's current thread. /// </summary> public static void ResumeFirstThread(this ReloadedProcess reloadedProcess) { ResumeThread(reloadedProcess.ThreadHandle); }
/// <summary> /// Kills the process behind the individual Reloaded Process instance. /// </summary> public static void KillProcess(this ReloadedProcess reloadedProcess) { System.Diagnostics.Process localReloadedProcess = reloadedProcess.GetProcessFromReloadedProcess(); localReloadedProcess.Kill(); }