C#
public static bool Inject(Process targetProcess)
{
IntPtr hProcess = OpenProcess(0x1F0FFF, false, targetProcess.Id); //The handler to the game process, with all access rights
if (hProcess == IntPtr.Zero) return false;
byte[] dllPathBytes = Encoding.UTF8.GetBytes(patcherPath + "\0"); //Convert the path to SpeedPatcher.dll to a byte array
IntPtr allocMem = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)dllPathBytes.Length, 0x3000, 0x40); //Allocate memory in the game
if (allocMem == IntPtr.Zero) return false; //If we cannot allocate memory in the game process, return false
if (!WriteProcessMemory(hProcess, allocMem, dllPathBytes, (uint)dllPathBytes.Length, out _)) return false; //Write the path to SpeedPatcher.dll into the allocated memory in the game process, if this fails, return false
IntPtr loadLibAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA"); //Get the address of the LoadLibraryA function in kernel32.dll, which we will call in the remote thread
IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLibAddr, allocMem, 0, IntPtr.Zero); //Create a remote thread in the game process that calls LoadLibraryA with the path to SpeedPatcher.dll
return hThread != IntPtr.Zero; //If the remote thread is created successfully, return true, otherwise return false
}
C#
public static bool Inject(Process targetProcess)
{
IntPtr hProcess = OpenProcess(0x1F0FFF, false, targetProcess.Id); //The handler to the game process, with all access rights
if (hProcess == IntPtr.Zero) return false;
byte[] dllPathBytes = Encoding.UTF8.GetBytes(patcherPath + "\0"); //Convert the path to SpeedPatcher.dll to a byte array
IntPtr allocMem = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)dllPathBytes.Length, 0x3000, 0x40); //Allocate memory in the game
if (allocMem == IntPtr.Zero) return false; //If we cannot allocate memory in the game process, return false
if (!WriteProcessMemory(hProcess, allocMem, dllPathBytes, (uint)dllPathBytes.Length, out _)) return false; //Write the path to SpeedPatcher.dll into the allocated memory in the game process, if this fails, return false
IntPtr loadLibAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA"); //Get the address of the LoadLibraryA function in kernel32.dll, which we will call in the remote thread
IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLibAddr, allocMem, 0, IntPtr.Zero); //Create a remote thread in the game process that calls LoadLibraryA with the path to SpeedPatcher.dll
return hThread != IntPtr.Zero; //If the remote thread is created successfully, return true, otherwise return false
}