public static void KillSporeProcesses() { try { List <Process> sporeProcesses = GetSporeProcesses(); for (int i = 0; i < sporeProcesses.Count; i++) { sporeProcesses[0].Kill(); } } catch (Exception ex) { MessageDisplay.ShowMessageBox(Settings.GetLanguageString(3, "KillSporeFailed") + "\n\n" + ex.GetType() + ": " + ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace); } }
private const uint MAXWAIT = 15000; //10000; public static void InjectDLL(PROCESS_INFORMATION pi, string dllPath) { Console.WriteLine("Injecting: " + dllPath); IntPtr retLib = NativeMethods.GetProcAddress(NativeMethods.GetModuleHandle("kernel32.dll"), "LoadLibraryA"); if (retLib == IntPtr.Zero) { throw new InjectException("LoadLibrary unreachable."); } /*IntPtr hProc; * if (Program.processHandle == IntPtr.Zero)*/ IntPtr hProc = NativeMethods.OpenProcess(NativeMethods.AccessRequired, false, pi.dwProcessId); //Open the process with all access if (hProc != IntPtr.Zero) {// Allocate memory to hold the path to the DLL file in the process' memory IntPtr objPtr = NativeMethods.VirtualAllocEx(hProc, IntPtr.Zero, (uint)dllPath.Length + 1, AllocationType.Commit, MemoryProtection.ReadWrite); if (objPtr == IntPtr.Zero) { int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); MessageDisplay.ShowMessageBox("Error: Virtual alloc failure: \n" + lastError.ToString() + "\n" + "hProc: " + hProc.ToString() + "\nProgram.processHandle: " + (SporeLauncher._processHandle == IntPtr.Zero)); throw new System.ComponentModel.Win32Exception(lastError); //throw new InjectException("Virtual alloc failure."); } //Write the path to the DLL file in the location just created var bytes = new byte[dllPath.Length + 1]; for (int i = 0; i < dllPath.Length; i++) { bytes[i] = (byte)dllPath[i]; } bytes[dllPath.Length] = 0; UIntPtr numBytesWritten; MessageDisplay.DebugShowMessageBox("Beginning WriteProcessMemory"); bool writeProcessMemoryOutput = NativeMethods.WriteProcessMemory(hProc, objPtr, bytes, (uint)bytes.Length, out numBytesWritten); if (!writeProcessMemoryOutput || numBytesWritten.ToUInt32() != bytes.Length) { SporeLauncher.ThrowWin32Exception("Write process memory failed."); } MessageDisplay.DebugShowMessageBox("WriteProcessMemory output: " + writeProcessMemoryOutput.ToString()); // Create a remote thread that begins at the LoadLibrary function and is passed as memory pointer IntPtr lpThreadId; IntPtr hRemoteThread = NativeMethods.CreateRemoteThread(hProc, IntPtr.Zero, 0, retLib, objPtr, 0, out lpThreadId); // Wait for the thread to finish if (hRemoteThread != IntPtr.Zero) { if (NativeMethods.WaitForSingleObject(hRemoteThread, MAXWAIT) == WAIT_TIMEOUT) { //throw new InjectException("Wait for single object failed."); SporeLauncher.ThrowWin32Exception("Wait for single object failed. This usually occurs if something has become stuck during injection, or if another error was left open for too long."); } } else { //throw new InjectException("Create remote thread failed."); int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); MessageDisplay.ShowMessageBox("Error: Create remote thread failed: \n" + lastError.ToString()); throw new System.ComponentModel.Win32Exception(lastError); } NativeMethods.VirtualFreeEx(hProc, objPtr, (uint)bytes.Length, AllocationType.Release); NativeMethods.CloseHandle(hProc); } else { SporeLauncher.ThrowWin32Exception("OpenProcess failed!"); } }
static void CreateSporeProcess() { var sb = new StringBuilder(); if ((!Environment.GetCommandLineArgs().Contains(UpdaterService.IgnoreUpdatesArg)) && (Environment.GetCommandLineArgs().Length > 1) && (Environment.GetCommandLineArgs()[1] == Settings.LaunchSporeWithoutManagerOptions)) { int i = 0; foreach (string arg in Environment.GetCommandLineArgs()) { if ((i != 0) && (arg.ToLowerInvariant() != Settings.LaunchSporeWithoutManagerOptions.ToLowerInvariant())) { sb.Append(arg); sb.Append(" "); } i++; } } else { if (Settings.ForceGameWindowingMode) { if (Settings.ForceWindowedMode == 1) { sb.Append("-f"); } else// if (Settings.ForceWindowedMode == 0) { sb.Append("-w"); } sb.Append(" "); string size = "-r:"; var monitor = NativeMethods.AllMonitors[0]; if (Settings.ForceWindowedMode == 2) { size += (monitor.rcMonitor.Right - monitor.rcMonitor.Left).ToString() + "x" + (monitor.rcMonitor.Bottom - monitor.rcMonitor.Top).ToString(); } else if (Settings.ForceGameWindowBounds) { //MessageDisplay.DebugShowMessageBox("MONITOR: " + monitor.rcMonitor.Left + ", " + monitor.rcMonitor.Top + ", " + monitor.rcMonitor.Right + ", " + monitor.rcMonitor.Bottom + "\n" + +monitor.rcWork.Left + ", " + monitor.rcWork.Top + ", " + monitor.rcWork.Right + ", " + monitor.rcWork.Bottom); if (Settings.AutoGameWindowBounds) { MessageDisplay.DebugShowMessageBox("Settings.AutoGameWindowBounds is true"); if (Settings.ForceGameWindowingMode) { MessageDisplay.DebugShowMessageBox("Settings.ForceGameWindowingMode is true, Settings.ForceWindowedMode is " + Settings.ForceWindowedMode); if (Settings.ForceWindowedMode == 0) { size += (monitor.rcWork.Right - monitor.rcWork.Left); } else// if (Settings.ForceWindowedMode == 1) { size += (monitor.rcMonitor.Right - monitor.rcMonitor.Left); } /*else * size += Settings.ForcedGameWindowWidth;*/ } else { size += Settings.ForcedGameWindowWidth; } } else { size += Settings.ForcedGameWindowWidth; } size += "x"; if (Settings.AutoGameWindowBounds) { if (Settings.ForceGameWindowingMode) { int maximizedTitlebarHeight = CaptionHeight; if (Settings.ForceWindowedMode == 0) { size += ((monitor.rcWork.Bottom - monitor.rcWork.Top) - maximizedTitlebarHeight); } else if (Settings.ForceWindowedMode == 1) { size += ((monitor.rcMonitor.Bottom - monitor.rcMonitor.Top) - maximizedTitlebarHeight); } else { size += Settings.ForcedGameWindowHeight; } } else { size += Settings.ForcedGameWindowHeight; } } else { size += Settings.ForcedGameWindowHeight; } } else { MessageDisplay.DebugShowMessageBox("Settings.ForceGameWindowBounds is false!"); } sb.Append(size); sb.Append(" "); } if (Settings.ForceGameLocale && (!string.IsNullOrWhiteSpace(Settings.ForcedGameLocale))) { string option = "-locale:"; if (Settings.ForcedGameLocale.StartsWith("-")) { option += Settings.ForcedGameLocale.Substring(1); } else { option += Settings.ForcedGameLocale; } sb.Append(option); sb.Append(" "); } if (Settings.UseCustomGameState && (!string.IsNullOrWhiteSpace(Settings.GameState))) { sb.Append("-state:" + Settings.GameState); sb.Append(" "); } if (!string.IsNullOrWhiteSpace(Settings.CommandLineOptions)) { string[] additionalOptions; if (Settings.CommandLineOptions.Contains(" ")) { additionalOptions = Settings.CommandLineOptions.Split(" ".ToCharArray()); } else { additionalOptions = new string[] { Settings.CommandLineOptions } }; foreach (string arg in additionalOptions) { sb.Append(arg); sb.Append(" "); } } } /*string currentSporebinPath = string.Empty; * if (LauncherSettings.ForcedSporebinEP1Path != null) * currentSporebinPath = LauncherSettings.ForcedSporebinEP1Path; * else * currentSporebinPath = this.SporebinPath;*/ MessageDisplay.DebugShowMessageBox("SporebinPath: " + GameInfo.SporebinEP1 + "\nExecutablePath: " + _executablePath + "\nCommand Line Options: " + GetGameCommandLineOptions()); if (!NativeMethods.CreateProcess(null, "\"" + _executablePath + "\" " + sb.ToString(), IntPtr.Zero, IntPtr.Zero, false, ProcessCreationFlags.CREATE_SUSPENDED, IntPtr.Zero, GameInfo.SporebinEP1, ref _startupInfo, out _processInfo)) { //throw new InjectException(Strings.ProcessNotStarted); int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); MessageDisplay.ShowMessageBox(Settings.GetLanguageString(3, "SporeProcessCreationFailed") + lastError.ToString()); //Strings.ProcessNotStarted); throw new System.ComponentModel.Win32Exception(lastError); } }