Beispiel #1
0
        /// <summary>
        /// Executes provided exe file and returns the result of program using.
        /// </summary>
        ///
        /// <param name="exePath">
        /// Path of exe file to run.
        /// </param>
        ///
        /// <param name="program">
        /// Execunitg constraints.(like memory limit, time limit)
        /// </param>
        /// <param name="arguments">
        ///
        /// </param>
        ///
        /// <returns>
        /// Detailed result of program executing.
        /// </returns>
        ///
        /// <exception cref="ArgumentNullException">
        /// If any argument is null.
        /// </exception>
        /// <exception cref="FileNotFoundException">
        /// If provided path is invalid.
        /// </exception>
        public static Result ExecuteWin32(string exePath, Program program, string arguments)
        {
            //validate arguments
            ProjectHelper.ValidateNotNull(program, "program");
            ProjectHelper.ValidateFileExists(exePath, "exePath");

            //Set error mode, for hiding error message boxes.
            SetErrorMode(0x0001 | 0x0002 | 0x0004 | 0x8000);

            var result = new Result {
                ProgramStatus = Status.Running
            };

            //create new process
            using (var exeProcess = new Process())
            {
                exeProcess.StartInfo.RedirectStandardError  = true;
                exeProcess.StartInfo.RedirectStandardInput  = true;
                exeProcess.StartInfo.RedirectStandardOutput = true;
                exeProcess.StartInfo.CreateNoWindow         = true;
                exeProcess.StartInfo.UseShellExecute        = false;

                exeProcess.StartInfo.FileName  = exePath;
                exeProcess.StartInfo.Arguments = arguments;
                exeProcess.StartInfo.Arguments = exeProcess.StartInfo.Arguments;

                //start process
                var memoryCounter = new MemoryCounter(exeProcess, 20);
                exeProcess.Start();

                Thread.Sleep(200);
                //write input data
                exeProcess.StandardInput.Write(program.InputTest);
                exeProcess.StandardInput.Close();
                exeProcess.WaitForExit(program.TimeLimit);

                if (!exeProcess.HasExited)
                {
                    exeProcess.Kill();
                    result.ProgramStatus = Status.TimeLimit;
                }

                memoryCounter.Stop();

                //get program statistic
                result.Error      = exeProcess.StandardError.ReadToEnd();
                result.Output     = exeProcess.StandardOutput.ReadToEnd();
                result.TimeUsed   = (int)exeProcess.TotalProcessorTime.TotalMilliseconds;
                result.MemoryUsed = (int)memoryCounter.Memory / 1024;
                result.Output     = result.Output.Trim();

                //set program status
                if (result.ProgramStatus != Status.TimeLimit)
                {
                    if (exeProcess.ExitCode != 0)
                    {
                        result.ProgramStatus = Status.Crashed;
                    }
                    else
                    {
                        if (result.MemoryUsed > program.MemoryLimit)
                        {
                            result.ProgramStatus = Status.MemoryLimit;
                        }
                        else
                        {
                            if (result.Output == program.OutputTest)
                            {
                                result.ProgramStatus = Status.Accepted;
                            }
                            else
                            {
                                result.ProgramStatus = Status.WrongAnswer;
                            }
                        }
                    }
                }
            }

            return(result);
        }