Exemple #1
0
 internal static extern bool CreateProcessWithLogonW(string userName, string domain, IntPtr password, ProcessNativeMethods.LogonFlags logonFlags, string appName, StringBuilder cmdLine, int creationFlags, IntPtr environmentBlock, string lpCurrentDirectory, ProcessNativeMethods.STARTUPINFO lpStartupInfo, SafeNativeMethods.PROCESS_INFORMATION lpProcessInformation);
        private int StartWithCreateProcess(ProcessStartInfo startinfo)
        {
            string str;
            bool   flag;

            ProcessNativeMethods.STARTUPINFO      sTARTUPINFO        = new ProcessNativeMethods.STARTUPINFO();
            SafeNativeMethods.PROCESS_INFORMATION pROCESSINFORMATION = new SafeNativeMethods.PROCESS_INFORMATION();
            int           lastWin32Error = 0;
            GCHandle      gCHandle       = new GCHandle();
            StringBuilder stringBuilder  = StartProcessCommand.BuildCommandLine(startinfo.FileName, startinfo.Arguments);

            try
            {
                if (this._redirectstandardinput == null)
                {
                    sTARTUPINFO.hStdInput = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-10), false);
                }
                else
                {
                    startinfo.RedirectStandardInput = true;
                    this._redirectstandardinput     = this.ResolveFilePath(this._redirectstandardinput);
                    sTARTUPINFO.hStdInput           = this.GetSafeFileHandleForRedirection(this._redirectstandardinput, ProcessNativeMethods.OPEN_EXISTING);
                }
                if (this._redirectstandardoutput == null)
                {
                    sTARTUPINFO.hStdOutput = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-11), false);
                }
                else
                {
                    startinfo.RedirectStandardOutput = true;
                    this._redirectstandardoutput     = this.ResolveFilePath(this._redirectstandardoutput);
                    sTARTUPINFO.hStdOutput           = this.GetSafeFileHandleForRedirection(this._redirectstandardoutput, ProcessNativeMethods.CREATE_ALWAYS);
                }
                if (this._redirectstandarderror == null)
                {
                    sTARTUPINFO.hStdError = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-12), false);
                }
                else
                {
                    startinfo.RedirectStandardError = true;
                    this._redirectstandarderror     = this.ResolveFilePath(this._redirectstandarderror);
                    sTARTUPINFO.hStdError           = this.GetSafeFileHandleForRedirection(this._redirectstandarderror, ProcessNativeMethods.CREATE_ALWAYS);
                }
                sTARTUPINFO.dwFlags = 0x100;
                int num = 0;
                if (!startinfo.CreateNoWindow)
                {
                    num = num | 16;
                    ProcessNativeMethods.STARTUPINFO sTARTUPINFO1 = sTARTUPINFO;
                    sTARTUPINFO1.dwFlags = sTARTUPINFO1.dwFlags | 1;
                    ProcessWindowStyle windowStyle = startinfo.WindowStyle;
                    switch (windowStyle)
                    {
                    case ProcessWindowStyle.Normal:
                    {
                        sTARTUPINFO.wShowWindow = 1;
                        break;
                    }

                    case ProcessWindowStyle.Hidden:
                    {
                        sTARTUPINFO.wShowWindow = 0;
                        break;
                    }

                    case ProcessWindowStyle.Minimized:
                    {
                        sTARTUPINFO.wShowWindow = 2;
                        break;
                    }

                    case ProcessWindowStyle.Maximized:
                    {
                        sTARTUPINFO.wShowWindow = 3;
                        break;
                    }
                    }
                }
                else
                {
                    num = 0;
                }
                IntPtr zero = IntPtr.Zero;
                if (startinfo.EnvironmentVariables != null && this.UseNewEnvironment)
                {
                    bool flag1 = false;
                    if (StartProcessCommand.ProcessManager.IsNt)
                    {
                        num   = num | 0x400;
                        flag1 = true;
                    }
                    gCHandle = GCHandle.Alloc(StartProcessCommand.EnvironmentBlock.ToByteArray(startinfo.EnvironmentVariables, flag1), GCHandleType.Pinned);
                    zero     = gCHandle.AddrOfPinnedObject();
                }
                if (this._credential == null)
                {
                    ProcessNativeMethods.SECURITY_ATTRIBUTES sECURITYATTRIBUTE  = new ProcessNativeMethods.SECURITY_ATTRIBUTES();
                    ProcessNativeMethods.SECURITY_ATTRIBUTES sECURITYATTRIBUTE1 = new ProcessNativeMethods.SECURITY_ATTRIBUTES();
                    flag = ProcessNativeMethods.CreateProcess(null, stringBuilder, sECURITYATTRIBUTE, sECURITYATTRIBUTE1, true, num, zero, startinfo.WorkingDirectory, sTARTUPINFO, pROCESSINFORMATION);
                    if (!flag)
                    {
                        lastWin32Error = Marshal.GetLastWin32Error();
                    }
                    if (!flag)
                    {
                        Win32Exception win32Exception = new Win32Exception(lastWin32Error);
                        str = StringUtil.Format(ProcessResources.InvalidStartProcess, win32Exception.Message);
                        ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null);
                        base.ThrowTerminatingError(errorRecord);
                    }
                }
                else
                {
                    ProcessNativeMethods.LogonFlags logonFlag = 0;
                    if (startinfo.LoadUserProfile)
                    {
                        logonFlag = ProcessNativeMethods.LogonFlags.LOGON_WITH_PROFILE;
                    }
                    IntPtr coTaskMemUnicode = IntPtr.Zero;
                    try
                    {
                        if (startinfo.Password != null)
                        {
                            coTaskMemUnicode = Marshal.SecureStringToCoTaskMemUnicode(startinfo.Password);
                        }
                        else
                        {
                            coTaskMemUnicode = Marshal.StringToCoTaskMemUni(string.Empty);
                        }
                        flag = ProcessNativeMethods.CreateProcessWithLogonW(startinfo.UserName, startinfo.Domain, coTaskMemUnicode, logonFlag, null, stringBuilder, num, zero, startinfo.WorkingDirectory, sTARTUPINFO, pROCESSINFORMATION);
                        if (!flag)
                        {
                            lastWin32Error = Marshal.GetLastWin32Error();
                        }
                        if (!flag)
                        {
                            if (lastWin32Error == 193)
                            {
                                str = StringUtil.Format(ProcessResources.InvalidApplication, this._path);
                                ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null);
                                base.ThrowTerminatingError(errorRecord1);
                            }
                            Win32Exception win32Exception1 = new Win32Exception(lastWin32Error);
                            str = StringUtil.Format(ProcessResources.InvalidStartProcess, win32Exception1.Message);
                            ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null);
                            base.ThrowTerminatingError(errorRecord2);
                        }
                    }
                    finally
                    {
                        if (coTaskMemUnicode != IntPtr.Zero)
                        {
                            Marshal.ZeroFreeCoTaskMemUnicode(coTaskMemUnicode);
                        }
                    }
                }
            }
            finally
            {
                if (gCHandle.IsAllocated)
                {
                    gCHandle.Free();
                }
                sTARTUPINFO.Dispose();
            }
            return(pROCESSINFORMATION.dwProcessId);
        }