static unsafe public Process CreateProcess(NDebugger debugger, string filename, string workingDirectory, string arguments) { debugger.TraceMessage("Executing " + filename + " " + arguments); uint[] processStartupInfo = new uint[17]; processStartupInfo[0] = sizeof(uint) * 17; uint[] processInfo = new uint[4]; ICorDebugProcess outProcess; if (string.IsNullOrEmpty(workingDirectory)) { workingDirectory = System.IO.Path.GetDirectoryName(filename); } _SECURITY_ATTRIBUTES secAttr = new _SECURITY_ATTRIBUTES(); secAttr.bInheritHandle = 0; secAttr.lpSecurityDescriptor = IntPtr.Zero; secAttr.nLength = (uint)sizeof(_SECURITY_ATTRIBUTES); fixed(uint *pprocessStartupInfo = processStartupInfo) { fixed(uint *pprocessInfo = processInfo) { outProcess = debugger.CorDebug.CreateProcess( filename, // lpApplicationName // If we do not prepend " ", the first argument migh just get lost " " + arguments, // lpCommandLine ref secAttr, // lpProcessAttributes ref secAttr, // lpThreadAttributes 1, //TRUE // bInheritHandles 0x00000010 /*CREATE_NEW_CONSOLE*/, // dwCreationFlags IntPtr.Zero, // lpEnvironment workingDirectory, // lpCurrentDirectory (uint)pprocessStartupInfo, // lpStartupInfo (uint)pprocessInfo, // lpProcessInformation, CorDebugCreateProcessFlags.DEBUG_NO_SPECIAL_OPTIONS // debuggingFlags ); } } return(new Process(debugger, outProcess, filename, workingDirectory)); }
static WindowsAPI() { NullAttribute = Marshal.AllocHGlobal(SECURITY_DESCRIPTOR_MIN_LENGTH); if (NullAttribute == IntPtr.Zero) { return; } try { if (!InitializeSecurityDescriptor(NullAttribute, SECURITY_DESCRIPTOR_REVISION)) { throw GenuineExceptions.Get_Windows_SharedMemoryError(Marshal.GetLastWin32Error()); } if (!SetSecurityDescriptorDacl(NullAttribute, true, IntPtr.Zero, false)) { throw GenuineExceptions.Get_Windows_SharedMemoryError(Marshal.GetLastWin32Error()); } AttributesWithNullDACL = new _SECURITY_ATTRIBUTES(); AttributesWithNullDACL.nLength = 12; AttributesWithNullDACL.lpSecurityDescriptor = NullAttribute; AttributesWithNullDACL.bInheritHandle = 1; } catch (Exception ex) { FailureReason = ex; try { Marshal.FreeHGlobal(NullAttribute); } catch (Exception) { } NullAttribute = IntPtr.Zero; } }
static void Main(string[] args) { if(4 != IntPtr.Size) { string bitserr = "Expected to be 32-bit process, not " + (IntPtr.Size * 8).ToString() + "-bit"; Console.WriteLine("Error: {0}", bitserr); Console.Error.WriteLine("{0}", bitserr); Environment.Exit(110); } Console.WriteLine("MR.Debug"); int iarg = 0; if (args.Length > iarg && "-xdebug" == args[iarg]) { iarg++; IsXDebug = true; IsVerbose = true; } if (args.Length > iarg && "-verbose" == args[iarg]) { iarg++; IsVerbose = true; } string ClientProcessName; if (args.Length > iarg) { ClientProcessName = args[iarg++]; } else { throw new ArgumentException("Expected client process name"); } string ClientProcessArgs = ""; if (args.Length > iarg) { ClientProcessArgs = args[iarg++]; } if (IsVerbose) { Console.WriteLine(" Debugging: \"{0}\" \"{1}\"", ClientProcessName, ClientProcessArgs); } DebugProcess dbgproc = new DebugProcess(); dbgproc.idbg = CreateDebuggingInterfaceFromVersion(3 /* 2.0 */, ClientProcessVersion); if (null == dbgproc.idbg) { throw new NotSupportedException("null debugger"); } dbgproc.idbg.Initialize(); dbgproc.idbg.SetManagedHandler(new DebugCallback(dbgproc)); if (IsVerbose) { Console.WriteLine(" Initialized debugging interface"); } _SECURITY_ATTRIBUTES nosecattribs = new _SECURITY_ATTRIBUTES(); nosecattribs.bInheritHandle = 0; nosecattribs.lpSecurityDescriptor = IntPtr.Zero; nosecattribs.nLength = (uint)Marshal.SizeOf(typeof(_SECURITY_ATTRIBUTES)); //PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); IntPtr ppi = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PROCESS_INFORMATION))); STARTUPINFO si = new STARTUPINFO(); si.cb = Marshal.SizeOf(typeof(STARTUPINFO)); si.dwFlags = 0x00000100; // STARTF_USESTDHANDLES si.hStdInput = new IntPtr(0); si.hStdOutput = GetStdHandle(-11); // STD_OUTPUT_HANDLE si.hStdError = GetStdHandle(-12); // STD_ERROR_HANDLE IntPtr psi = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(STARTUPINFO))); Marshal.StructureToPtr(si, psi, false); lock (dbgproc) { dbgproc.idbg.CreateProcess(ClientProcessName, "\"" + ClientProcessName + "\" " + ClientProcessArgs, ref nosecattribs, ref nosecattribs, 1, 0, IntPtr.Zero, Environment.CurrentDirectory, (uint)psi.ToInt32(), (uint)ppi.ToInt32(), CorDebugCreateProcessFlags.DEBUG_NO_SPECIAL_OPTIONS, out dbgproc.idbgproc); } if (IsVerbose) { Console.WriteLine(" Started debug process"); } System.Threading.Thread runthread = new System.Threading.Thread( new System.Threading.ThreadStart( delegate() { try { dbgproc.Run(); } catch (Exception e) { string runthderr = "Run thread error: " + e.Message; Console.WriteLine("Error: {0}", runthderr); Console.Error.WriteLine("{0}", runthderr); Environment.Exit(111); } })); runthread.Start(); while (!dbgproc.FinishedInitializing) { System.Threading.Thread.Sleep(100); } for (; ; ) { string line = Console.ReadLine(); if (null == line) { line = "quit"; } dbgproc.AddCommand(line); if (dbgproc.ProcessExit) { break; } } dbgproc.idbg.Terminate(); #if DEBUG Console.WriteLine("DEBUG: exit"); #endif }
int ICorDebug.CreateProcess(string lpApplicationName, string lpCommandLine, _SECURITY_ATTRIBUTES lpProcessAttributes, _SECURITY_ATTRIBUTES lpThreadAttributes, int bInheritHandles, uint dwCreationFlags, System.IntPtr lpEnvironment, string lpCurrentDirectory, _STARTUPINFO lpStartupInfo, _PROCESS_INFORMATION lpProcessInformation, CorDebugCreateProcessFlags debuggingFlags, out ICorDebugProcess ppProcess) { ppProcess = null; return(COM_HResults.E_NOTIMPL); }
static internal extern IntPtr CreateEvent(_SECURITY_ATTRIBUTES lpEventAttributes, int bManualReset, int bInitialState, string lpName);
static internal extern IntPtr CreateFileMapping(IntPtr hFile, _SECURITY_ATTRIBUTES lpSecurityAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);
static private extern IntPtr CreateMutex(_SECURITY_ATTRIBUTES lpMutexAttributes, bool bInitialOwner, string lpName);
private static extern IntPtr CreateMutex(_SECURITY_ATTRIBUTES lpMutexAttributes, bool bInitialOwner, string lpName);
internal static extern IntPtr CreateFileMapping(IntPtr hFile, _SECURITY_ATTRIBUTES lpSecurityAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);
internal static extern IntPtr CreateEvent(_SECURITY_ATTRIBUTES lpEventAttributes, int bManualReset, int bInitialState, string lpName);
static WindowsAPI() { NullAttribute = Marshal.AllocHGlobal(SECURITY_DESCRIPTOR_MIN_LENGTH); if (NullAttribute == IntPtr.Zero) return ; try { if (! InitializeSecurityDescriptor(NullAttribute, SECURITY_DESCRIPTOR_REVISION)) throw GenuineExceptions.Get_Windows_SharedMemoryError(Marshal.GetLastWin32Error()); if (! SetSecurityDescriptorDacl(NullAttribute, true, IntPtr.Zero, false)) throw GenuineExceptions.Get_Windows_SharedMemoryError(Marshal.GetLastWin32Error()); AttributesWithNullDACL = new _SECURITY_ATTRIBUTES(); AttributesWithNullDACL.nLength = 12; AttributesWithNullDACL.lpSecurityDescriptor = NullAttribute; AttributesWithNullDACL.bInheritHandle = 1; } catch(Exception ex) { FailureReason = ex; try { Marshal.FreeHGlobal(NullAttribute); } catch(Exception) { } NullAttribute = IntPtr.Zero; } }