Can I use SetErrorMode in a C # process?

I'm getting ready to write an online judge,
A program that can compile user code and run a program to check the answer, like an online judge uva.

And I had a problem in catching an exception from the sending program, as shown below.

int main() { while(~scanf("%d %d",n,&m)) { printf("%d\n",n+m); } return 0; } 

its access is denied in the first line because it scans an integer for the error position.
How can I catch a runtime error for my process?

I used "try catch" to catch the exception,
but he did not respond to a runtime error.

therefore, I only check the exit code of the sending program, although this is not a good method to verify, other than the process.
and it looks like photo

I need to close the error message box manually,
and I find a solution that should use the SEH DLL handler for the process.

 SetErrorMode(SEM_NOGPFAULTERRORBOX); 

but I don't know how to use it in a C # process.

and below my code of judgment

 timer = new Stopwatch(); timer.Reset(); submitProg = new Process(); submitProg.StartInfo.FileName = outputFile; submitProg.StartInfo.UseShellExecute = false; submitProg.StartInfo.CreateNoWindow = true; submitProg.StartInfo.RedirectStandardInput = true; submitProg.StartInfo.RedirectStandardOutput = true; submitProg.StartInfo.RedirectStandardError = true; submitProg.StartInfo.ErrorDialog = false; submitProg.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; submitProg.EnableRaisingEvents = true; submitProg.Start(); timer.Start(); progInput = submitProg.StandardInput; progOutput = submitProg.StandardOutput; progInput.Write(inputStream.ReadToEnd()); submitProg.StandardInput.Close(); while (!submitProg.HasExited) { peakPagedMem = submitProg.PeakPagedMemorySize64; peakVirtualMem = submitProg.PeakVirtualMemorySize64; peakWorkingSet = submitProg.PeakWorkingSet64; if (peakPagedMem > memLimit) { submitProg.Kill(); } if (timer.ElapsedMilliseconds > timeLimit) { timeLimitExceed = true; submitProg.Kill(); } } timeUsed = timer.ElapsedMilliseconds; timer.Stop(); if(submitProg.ExitCode!=0)systemRuntimeError = true; 

Thank you for helping, and so sorry for my poor English.

====================================
PS
the question is how can I set the error mode for a child process in C #.
My English is not good enough to explain the problem, so sorry. <(_ _)>

+6
source share
2 answers

You cannot set the error mode in another process without inserting code into this process. This cannot be done in C #. This will not work well in C / C ++, these programs are short-lived to give you time for injection.

In any case, this does not solve your problem, you also need to protect against programs that never exit after they get stuck in an endless loop. A simple solution is to give each program a limited amount of time to execute. Say 10 seconds. If it does not complete the Process.Kill () process and declares a failure. It will also take care of programs that bombed using the message box.

It is trivial to implement Process.WaitForExit (milliseconds) with an overload.

+5
source

If you mean the Win32 API function SetErrorMode , you will need to use P / Invoke , which is easy with such a simple signature:

 [DllImport("kernel32.dll")] static extern ErrorModes SetErrorMode(ErrorModes uMode); [Flags] public enum ErrorModes : uint { SYSTEM_DEFAULT = 0x0, SEM_FAILCRITICALERRORS = 0x0001, SEM_NOALIGNMENTFAULTEXCEPT = 0x0004, SEM_NOGPFAULTERRORBOX = 0x0002, SEM_NOOPENFILEERRORBOX = 0x8000 } 

(The site http://www.pinvoike.net/ is always a good place to start getting the right ad.)

+10
source

Source: https://habr.com/ru/post/888689/


All Articles