windows 运行批处理文件时如何防止失败的程序(.exe)弹出错误消息框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4062215/
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
How to prevent error pop-up message box for failed program (.exe) when running batch file
提问by Artyom
I'm running a test script from batch file.
我正在从批处理文件运行测试脚本。
Because it is test, the programs are expected to fail once in a while. It is file as long as error code is returned so I can continue and mark specific test as failed.
因为它是测试,所以预计程序偶尔会失败。只要返回错误代码,它就是文件,因此我可以继续并将特定测试标记为失败。
However there is very annoying behavior of executable files under Microsoft Windows - if something fails it pop-ups window like:
然而,在 Microsoft Windows 下,可执行文件的行为非常烦人——如果出现问题,它会弹出如下窗口:
This application has failed to start because foo.dll was not found, Re-installing the application may fix the problem
<OK>
此应用程序无法启动,因为找不到 foo.dll,重新安装应用程序可能会解决问题
<OK>
Or even better:
或者甚至更好:
The instruction at "..." referenced to memory at "..." ..
Click on OK to terminate the program
Click on CANCEL to debug the program
“...”处的指令引用了“...”处的内存...
点击 OK 终止程序
点击 CANCEL 调试程序
The result is known - the script execution blocks till somebody presses "Ok" button. And when we talk about automatic scripts that may run automatically at night in some headless virtual machine, it may be very problematic.
结果是已知的 - 脚本执行阻塞,直到有人按下“确定”按钮。而当我们谈论在一些无头虚拟机中可能会在夜间自动运行的自动脚本时,它可能会很成问题。
Is there a simpleway to prevent such behavior and just make an application to exit with failure code - withoutchanging the code of the program itself?
有没有一种简单的方法可以防止这种行为,而只是让应用程序以失败代码退出——而不更改程序本身的代码?
Is this possible at all?
这可能吗?
采纳答案by Artyom
The answer is following: You need to disable WER.
答案如下:您需要禁用WER。
Simplest description for this I found at http://www.noktec.be/archives/259
我在http://www.noktec.be/archives/259 上找到了对此的最简单描述
Simply (ON XP): Right Click on My Computer > Advanced > Error Reporting > Disable
简单(在 XP 上):右键单击“我的电脑”>“高级”>“错误报告”>“禁用”
Voila - programs crash silently!
瞧 - 程序无声地崩溃!
This does not solves problem when DLL is missing, but this is much rare case and this is good enough for me.
当 DLL 丢失时,这并不能解决问题,但这种情况非常罕见,这对我来说已经足够了。
回答by Vatsan
You can suppress AV's and such from showing a dialog box by running your application, or the script (the script engine, like cscript.exe), under a debugger.
您可以通过在调试器下运行您的应用程序或脚本(脚本引擎,如 cscript.exe)来禁止 AV 等显示对话框。
Use Gflags.exe, or modify the registry directly, and set Image File Execution Options for the image in question. See this articlefor details on how to use the appropriate registry keys. You can set it up using a debugger commandline like "C:\Debuggers\ntsd.exe -g -G -c'command'", where you can pass commands to ignore certain types of exceptions in the -c"commmand" argument. This will effectively give you a tool to suppress interactive dialogs as a result of exceptions like AV, and will let the process continue (presumably to immediate end after the exception has occured).
使用Gflags.exe,或者直接修改注册表,为有问题的镜像设置Image File Execution Options。有关如何使用适当的注册表项的详细信息,请参阅本文。您可以使用诸如“C:\Debuggers\ntsd.exe -g -G -c'command'”之类的调试器命令行对其进行设置,您可以在其中传递命令以忽略 -c"commmand" 参数中的某些类型的异常。这将有效地为您提供一个工具来抑制由于 AV 等异常而导致的交互式对话框,并使进程继续(大概在异常发生后立即结束)。
Thisarticle explains the commands you can use to control exceptions and events from withing the debugger.
此文章解释了您可以使用从withing调试器控制异常和事件的命令。
The -g and -G flags make sure that the process won't break into the debugger automatically during process start and end respectively. You'll have to play with the various exception suppression options to make sure that you 'eat' all possible first and second chance exceptiosn that might cause the process to break into the debugger.
-g 和 -G 标志确保进程不会分别在进程开始和结束期间自动闯入调试器。您必须使用各种异常抑制选项,以确保您“吃掉”所有可能导致进程中断调试器的第一次和第二次机会异常。
Also, if you can tolerate a process being broken into the debugger (as against being stuck showing a dialog box), perhaps that would be a better option overall. You can evaluate each debug break in batch mode at a later time and decide which bugs you care to fix.
此外,如果您可以容忍一个进程被闯入调试器(而不是卡在显示对话框中),那么总体而言这可能是一个更好的选择。您可以稍后以批处理模式评估每个调试中断,并决定您需要修复哪些错误。
回答by paxdiablo
It ispossible. We used to use IBM's Rational Robot product which could monitor the screen for specific items and, if found, send keystrokes to windows and other sorts of things.
这是可能的。我们曾经使用 IBM 的 Rational Robot 产品,该产品可以监控特定项目的屏幕,如果找到,则将击键发送到窗口和其他类型的东西。
We actually used it for fully automated unit and system testing, much like you're trying to do.
我们实际上将它用于全自动单元和系统测试,就像您正在尝试做的那样。
Now I thought that Robot has been through quite a few name changes so it may be hard to find but there it is, right on IBM's web page and with a free downloadable trial for you. It's not cheap, clocking in at a smidgeon under USD5,000 but it wasworth it for us.
现在我认为 Robot 已经经历了很多名称更改,因此可能很难找到它,但它就在 IBM 的网页上,并为您提供免费下载试用版。它并不便宜,价格低于 5,000美元,但对我们来说是值得的。
There's also TestCompletewhere you could get a licence for just unedr USD1,000 - it touts "Black-box testing - Functional testing of any Windows application" as one of its features and also has a downloadable demo to see if it's suitable before purchase.
还有TestComplete,您只需花费 1,000 美元即可获得许可证——它吹捧“黑盒测试——任何 Windows 应用程序的功能测试”作为其功能之一,并且还有一个可下载的演示,以在购买前查看它是否合适。
However, you may be able to find another product to do the same sort of thing.
但是,您也许可以找到其他产品来做同样的事情。
I initially thought of Expect
but the ActiveState one seems to concentrate on console applications which leads me to believe it may not do graphics well.
我最初想到的Expect
是 ActiveState 似乎专注于控制台应用程序,这让我相信它可能无法很好地处理图形。
The only other option I can suggest is to write your ownprogram in VBScript. I've done this before to automate the starting of many processes (log on to work VPN, start mail, log in and so on) so I could be fully set up with one mouseclick instead of having to start everything manually.
我可以建议的唯一其他选择是用 VBScript编写您自己的程序。我以前这样做是为了自动启动许多进程(登录到工作 VPN、启动邮件、登录等),这样我就可以通过单击鼠标完成设置,而不必手动启动所有内容。
You can use AppActivate
to bring a window to the foreground and SendKeys
to send arbitrary keypresses to it after that. It's possible you may be able to cobble together something from that if you want a cheaper solution.
您可以使用AppActivate
将窗口置于前台并SendKeys
在此之后向其发送任意按键。如果您想要更便宜的解决方案,您可能可以从中拼凑出一些东西。