C++ 如何在 Windows 的发布模式下避免“program.exe 已停止工作”窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11559435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:15:50  来源:igfitidea点击:

How to avoid "program.exe has stopped working" window in release mode on windows?

c++windowsvisual-studio-2010

提问by Cornelia Serban

I'm working on the development of a software in C++ on Visual Studio 2010. As this software should be run on servers where human interaction is not available, I really really need to get rid of this "program.exe has stopped working" window that pops up in the release version in case of errors. I just want the program to terminate (maybe also give an error message, but not necessarily) and not have it remain blocked waiting for someone to click the "Close the program" button. I have to mention that I have 64 bit Windows 7 Professional on my machine.

我正在开发 Visual Studio 2010 上的 C++ 软件。由于该软件应该在无法进行人工交互的服务器上运行,我真的需要摆脱这个“program.exe 已停止工作”窗口如果出现错误,会在发布版本中弹出。我只希望程序终止(可能也会给出错误消息,但不一定),而不是让它保持阻塞状态,等待某人单击“关闭程序”按钮。我不得不提一下,我的机器上装有 64 位 Windows 7 Professional。

I have read about several things, such as:

我已经阅读了几件事,例如:

  • the _set_abort_behavior function. This solves the case when abort() is called, but that is not the case for errors like "vector subscript out of range".

  • I know I could be solving some of these errors by doing exception handling, but not all errors are exceptions, therefore this would not solve my entire problem.

  • I've also read something about the Dr. Watson debugger, which is supposed to terminate the application silently, but I have the impression that this debugger is not available for Windows 7. Plus I don't know if this debugger would solve my problem in the release mode...

  • I don't find that disabling Error Reporting on my entire machine is an elegant option, although I read that this could also be an alternative (not really one that I want to take).

  • _set_abort_behavior 函数。这解决了调用 abort() 时的情况,但对于“向量下标超出范围”之类的错误则不是这种情况。

  • 我知道我可以通过进行异常处理来解决其中一些错误,但并非所有错误都是异常,因此这不能解决我的整个问题。

  • 我还阅读了有关 Dr. Watson 调试器的一些内容,它应该以静默方式终止应用程序,但我的印象是此调试器不适用于 Windows 7。另外,我不知道此调试器是否能解决我的问题在发布模式...

  • 我不认为在我的整个机器上禁用错误报告是一个优雅的选择,尽管我读到这也可能是一种替代方法(不是我真正想要的)。

How can I do this in Visual Studio? Are there any settings I could use?

如何在 Visual Studio 中执行此操作?有什么我可以使用的设置吗?

Is there maybe a way to turn all errors in exceptions in Visual Studio, so that I can solve the problem with the exception handling mechanism? (please be tolerant if this was a stupid question)

有没有办法在Visual Studio中把所有的错误都转成异常,这样我就可以用异常处理机制来解决问题?(如果这是一个愚蠢的问题,请宽容)

I'm looking forward to your proposals. Many thanks for your time!

我期待着你的提议。非常感谢您的时间!

Best regards, Cornelia

最好的问候,科妮莉亚

回答by jsist

You can use

您可以使用

SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);

MSDN documentation for SetErrorMode

SetErrorMode 的 MSDN 文档

You can also refer here

你也可以参考这里

Hope this will solve your problem...

希望这能解决你的问题...

回答by the_mandrill

A good way is to add your own unhandled exception filter (see SetUnhandledExceptionFilter()docs) and then write a log and a mini dump so that you can do whatever error handling you need to (eg close devices/handles) and you also have a crash dump that you can analyse later. Useful information in this answer: https://stackoverflow.com/a/1547251/188414

一个好方法是添加您自己的未处理异常过滤器(请参阅SetUnhandledExceptionFilter()文档),然后编写日志和小型转储,以便您可以执行所需的任何错误处理(例如关闭设备/句柄),并且您还有一个故障转储,您可以稍后分析。此答案中的有用信息:https: //stackoverflow.com/a/1547251/188414

回答by Khalid Baig

Sometimes this error occurs only because you have not added a &sign before the value you have used in scanf

有时发生此错误只是因为您没有&在您使用的值之前添加符号scanf

Try the following which solved my problem

尝试以下解决了我的问题

From

scanf("%d",code);

To

scanf("%d",&code);