private static int Main(string[] args) { if (Environment.GetEnvironmentVariable("ARGJSON") != null && Environment.GetEnvironmentVariable("ARGJSON").Length >= 2) args = JsonConvert.DeserializeObject<string[]>(Environment.GetEnvironmentVariable("ARGJSON")); if (args.Length < 2) { Console.Error.WriteLine("Launcher was run with insufficient arguments. Usage: launcher.exe <app directory> <start command>"); return 1; } PROCESS_INFORMATION processInformation; var startupInformation = new STARTUPINFO(); var workingDirectory = Path.Combine(Directory.GetCurrentDirectory(), args[0]); var executablePath = workingDirectory + @"\" + args[1]; if (String.IsNullOrWhiteSpace(args[1])) { Console.Error.WriteLine("Could not determine a start command. Use the -c flag to 'cf push' to specify a custom start command."); return 1; } Console.Out.WriteLine("Running {0}", executablePath); var result = CreateProcess(null, executablePath, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, workingDirectory, ref startupInformation, out processInformation); if (!result) { return Marshal.GetLastWin32Error(); } WaitForSingleObject(processInformation.hProcess, INFINITE); UInt32 exitCode = 0; GetExitCodeProcess(processInformation.hProcess, ref exitCode); return (int) exitCode; }
private void Debugger(object value) { _continue = true; var parameters = value as object[]; var filename = parameters[0] as string; var arguments = parameters[1] as string; if (!string.IsNullOrEmpty(arguments) && !arguments.StartsWith(" ")) arguments = string.Concat(" ", arguments); var si = new STARTUPINFO(); PROCESS_INFORMATION pi; //Create Process if (!CreateProcess(null, string.Format("\"{0}\"{1}", filename, arguments), IntPtr.Zero, IntPtr.Zero, true, ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS | ProcessCreationFlags.DEBUG_PROCESS, IntPtr.Zero, null, ref si, out pi)) throw new Exception(string.Format("Failed to start {0}.", Path.GetFileName(filename))); //if (!CreateProcess(null, string.Format("\"{0}\"{1}", filename, arguments), // IntPtr.Zero, IntPtr.Zero, false, // ProcessCreationFlags.NORMAL_PRIORITY_CLASS, // IntPtr.Zero, null, ref si, out pi)) // throw new Exception(string.Format("Failed to start {0}.", Path.GetFileName(filename))); //Open Process Handle _handle = OpenProcess(ProcessAccessPriviledges.PROCESS_VM_READ | ProcessAccessPriviledges.PROCESS_TERMINATE | ProcessAccessPriviledges.PROCESS_VM_WRITE | ProcessAccessPriviledges.PROCESS_VM_OPERATION, false, pi.dwProcessId); //Create debug event var debugEvent = new DEBUG_EVENT() { dwProcessId = pi.dwProcessId, dwThreadId = pi.dwThreadId }; try { do { //DebugDetector.AssertCheckRunning(); if (WaitForDebugEvent(ref debugEvent, 100)) { switch (debugEvent.dwDebugEventCode) { //Continue case DebugEventTypes.EXCEPTION_DEBUG_EVENT: case DebugEventTypes.CREATE_THREAD_DEBUG_EVENT: case DebugEventTypes.LOAD_DLL_DEBUG_EVENT: case DebugEventTypes.UNLOAD_DLL_DEBUG_EVENT: case DebugEventTypes.OUTPUT_DEBUG_STRING_EVENT: case DebugEventTypes.EXIT_THREAD_DEBUG_EVENT: break; case DebugEventTypes.CREATE_PROCESS_DEBUG_EVENT: _fileHandle = debugEvent.u.CreateProcessInfo.hFile; _processId = debugEvent.dwProcessId; _processStarted = true; break; //Exit the loop case DebugEventTypes.EXIT_PROCESS_DEBUG_EVENT: case DebugEventTypes.RIP_EVENT: default: _continue = false; break; } } if (_signalEnd && _processStarted) { Process process; if (TryGetAttachedProcess(out process) == true) process.CloseMainWindow(); } ContinueDebugEvent(debugEvent.dwProcessId, debugEvent.dwThreadId, DebugStates.DBG_CONTINUE); } while (_continue); } finally { End(); TaskHandler.RunTask(delegate(object input) { if (OnExiting != null) OnExiting(this, EventArgs.Empty); }); } }
static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);