Exemplo n.º 1
0
            public static IntPtr OpenAppendFile(string fileName)
            {
                if (fileName == null)
                {
                    throw new ArgumentNullException("fileName");
                }
                Win32.SecurityAttributes securityAttributes = new Win32.SecurityAttributes();
                securityAttributes.Initialize();
                IntPtr handle = Win32.CreateFile(fileName,
                                                 Win32.FileAccess.AppendData,
                                                 Win32.FileShare.Read,
                                                 ref securityAttributes,
                                                 Win32.FileCreateDisposition.OpenAlways,
                                                 Win32.FileAttributes.Normal, IntPtr.Zero);

                if (handle == IntPtr.Zero)
                {
                    int lastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(lastError,
                                             string.Format(CouldNotOpenFileErrorFormat, fileName, lastError));
                }
                return(handle);
            }
Exemplo n.º 2
0
            void StartProcess()
            {
                IntPtr stdin       = IntPtr.Zero;
                IntPtr stdoutRead  = IntPtr.Zero;
                IntPtr stdoutWrite = IntPtr.Zero;
                IntPtr stderr      = IntPtr.Zero;

                try
                {
                    Win32.StartupInfo startupInfo = new Win32.StartupInfo();
                    startupInfo.Initialize();
                    startupInfo.Flags = Win32.StartProcessFlags.UseStdHandles;
                    #region Set stdin
                    {
                        if (RedirectedStandardInput != null)
                        {
                            stdin = RedirectedStandardInput.ReleaseStandardOutputReadHandle();
                            Helper.EnsureHandleInheritable(stdin);
                        }
                        else
                        {
                            /* Duplicate the handle so that
                             * the cleaning logic is unified.
                             */
                            stdin = Helper.DuplicateHandleForInheritance(
                                Win32.GetStdHandle(Win32.StandardHandleId.StandardInput));
                        }
                        startupInfo.StandardInput = stdin;
                    }
                    #endregion Set stdin
                    #region Set stdout
                    {
                        Win32.SecurityAttributes pipeAttributes = new Win32.SecurityAttributes();
                        pipeAttributes.Initialize();
                        if (Win32.CreatePipe(out stdoutRead, out stdoutWrite,
                                             ref pipeAttributes, 4096) == Win32.Bool.False)
                        {
                            stdoutRead  = IntPtr.Zero;
                            stdoutWrite = IntPtr.Zero;
                            int lastError = Marshal.GetLastWin32Error();
                            throw new Win32Exception(lastError,
                                                     string.Format(CouldNotCreatePipeErrorFormat, lastError));
                        }
                        Helper.EnsureHandleInheritable(stdoutWrite);
                        startupInfo.StandardOutput = stdoutWrite;
                    }
                    #endregion Set stdout
                    #region Set stderr
                    {
                        if (RedirectedStandardError != null)
                        {
                            stderr = standardErrorAppend
                                ? Helper.OpenAppendFile(RedirectedStandardError)
                                : Helper.OpenTruncatedFile(RedirectedStandardError);
                            Helper.EnsureHandleInheritable(stderr);
                        }
                        else
                        {
                            stderr = Helper.DuplicateHandleForInheritance(
                                Win32.GetStdHandle(Win32.StandardHandleId.StandardError));
                        }
                        startupInfo.StandardError = stderr;
                    }
                    #endregion Set stderr
                    Win32.ProcessInformation processInformation;

                    /* Create the process suspended,
                     * and resume it only after we
                     * have started waiting for it.
                     */
                    if (Win32.CreateProcess(null,
                                            BuildCommandLine(),
                                            IntPtr.Zero, IntPtr.Zero,
                                            Win32.Bool.True, Win32.ProcessCreationFlags.CreateSuspended, IntPtr.Zero,
                                            InitialWorkingDirectory,
                                            ref startupInfo, out processInformation)
                        == Win32.Bool.False)
                    {
                        int lastError = Marshal.GetLastWin32Error();
                        throw new Win32Exception(lastError,
                                                 string.Format(CouldNotCreateProcessErrorFormat, lastError));
                    }
                    hasExited     = false;
                    processHandle = processInformation.HandleToProcess;
                    ProcessId     = processInformation.ProcessId;
                    new Thread(WaitForProcessExitWorker).Start(this);
                    Win32.ResumeThread(processInformation.HandleToThread);
                    Win32.CloseHandle(processInformation.HandleToThread);
                    stdoutReadHandle = stdoutRead;

                    /* Prevent this very handle from being
                     * closed. Other handles are useless now
                     * and will be closed in "finally".
                     */
                    stdoutRead = IntPtr.Zero;
                }
                finally
                {
                    if (stdin != IntPtr.Zero)
                    {
                        Win32.CloseHandle(stdin);
                    }
                    if (stdoutRead != IntPtr.Zero)
                    {
                        Win32.CloseHandle(stdoutRead);
                    }
                    if (stdoutWrite != IntPtr.Zero)
                    {
                        Win32.CloseHandle(stdoutWrite);
                    }
                    if (stderr != IntPtr.Zero)
                    {
                        Win32.CloseHandle(stderr);
                    }
                }
            }