Ejemplo n.º 1
0
        static void ShellExecCmdLine(IntPtr hInstance, IntPtr hwnd, string command, string startDir, global::System.Diagnostics.ProcessWindowStyle nShow, ShellExecCmdLineFlags dwSeclFlags)
        {
            string cmd = command;
            string args = null;
            if (UrlIs(command, URLIS_URL))
                cmd = command;
            else
            {
                if (global::System.Environment.OSVersion.Version.Major >= 6)
                    EvaluateSystemAndUserCommandLine(cmd, startDir, out cmd, out args, dwSeclFlags);
                else
                    EvaluateUserCommandLine(cmd, startDir, out cmd, out args);
            }

            if (!UrlIs(cmd, URLIS_URL)
                && (
                    (dwSeclFlags & ShellExecCmdLineFlags.SECL_USEFULLPATHDIR) == ShellExecCmdLineFlags.SECL_USEFULLPATHDIR
                    || startDir == null
                    || startDir.Length == 0))
            {
                string dir = QualifyWorkingDir(cmd);
                if (dir != null)
                    startDir = dir;
            }

            if (UserSettingStorage.Instance.LeaveCmdOpen && File.Exists(cmd))
            {
                bool needsCommandLine;

                try
                {

                    var peHeaderReader = new PeHeaderReader(cmd);

                    if (peHeaderReader.Is32BitHeader)
                        needsCommandLine = peHeaderReader.OptionalHeader32.Subsystem == 3; // IMAGE_SUBSYSTEM_WINDOWS_CUI == 3
                    else
                        needsCommandLine = peHeaderReader.OptionalHeader64.Subsystem == 3;
                }

                catch (Exception)
                {
                    // Error reading the headers. We will try to run the command the standard way.
                    needsCommandLine = false;
                }

                if (needsCommandLine)
                {
                    string cmdExe;
                    string dummy;
                    EvaluateSystemAndUserCommandLine("cmd.exe", startDir, out cmdExe, out dummy, dwSeclFlags);

                    // check whether user typed >cmd, because we don't want to create 2 nested shells
                    if (cmdExe != cmd)
                    {
                        args = string.Format("/k {0} {1}", cmd, args);
                        cmd = cmdExe;
                    }
                }
            }

            global::System.Diagnostics.ProcessStartInfo startInfo = new global::System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.Arguments = args;
            startInfo.FileName = cmd;
            startInfo.WindowStyle = global::System.Diagnostics.ProcessWindowStyle.Normal;
            startInfo.ErrorDialog = (dwSeclFlags | ShellExecCmdLineFlags.SECL_NO_UI) == 0;
            startInfo.ErrorDialogParentHandle = hwnd;

            try
            {
                global::System.Diagnostics.Process.Start(startInfo);
            }
            catch (Exception e)
            {
                if (!startInfo.ErrorDialog)
                    throw e;
            }
        }
Ejemplo n.º 2
0
        static void EvaluateSystemAndUserCommandLine(string command, string startDir, out string cmd, out string args, ShellExecCmdLineFlags dwSeclFlags)
        {
            IntPtr pcmd, pcmdl, parg;
            int result = SHEvaluateSystemCommandTemplate(command, out pcmd, out pcmdl, out parg);
            if (result < 0)
            {
                if ((dwSeclFlags & ShellExecCmdLineFlags.SECL__IGNORE_ERROR) == 0)
                    throwHRESULT(result);

                EvaluateUserCommandLine(command, startDir, out cmd, out args);
            }
            else
            {
                cmd = Marshal.PtrToStringUni(pcmd);
                args = Marshal.PtrToStringUni(parg);
                Marshal.FreeCoTaskMem(pcmd);
                Marshal.FreeCoTaskMem(pcmdl);
                Marshal.FreeCoTaskMem(parg);
            }
        }
Ejemplo n.º 3
0
        static void ShellExecCmdLine(IntPtr hInstance, IntPtr hwnd, string command, string startDir, global::System.Diagnostics.ProcessWindowStyle nShow, ShellExecCmdLineFlags dwSeclFlags, bool runAsAdministrator = false, bool leaveCmdOpen = false)
        {
            string cmd  = command;
            string args = null;

            if (UrlIs(command, URLIS_URL))
            {
                cmd = command;
            }
            else
            {
                if (global::System.Environment.OSVersion.Version.Major >= 6)
                {
                    EvaluateSystemAndUserCommandLine(cmd, startDir, out cmd, out args, dwSeclFlags);
                }
                else
                {
                    EvaluateUserCommandLine(cmd, startDir, out cmd, out args);
                }
            }

            if (!UrlIs(cmd, URLIS_URL) &&
                (
                    (dwSeclFlags & ShellExecCmdLineFlags.SECL_USEFULLPATHDIR) == ShellExecCmdLineFlags.SECL_USEFULLPATHDIR ||
                    startDir == null ||
                    startDir.Length == 0))
            {
                string dir = QualifyWorkingDir(cmd);
                if (dir != null)
                {
                    startDir = dir;
                }
            }

            if (leaveCmdOpen && File.Exists(cmd))
            {
                bool needsCommandLine;

                try
                {
                    var peHeaderReader = new PeHeaderReader(cmd);

                    if (peHeaderReader.Is32BitHeader)
                    {
                        needsCommandLine = peHeaderReader.OptionalHeader32.Subsystem == 3;                 // IMAGE_SUBSYSTEM_WINDOWS_CUI == 3
                    }
                    else
                    {
                        needsCommandLine = peHeaderReader.OptionalHeader64.Subsystem == 3;
                    }
                }

                catch (Exception)
                {
                    // Error reading the headers. We will try to run the command the standard way.
                    needsCommandLine = false;
                }

                if (needsCommandLine)
                {
                    string cmdExe;
                    string dummy;
                    EvaluateSystemAndUserCommandLine("cmd.exe", startDir, out cmdExe, out dummy, dwSeclFlags);

                    // check whether user typed >cmd, because we don't want to create 2 nested shells
                    if (cmdExe != cmd)
                    {
                        args = string.Format("/k {0} {1}", cmd, args);
                        cmd  = cmdExe;
                    }
                }
            }

            global::System.Diagnostics.ProcessStartInfo startInfo = new global::System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.Arguments       = args;
            startInfo.FileName        = cmd;
            if (runAsAdministrator)
            {
                startInfo.Verb = "runas";
            }
            startInfo.WindowStyle             = global::System.Diagnostics.ProcessWindowStyle.Normal;
            startInfo.ErrorDialog             = (dwSeclFlags | ShellExecCmdLineFlags.SECL_NO_UI) == 0;
            startInfo.ErrorDialogParentHandle = hwnd;
            startInfo.WorkingDirectory        = startDir;
            try
            {
                global::System.Diagnostics.Process.Start(startInfo);
            }
            catch (Exception e)
            {
                if (!startInfo.ErrorDialog)
                {
                    throw e;
                }
            }
        }
