{"id":92972,"date":"2016-02-04T07:00:00","date_gmt":"2016-02-04T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=92972"},"modified":"2019-03-13T10:29:56","modified_gmt":"2019-03-13T17:29:56","slug":"20160204-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160204-00\/?p=92972","title":{"rendered":"How do I prevent a child process from displaying the Windows Error Reporting dialog?"},"content":{"rendered":"<p>A customer wanted to know if there was a way to disable Windows Error Reporting for a child process. Their scenario was that they had a main program which runs a child program that is expected to crash (because it&#8217;s a unit test). Normally, this would result in the Windows Error Report dialog appearing, offering to send a crash report to Microsoft. This dialog is unwanted, since it makes it difficult to run the unit test as part of an automated script, plus there&#8217;s no point sending crash reports for a unit test that is expected to crash. <\/p>\n<p>One person suggested using the <a HREF=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/bb513617(v=vs.85).aspx\"><code>Wer&shy;Add&shy;Excluded&shy;Application<\/code> function<\/a> to add the program to the list of programs to be excluded from error reporting, and then call the <code>Wer&shy;Remove&shy;Excluded&shy;Application<\/code> function when the test completes. There are some problems with this approach: <\/p>\n<ul>\n<li>The name of your unit test application may happen to match that of a legitimate application, and the <code>Wer&shy;Add&shy;Excluded&shy;Application<\/code> function takes only the file name, not a full path. You can try to arrange for your unit test name to be sufficiently unique that this sort of collision is unlikely. \n<li>If the test harness crashes, then  <code>Wer&shy;Remove&shy;Excluded&shy;Application<\/code> will never get called, and then the unit test ends up excluded permanently. \n<li>Suppose two copies of the unit test are running. Given that you&#8217;re doing automated testing, there&#8217;s a decent chance that this will happen; for example, the automated system may be running unit tests as part of a gated check-in system, and multiple check-ins could be undergoing validation at once. In that case, both test harnesses will call <code>Wer&shy;Add&shy;Excluded&shy;Application<\/code> when they start up, and the first one to finish will call <code>Wer&shy;Remove&shy;Excluded&shy;Application<\/code>. This rips the registration out from under the second instance, which is still running, and the crash dialog will appear. <\/ul>\n<p>Basically, we are applying a global solution to a local problem. <\/p>\n<p>Better to apply a local solution: Since the error mode is inherited by child processes, you can have the test harness call <code>Set&shy;Error&shy;Mode<\/code> with the <code>SEM_NO&shy;GP&shy;FAULT&shy;ERROR&shy;BOX<\/code> flag to disable the error reporting dialog for itself and its children. <\/p>\n<p>Here&#8217;s a Little Program to illustrate. Remember that Little Programs do little to no error checking. In this case, the Little Program also leaks handles!<\/p>\n<pre>\n#include &lt;windows.h&gt;\n#include &lt;stdio.h&gt;\n\nint main(int argc, char**argv)\n{\n    printf(\"Error mode is %d\\n\", GetErrorMode());\n    if (argc == 1) {\n        TCHAR buf[256];\n        GetModuleFileName(nullptr, buf, 256);\n        STARTUPINFO si = { sizeof(si) };\n        PROCESS_INFORMATION pi;\n        SetErrorMode(SEM_NOGPFAULTERRORBOX);\n        printf(\"Spwaning child, error mode is %d\\n\", GetErrorMode());\n        CreateProcess(buf, TEXT(\"a b c d\"), nullptr, nullptr, FALSE,\n            0, nullptr, nullptr, &amp;si, &amp;pi);\n        WaitForSingleObject(pi.hProcess, INFINITE);\n        printf(\"Done\\n\");\n    } else {\n        DebugBreak();\n    }\n    return 0;\n}\n<\/pre>\n<p>If you run this program, it will start by reporting that its error mode is zero, and then it changes to 2 (which is the value of the <code>SEM_NO&shy;GP&shy;FAULT&shy;ERROR&shy;BOX<\/code> flag), and then the child process will report that its error mode is 2 (inherited), and then it will crash by invoking a nonexistent debugger. Since error dialogs are disabled, the child process will exit silently. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Take advantage of the fact that SetErrorMode is inherited.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-92972","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Take advantage of the fact that SetErrorMode is inherited.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/92972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=92972"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/92972\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=92972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=92972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=92972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}