Disable Windows error reporting dialogs

Disables various Windows error reporting dialogs in pdfium_test. Crashes
otherwise will trigger an error dialog, and cause pdfium_test to hang
during automated testing.

Bug: pdfium:1964
Change-Id: Ic10527e8601b643453119da6068bf7211d50c6a0
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/102671
Commit-Queue: K. Moon <kmoon@chromium.org>
Reviewed-by: Nigi <nigi@chromium.org>
Auto-Submit: K. Moon <kmoon@chromium.org>
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index c7dbe8d..35597ea 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -43,10 +43,12 @@
 #include "third_party/abseil-cpp/absl/types/optional.h"
 
 #ifdef _WIN32
+#include <crtdbg.h>
+#include <errhandlingapi.h>
 #include <io.h>
 #else
 #include <unistd.h>
-#endif
+#endif  // _WIN32
 
 #ifdef ENABLE_CALLGRIND
 #include <valgrind/callgrind.h>
@@ -1423,9 +1425,23 @@
     "  --time=<number> - Seconds since the epoch to set system time.\n"
     "";
 
+void SetUpErrorHandling() {
+#ifdef _WIN32
+  // Suppress various Windows error reporting mechanisms that can pop up dialog
+  // boxes and cause the program to hang.
+  SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT |
+               SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
+  _set_error_mode(_OUT_TO_STDERR);
+  _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
+  _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
+  _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
+#endif  // _WIN32
+}
+
 }  // namespace
 
 int main(int argc, const char* argv[]) {
+  SetUpErrorHandling();
   setlocale(LC_CTYPE, "en_US.UTF-8");  // For printf() of high-characters.
 
   std::vector<std::string> args(argv, argv + argc);