Ejemplo n.º 4
0
        static void EvaluateSystemAndUserCommandLine(string command, string startDir, out string cmd, out string args, ShellExecCmdLineFlags dwSeclFlags)
        {
            IntPtr pcmd, pcmdl, parg;
            int    result = SHEvaluateSystemCommandTemplate(command, out pcmd, out pcmdl, out parg);

            if (result < 0)
            {
                if ((dwSeclFlags & ShellExecCmdLineFlags.SECL__IGNORE_ERROR) == 0)
                {
                    throwHRESULT(result);
                }

                EvaluateUserCommandLine(command, startDir, out cmd, out args);
            }
            else
            {
                cmd  = Marshal.PtrToStringUni(pcmd);
                args = Marshal.PtrToStringUni(parg);
                Marshal.FreeCoTaskMem(pcmd);
                Marshal.FreeCoTaskMem(pcmdl);
                Marshal.FreeCoTaskMem(parg);
            }
        }
Ejemplo n.º 5
0
        static void ShellExecCmdLine(IntPtr hInstance, IntPtr hwnd, string command, string startDir, global::System.Diagnostics.ProcessWindowStyle nShow, ShellExecCmdLineFlags dwSeclFlags)
        {
            string cmd = command;
            string args = null;
            if (UrlIs(command, URLIS_URL))
                cmd = command;
            else
            {
                if (global::System.Environment.OSVersion.Version.Major >= 6)
                    EvaluateSystemAndUserCommandLine(cmd, startDir, out cmd, out args, dwSeclFlags);
                else
                    EvaluateUserCommandLine(cmd, startDir, out cmd, out args);
            }

            if (!UrlIs(cmd, URLIS_URL)
                && (
                    (dwSeclFlags & ShellExecCmdLineFlags.SECL_USEFULLPATHDIR) == ShellExecCmdLineFlags.SECL_USEFULLPATHDIR
                    || startDir == null
                    || startDir.Length == 0))
            {
                string dir = QualifyWorkingDir(cmd);
                if (dir != null)
                    startDir = dir;
            }

            global::System.Diagnostics.ProcessStartInfo startInfo = new global::System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.Arguments = args;
            startInfo.FileName = cmd;
            startInfo.WindowStyle = global::System.Diagnostics.ProcessWindowStyle.Normal;
            startInfo.ErrorDialog = (dwSeclFlags | ShellExecCmdLineFlags.SECL_NO_UI) == 0;
            startInfo.ErrorDialogParentHandle = hwnd;

            try
            {
                global::System.Diagnostics.Process.Start(startInfo);
            }
            catch (Exception e)
            {
                if (!startInfo.ErrorDialog)
                    throw e;
            }
        }
Ejemplo n.º 6
0
        static void ShellExecCmdLine(IntPtr hInstance, IntPtr hwnd, string command, string startDir, global::System.Diagnostics.ProcessWindowStyle nShow, ShellExecCmdLineFlags dwSeclFlags)
        {
            string cmd  = command;
            string args = null;

            if (UrlIs(command, URLIS_URL))
            {
                cmd = command;
            }
            else
            {
                if (global::System.Environment.OSVersion.Version.Major >= 6)
                {
                    EvaluateSystemAndUserCommandLine(cmd, startDir, out cmd, out args, dwSeclFlags);
                }
                else
                {
                    EvaluateUserCommandLine(cmd, startDir, out cmd, out args);
                }
            }

            if (!UrlIs(cmd, URLIS_URL) &&
                (
                    (dwSeclFlags & ShellExecCmdLineFlags.SECL_USEFULLPATHDIR) == ShellExecCmdLineFlags.SECL_USEFULLPATHDIR ||
                    startDir == null ||
                    startDir.Length == 0))
            {
                string dir = QualifyWorkingDir(cmd);
                if (dir != null)
                {
                    startDir = dir;
                }
            }

            global::System.Diagnostics.ProcessStartInfo startInfo = new global::System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute         = true;
            startInfo.Arguments               = args;
            startInfo.FileName                = cmd;
            startInfo.WindowStyle             = global::System.Diagnostics.ProcessWindowStyle.Normal;
            startInfo.ErrorDialog             = (dwSeclFlags | ShellExecCmdLineFlags.SECL_NO_UI) == 0;
            startInfo.ErrorDialogParentHandle = hwnd;

            try
            {
                global::System.Diagnostics.Process.Start(startInfo);
            }
            catch (Exception e)
            {
                if (!startInfo.ErrorDialog)
                {
                    throw e;
                }
            }
        }