/* Implementation */ private void ProcessWatcherOnOnNewProcess(Process newProcess) { try { string fullPath = newProcess.GetExecutablePath(); var config = _mainPageViewModel.Applications.FirstOrDefault(x => string.Equals(x.Config.AppLocation, fullPath, StringComparison.OrdinalIgnoreCase)); if (config != null && config.Config.AutoInject) { var appInjector = new ApplicationInjector(newProcess); appInjector.Inject(); } } catch (Exception) { // Ignored } }
/// <summary> /// Starts the application, injecting Reloaded into it. /// </summary> public void Start() { // Start up the process Native.STARTUPINFO startupInfo = new Native.STARTUPINFO(); Native.SECURITY_ATTRIBUTES lpProcessAttributes = new Native.SECURITY_ATTRIBUTES(); Native.SECURITY_ATTRIBUTES lpThreadAttributes = new Native.SECURITY_ATTRIBUTES(); Native.PROCESS_INFORMATION processInformation = new Native.PROCESS_INFORMATION(); if (_arguments == null) { _arguments = ""; } bool success = Native.CreateProcessW(null, $"\"{_location}\" {_arguments}", ref lpProcessAttributes, ref lpThreadAttributes, false, Native.ProcessCreationFlags.CREATE_SUSPENDED, IntPtr.Zero, Path.GetDirectoryName(_location), ref startupInfo, ref processInformation); if (!success) { string windowsErrorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; throw new ArgumentException($"{Errors.FailedToStartProcess()} {windowsErrorMessage}"); } // DLL Injection var process = Process.GetProcessById((int)processInformation.dwProcessId); var injector = new ApplicationInjector(process); try { injector.Inject(); } catch (Exception) { Native.ResumeThread(processInformation.hThread); throw; } Native.ResumeThread(processInformation.hThread); }