Ejemplo n.º 1
0
        private bool StartWithCreateProcess(ProcessStartInfo startInfo)
        {
            if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput)
            {
                throw new InvalidOperationException("StandardOutputEncodingNotAllowed");
            }
            if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError)
            {
                throw new InvalidOperationException("StandardErrorEncodingNotAllowed");
            }
            StringBuilder stringBuilder = Process2.BuildCommandLine(startInfo.FileName, startInfo.Arguments);

            NativeMethods.STARTUPINFO             sTARTUPINFO         = new NativeMethods.STARTUPINFO();
            SafeNativeMethods.PROCESS_INFORMATION pROCESS_INFORMATION = new SafeNativeMethods.PROCESS_INFORMATION();
            SafeProcessHandle safeProcessHandle = new SafeProcessHandle();
            SafeThreadHandle  safeThreadHandle  = new SafeThreadHandle();
            int            num      = 0;
            SafeFileHandle handle   = null;
            SafeFileHandle handle2  = null;
            SafeFileHandle handle3  = null;
            GCHandle       gCHandle = default(GCHandle);

            lock (Process2.s_CreateProcessLock)
            {
                try
                {
                    if (startInfo.RedirectStandardInput || startInfo.RedirectStandardOutput || startInfo.RedirectStandardError)
                    {
                        if (startInfo.RedirectStandardInput)
                        {
                            CreatePipe(out handle, out sTARTUPINFO.hStdInput, true);
                        }
                        else
                        {
                            sTARTUPINFO.hStdInput = new SafeFileHandle(NativeMethods.GetStdHandle(-10), false);
                        }
                        if (startInfo.RedirectStandardOutput)
                        {
                            CreatePipe(out handle2, out sTARTUPINFO.hStdOutput, false);
                        }
                        else
                        {
                            sTARTUPINFO.hStdOutput = new SafeFileHandle(NativeMethods.GetStdHandle(-11), false);
                        }
                        if (startInfo.RedirectStandardError)
                        {
                            CreatePipe(out handle3, out sTARTUPINFO.hStdError, false);
                        }
                        else
                        {
                            sTARTUPINFO.hStdError = sTARTUPINFO.hStdOutput;
                        }
                        sTARTUPINFO.dwFlags = 256;
                    }
                    int num2 = 0;
                    if (startInfo.CreateNoWindow)
                    {
                        num2 |= 134217728;
                    }
                    IntPtr intPtr = (IntPtr)0;
                    //if (startInfo.environmentVariables != null)
                    //{
                    //    bool unicode = false;
                    //    if (ProcessManager.IsNt)
                    //    {
                    //        num2 |= 1024;
                    //        unicode = true;
                    //    }
                    //    byte[] value = EnvironmentBlock.ToByteArray(startInfo.environmentVariables, unicode);
                    //    gCHandle = GCHandle.Alloc(value, GCHandleType.Pinned);
                    //    intPtr = gCHandle.AddrOfPinnedObject();
                    //}
                    string text = startInfo.WorkingDirectory;
                    if (text == string.Empty)
                    {
                        text = Environment.CurrentDirectory;
                    }
                    bool flag2;
                    //if (startInfo.UserName.Length != 0)
                    //{
                    //    NativeMethods.LogonFlags logonFlags = (NativeMethods.LogonFlags)0;
                    //    if (startInfo.LoadUserProfile)
                    //    {
                    //        logonFlags = NativeMethods.LogonFlags.LOGON_WITH_PROFILE;
                    //    }
                    //    IntPtr intPtr2 = IntPtr.Zero;
                    //    try
                    //    {
                    //        if (startInfo.Password == null)
                    //        {
                    //            intPtr2 = Marshal.StringToCoTaskMemUni(string.Empty);
                    //        }
                    //        else
                    //        {
                    //            intPtr2 = Marshal.SecureStringToCoTaskMemUnicode(startInfo.Password);
                    //        }
                    //        RuntimeHelpers.PrepareConstrainedRegions();
                    //        try
                    //        {
                    //        }
                    //        finally
                    //        {
                    //            flag2 = NativeMethods.CreateProcessWithLogonW(startInfo.UserName, startInfo.Domain, intPtr2, logonFlags, null, stringBuilder, num2, intPtr, text, sTARTUPINFO, pROCESS_INFORMATION);
                    //            if (!flag2)
                    //            {
                    //                num = Marshal.GetLastWin32Error();
                    //            }
                    //            if (pROCESS_INFORMATION.hProcess != (IntPtr)0 && pROCESS_INFORMATION.hProcess != NativeMethods.INVALID_HANDLE_VALUE)
                    //            {
                    //                safeProcessHandle.InitialSetHandle(pROCESS_INFORMATION.hProcess);
                    //            }
                    //            if (pROCESS_INFORMATION.hThread != (IntPtr)0 && pROCESS_INFORMATION.hThread != NativeMethods.INVALID_HANDLE_VALUE)
                    //            {
                    //                safeThreadHandle.InitialSetHandle(pROCESS_INFORMATION.hThread);
                    //            }
                    //        }
                    //        if (flag2)
                    //        {
                    //            goto IL_3B9;
                    //        }
                    //        if (num == 193 || num == 216)
                    //        {
                    //            throw new Win32Exception(num, SR.GetString("InvalidApplication"));
                    //        }
                    //        throw new Win32Exception(num);
                    //    }
                    //    finally
                    //    {
                    //        if (intPtr2 != IntPtr.Zero)
                    //        {
                    //            Marshal.ZeroFreeCoTaskMemUnicode(intPtr2);
                    //        }
                    //    }
                    //}
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                    }
                    finally
                    {
                        flag2 = NativeMethods.CreateProcess(null, stringBuilder, null, null, true, num2, intPtr, text, sTARTUPINFO, pROCESS_INFORMATION);
                        if (!flag2)
                        {
                            num = Marshal.GetLastWin32Error();
                        }
                        if (pROCESS_INFORMATION.hProcess != (IntPtr)0 && pROCESS_INFORMATION.hProcess != NativeMethods.INVALID_HANDLE_VALUE)
                        {
                            safeProcessHandle.InitialSetHandle(pROCESS_INFORMATION.hProcess);
                        }
                        if (pROCESS_INFORMATION.hThread != (IntPtr)0 && pROCESS_INFORMATION.hThread != NativeMethods.INVALID_HANDLE_VALUE)
                        {
                            safeThreadHandle.InitialSetHandle(pROCESS_INFORMATION.hThread);
                        }
                    }
                    if (!flag2)
                    {
                        if (num == 193 || num == 216)
                        {
                            throw new Win32Exception(num, "InvalidApplication");
                        }
                        throw new Win32Exception(num);
                    }
                }
                finally
                {
                    if (gCHandle.IsAllocated)
                    {
                        gCHandle.Free();
                    }
                    sTARTUPINFO.Dispose();
                }
            }
            if (startInfo.RedirectStandardInput)
            {
                standardInput           = new StreamWriter(new FileStream(handle, FileAccess.Write, 4096, false), Console.InputEncoding, 4096);
                standardInput.AutoFlush = true;
            }
            if (startInfo.RedirectStandardOutput)
            {
                Encoding encoding = (startInfo.StandardOutputEncoding != null) ? startInfo.StandardOutputEncoding : Console.OutputEncoding;
                standardOutput = new StreamReader(new FileStream(handle2, FileAccess.Read, 4096, false), encoding, true, 4096);
            }
            if (startInfo.RedirectStandardError)
            {
                Encoding encoding2 = (startInfo.StandardErrorEncoding != null) ? startInfo.StandardErrorEncoding : Console.OutputEncoding;
                standardError = new StreamReader(new FileStream(handle3, FileAccess.Read, 4096, false), encoding2, true, 4096);
            }
            bool result = false;

            if (!safeProcessHandle.IsInvalid)
            {
                SetProcessHandle(safeProcessHandle);
                SetProcessId(pROCESS_INFORMATION.dwProcessId);
                safeThreadHandle.Close();
                result = true;
            }
            return(result);
        }
