private void StartProcess()
        {
            bool retValue;

            // Create startup info for new console process.
            STARTUPINFO startupInfo = new STARTUPINFO();

            startupInfo.cb = Marshal.SizeOf(startupInfo);
            startupInfo.dwFlags = StartFlags.STARTF_USESHOWWINDOW;
            startupInfo.wShowWindow = _consoleVisible ? WindowShowStyle.Show : WindowShowStyle.Hide;
            startupInfo.lpTitle = this.ConsoleTitle ?? "Console";

            SECURITY_ATTRIBUTES procAttrs = new SECURITY_ATTRIBUTES();
            SECURITY_ATTRIBUTES threadAttrs = new SECURITY_ATTRIBUTES();

            procAttrs.nLength = Marshal.SizeOf(procAttrs);
            threadAttrs.nLength = Marshal.SizeOf(threadAttrs);

            // Set environment variables for new process.
            IntPtr pEnvironment = IntPtr.Zero;

            // Start new console process.
            retValue = WinApi.CreateProcess(null, this.CommandLine, ref procAttrs, ref threadAttrs, false,
                CreationFlags.CREATE_NEW_CONSOLE | CreationFlags.CREATE_SUSPENDED, pEnvironment, null,
                ref startupInfo, out _procInfo);
            if (!retValue) throw new Win32Exception(Marshal.GetLastWin32Error(),
                "Unable to create new console process.");

            _proc = Process.GetProcessById(_procInfo.dwProcessId);

            // Create objects in shared memory.
            CreateSharedObjects(_procInfo.dwProcessId);

            // Write startup parameters to shared memory.
            unsafe
            {
                ConsoleParams* consoleParams = (ConsoleParams*)_consoleParams.Get();

                consoleParams->ConsoleMainThreadId = _procInfo.dwThreadId;
                consoleParams->ParentProcessId = Process.GetCurrentProcess().Id;
                consoleParams->NotificationTimeout = 10;
                consoleParams->RefreshInterval = 100;
                consoleParams->Rows = this.ConsoleInitialWindowHeight;
                consoleParams->Columns = this.ConsoleInitialWindowWidth;
                consoleParams->BufferRows = this.ConsoleInitialBufferHeight;
                consoleParams->BufferColumns = this.ConsoleInitialBufferWidth;
            }
        }
Exemple #2
0
 public static extern bool CreateProcess(string lpApplicationName,
    string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
    ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles,
    CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
    [In] ref STARTUPINFO lpStartupInfo,
    out PROCESS_INFORMATION lpProcessInformation);
 public static extern bool CreateProcess(string lpApplicationName,
                                         string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
                                         ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles,
                                         CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
                                         [In] ref STARTUPINFO lpStartupInfo,
                                         out PROCESS_INFORMATION lpProcessInformation);