コード例 #1
0
        public void DirectoryAsOutFileTest()
        {
            // Create a temp directory and try to pass it to mse
            string tempName = Path.GetTempFileName();
            File.Delete(tempName);

            tempName += "d";
            Directory.CreateDirectory(tempName);
            ArgumentExecuter target = new ArgumentExecuter(new string[] { "/p", "/o", tempName });
            try
            {
                target.ExecuteArguments();
                Assert.Fail("Invalid out file test failed when using a directory for /o!");
            }
            catch (IOException)
            {
                // Success
            }
            finally
            {
                Directory.Delete(tempName);
            }
        }
コード例 #2
0
        public static int Main(string[] args)
        {
            //if true then new process isnt created which allows easier debugging
            bool debugMode = false;

            if (Debugger.IsAttached)
            {
                debugMode = true;
            }

            IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);

            if (args == null || args.Length == 0)
            {
                const int STD_OUTPUT_HANDLE = -11;
                IntPtr    stdOut            = GetStdHandle(STD_OUTPUT_HANDLE);

                //if the stdOuit handle is -1 or we are in debug mode then run the gui
                if ((stdOut == INVALID_HANDLE_VALUE) || debugMode)
                {
                    Application.EnableVisualStyles();
                    Application.Run(new MainGui());
                }
                else
                {
                    //create a new process to run the gui in

                    const int DETACHED_PROCESS     = 0x00000008;
                    const int STARTF_USESHOWWINDOW = 0x00000001;
                    const int STARTF_USESTDHANDLES = 0x00000100;
                    const int SW_SHOW = 5;

                    Process p = Process.GetCurrentProcess();

                    //path to the exe
                    string Application        = System.IO.Path.GetFullPath(p.MainModule.FileName);
                    string CommandLine        = "";
                    PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
                    STARTUPINFO         sInfo = new STARTUPINFO();
                    sInfo.cb          = System.Runtime.InteropServices.Marshal.SizeOf(sInfo);
                    sInfo.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
                    sInfo.wShowWindow = SW_SHOW;

                    //send stdOutput as -1 to tell new process to open as gui
                    sInfo.hStdOutput = (IntPtr)(-1);

                    SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
                    SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
                    pSec.nLength = Marshal.SizeOf(pSec);
                    tSec.nLength = Marshal.SizeOf(tSec);

                    //create the new process
                    CreateProcess(Application, CommandLine, ref pSec, ref tSec, true, DETACHED_PROCESS,
                                  IntPtr.Zero, null, ref sInfo, out pInfo);
                }
            }
            else
            {
                //run in command line mode if arguments are present
                int error = 1;
                using (ArgumentExecuter argExecute = new ArgumentExecuter(args))
                {
                    try
                    {
                        if ((error = argExecute.ExecuteArguments()) != 0)
                        {
                            Console.WriteLine(argExecute.ErrorMessage);
                        }
                    }
                    catch (System.IO.IOException ex)
                    {
                        error = 1;
                        Console.WriteLine(ex.Message);
                    }
                }
                return(error);
            }

            return(0);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: artisticcheese/MSE
        public static int Main(string[] args)
        {
            //if true then new process isnt created which allows easier debugging
            bool debugMode = false;

            if (Debugger.IsAttached)
                debugMode = true;

            IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);
            if (args == null || args.Length == 0)
            {
                const int STD_OUTPUT_HANDLE = -11;
                IntPtr stdOut = GetStdHandle(STD_OUTPUT_HANDLE);

                //if the stdOuit handle is -1 or we are in debug mode then run the gui
                if ((stdOut == INVALID_HANDLE_VALUE) || debugMode)
                {
                    Application.EnableVisualStyles();
                    Application.Run(new MainGui());
                }
                else
                {
                    //create a new process to run the gui in

                    const int DETACHED_PROCESS = 0x00000008;
                    const int STARTF_USESHOWWINDOW = 0x00000001;
                    const int STARTF_USESTDHANDLES = 0x00000100;
                    const int SW_SHOW = 5;

                    Process p = Process.GetCurrentProcess();

                    //path to the exe
                    string Application = System.IO.Path.GetFullPath(p.MainModule.FileName);
                    string CommandLine = "";
                    PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
                    STARTUPINFO sInfo = new STARTUPINFO();
                    sInfo.cb = System.Runtime.InteropServices.Marshal.SizeOf(sInfo);
                    sInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
                    sInfo.wShowWindow = SW_SHOW;

                    //send stdOutput as -1 to tell new process to open as gui
                    sInfo.hStdOutput = (IntPtr)(-1);

                    SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
                    SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
                    pSec.nLength = Marshal.SizeOf(pSec);
                    tSec.nLength = Marshal.SizeOf(tSec);

                    //create the new process
                    CreateProcess(Application, CommandLine, ref pSec, ref tSec, true, DETACHED_PROCESS,
                                    IntPtr.Zero, null, ref sInfo, out pInfo);
                }
            }
            else
            {
                //run in command line mode if arguments are present
                int error = 1;
                using (ArgumentExecuter argExecute = new ArgumentExecuter(args))
                {
                    try
                    {
                        if ((error = argExecute.ExecuteArguments()) != 0)
                        {
                            Console.WriteLine(argExecute.ErrorMessage);
                        }
                    }
                    catch (System.IO.IOException ex)
                    {
                        error = 1;
                        Console.WriteLine(ex.Message);
                    }
                }
                return error;
            }

            return 0;
        }
コード例 #4
0
        public void ExecuteArgumentsTest()
        {
            int procID = TestEnvironment.testProcessInfo.ProcessId;
            string fileName = Path.GetTempFileName();
            string[] args = new string[] { "/p", procID.ToString(), "/s", "/o", fileName };

            ArgumentExecuter target = new ArgumentExecuter(args);
            int expected = 0;

            int actual = target.ExecuteArguments();
            bool success = false;
            using (StreamReader fileReader = new StreamReader(new FileStream(fileName, FileMode.Open)))
            {
                string stackString = fileReader.ReadToEnd();
                success = TestEnvironment.MatchedPredictedStackTrace(stackString);
            }
            try
            {
                File.Delete(fileName);
            }
            catch (FileNotFoundException ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            if ((expected != actual) || !success)
            {
                Assert.Fail("Microsoft.Mse.Gui.ArgumentExecuter.ExecuteArguments failed.");
            }
        }
コード例 #5
0
 public void InvalidProcessIdTest()
 {
     ArgumentExecuter target = new ArgumentExecuter(new string[] { "/p", "0" });
     int retValue = target.ExecuteArguments();
     if (retValue != 1)
     {
         Assert.Fail("Invalid process Id test failed for process ID 0!");
     }
 }
コード例 #6
0
        public void InvalidOutFileTest()
        {
            // Pass in an invalid file name and check that IOException is thrown
            ArgumentExecuter target = new ArgumentExecuter(new string[] {"/p", "/o", "???.txt"});

            try
            {
                target.ExecuteArguments();
                Assert.Fail("Invalid out file test failed for ???.txt!");
            }
            catch (IOException)
            {
                // Success
            }
        }