Ejemplo n.º 2
0
 private bool StartWithCreateProcess(ProcessStartInfo startInfo)
 {
     if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput)
     {
         throw new InvalidOperationException("StandardOutputEncodingNotAllowed");
     }
     if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError)
     {
         throw new InvalidOperationException("StandardErrorEncodingNotAllowed");
     }
     StringBuilder stringBuilder = Process2.BuildCommandLine(startInfo.FileName, startInfo.Arguments);
     NativeMethods.STARTUPINFO sTARTUPINFO = new NativeMethods.STARTUPINFO();
     SafeNativeMethods.PROCESS_INFORMATION pROCESS_INFORMATION = new SafeNativeMethods.PROCESS_INFORMATION();
     SafeProcessHandle safeProcessHandle = new SafeProcessHandle();
     SafeThreadHandle safeThreadHandle = new SafeThreadHandle();
     int num = 0;
     SafeFileHandle handle = null;
     SafeFileHandle handle2 = null;
     SafeFileHandle handle3 = null;
     GCHandle gCHandle = default(GCHandle);
     lock (Process2.s_CreateProcessLock)
     {
         try
         {
             if (startInfo.RedirectStandardInput || startInfo.RedirectStandardOutput || startInfo.RedirectStandardError)
             {
                 if (startInfo.RedirectStandardInput)
                 {
                     CreatePipe(out handle, out sTARTUPINFO.hStdInput, true);
                 }
                 else
                 {
                     sTARTUPINFO.hStdInput = new SafeFileHandle(NativeMethods.GetStdHandle(-10), false);
                 }
                 if (startInfo.RedirectStandardOutput)
                 {
                     CreatePipe(out handle2, out sTARTUPINFO.hStdOutput, false);
                 }
                 else
                 {
                     sTARTUPINFO.hStdOutput = new SafeFileHandle(NativeMethods.GetStdHandle(-11), false);
                 }
                 if (startInfo.RedirectStandardError)
                 {
                     CreatePipe(out handle3, out sTARTUPINFO.hStdError, false);
                 }
                 else
                 {
                     sTARTUPINFO.hStdError = sTARTUPINFO.hStdOutput;
                 }
                 sTARTUPINFO.dwFlags = 256;
             }
             int num2 = 0;
             if (startInfo.CreateNoWindow)
             {
                 num2 |= 134217728;
             }
             IntPtr intPtr = (IntPtr)0;
             //if (startInfo.environmentVariables != null)
             //{
             //    bool unicode = false;
             //    if (ProcessManager.IsNt)
             //    {
             //        num2 |= 1024;
             //        unicode = true;
             //    }
             //    byte[] value = EnvironmentBlock.ToByteArray(startInfo.environmentVariables, unicode);
             //    gCHandle = GCHandle.Alloc(value, GCHandleType.Pinned);
             //    intPtr = gCHandle.AddrOfPinnedObject();
             //}
             string text = startInfo.WorkingDirectory;
             if (text == string.Empty)
             {
                 text = Environment.CurrentDirectory;
             }
             bool flag2;
             //if (startInfo.UserName.Length != 0)
             //{
             //    NativeMethods.LogonFlags logonFlags = (NativeMethods.LogonFlags)0;
             //    if (startInfo.LoadUserProfile)
             //    {
             //        logonFlags = NativeMethods.LogonFlags.LOGON_WITH_PROFILE;
             //    }
             //    IntPtr intPtr2 = IntPtr.Zero;
             //    try
             //    {
             //        if (startInfo.Password == null)
             //        {
             //            intPtr2 = Marshal.StringToCoTaskMemUni(string.Empty);
             //        }
             //        else
             //        {
             //            intPtr2 = Marshal.SecureStringToCoTaskMemUnicode(startInfo.Password);
             //        }
             //        RuntimeHelpers.PrepareConstrainedRegions();
             //        try
             //        {
             //        }
             //        finally
             //        {
             //            flag2 = NativeMethods.CreateProcessWithLogonW(startInfo.UserName, startInfo.Domain, intPtr2, logonFlags, null, stringBuilder, num2, intPtr, text, sTARTUPINFO, pROCESS_INFORMATION);
             //            if (!flag2)
             //            {
             //                num = Marshal.GetLastWin32Error();
             //            }
             //            if (pROCESS_INFORMATION.hProcess != (IntPtr)0 && pROCESS_INFORMATION.hProcess != NativeMethods.INVALID_HANDLE_VALUE)
             //            {
             //                safeProcessHandle.InitialSetHandle(pROCESS_INFORMATION.hProcess);
             //            }
             //            if (pROCESS_INFORMATION.hThread != (IntPtr)0 && pROCESS_INFORMATION.hThread != NativeMethods.INVALID_HANDLE_VALUE)
             //            {
             //                safeThreadHandle.InitialSetHandle(pROCESS_INFORMATION.hThread);
             //            }
             //        }
             //        if (flag2)
             //        {
             //            goto IL_3B9;
             //        }
             //        if (num == 193 || num == 216)
             //        {
             //            throw new Win32Exception(num, SR.GetString("InvalidApplication"));
             //        }
             //        throw new Win32Exception(num);
             //    }
             //    finally
             //    {
             //        if (intPtr2 != IntPtr.Zero)
             //        {
             //            Marshal.ZeroFreeCoTaskMemUnicode(intPtr2);
             //        }
             //    }
             //}
             RuntimeHelpers.PrepareConstrainedRegions();
             try
             {
             }
             finally
             {
                 flag2 = NativeMethods.CreateProcess(null, stringBuilder, null, null, true, num2, intPtr, text, sTARTUPINFO, pROCESS_INFORMATION);
                 if (!flag2)
                 {
                     num = Marshal.GetLastWin32Error();
                 }
                 if (pROCESS_INFORMATION.hProcess != (IntPtr)0 && pROCESS_INFORMATION.hProcess != NativeMethods.INVALID_HANDLE_VALUE)
                 {
                     safeProcessHandle.InitialSetHandle(pROCESS_INFORMATION.hProcess);
                 }
                 if (pROCESS_INFORMATION.hThread != (IntPtr)0 && pROCESS_INFORMATION.hThread != NativeMethods.INVALID_HANDLE_VALUE)
                 {
                     safeThreadHandle.InitialSetHandle(pROCESS_INFORMATION.hThread);
                 }
             }
             if (!flag2)
             {
                 if (num == 193 || num == 216)
                 {
                     throw new Win32Exception(num, "InvalidApplication");
                 }
                 throw new Win32Exception(num);
             }
         }
         finally
         {
             if (gCHandle.IsAllocated)
             {
                 gCHandle.Free();
             }
             sTARTUPINFO.Dispose();
         }
     }
     if (startInfo.RedirectStandardInput)
     {
         standardInput = new StreamWriter(new FileStream(handle, FileAccess.Write, 4096, false), Console.InputEncoding, 4096);
         standardInput.AutoFlush = true;
     }
     if (startInfo.RedirectStandardOutput)
     {
         Encoding encoding = (startInfo.StandardOutputEncoding != null) ? startInfo.StandardOutputEncoding : Console.OutputEncoding;
         standardOutput = new StreamReader(new FileStream(handle2, FileAccess.Read, 4096, false), encoding, true, 4096);
     }
     if (startInfo.RedirectStandardError)
     {
         Encoding encoding2 = (startInfo.StandardErrorEncoding != null) ? startInfo.StandardErrorEncoding : Console.OutputEncoding;
         standardError = new StreamReader(new FileStream(handle3, FileAccess.Read, 4096, false), encoding2, true, 4096);
     }
     bool result = false;
     if (!safeProcessHandle.IsInvalid)
     {
         SetProcessHandle(safeProcessHandle);
         SetProcessId(pROCESS_INFORMATION.dwProcessId);
         safeThreadHandle.Close();
         result = true;
     }
     return result;
 }