在没有 .NET 的系统上启动 .NET 应用程序时,带有 SEE_MASK_FLAG_NO_UI 的 ShellExecuteEx 显示错误
ShellExecuteEx
Win32 函数调用在其 SHELLEXECUTEINFO
结构中有一个标志 SEE_MASK_FLAG_NO_UI
,该标志应禁止在以下情况下因错误而可能显示的任何错误对话框:启动应用程序。
MSDN文档此处对此非常明确:
SEE_MASK_FLAG_NO_UI
0x00000400. Do not display an error message box if an error occurs.
就我而言,我在未安装 .NET 的 Windows XP 系统上启动 .NET 可执行文件。我系统地收到以下消息,由 Windows 在对话窗口中显示:
Xxx.exe - Application Error
The application failed to initialize properly (0xc0000135).
Click on OK to terminate the application.
[ OK ]
我不希望用户必须处理此消息。我宁愿从 ShellExecuteEx 返回错误代码,并能够在我的程序中优雅地处理它。以下是我用来启动外部可执行文件的代码片段:
#include <windows.h>
int wmain(int argc, wchar_t* argv[])
{
SHELLEXECUTEINFO info;
memset(&info, 0, sizeof(SHELLEXECUTEINFO));
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.fMask = SEE_MASK_FLAG_NO_UI;
info.lpVerb = L"open";
info.lpFile = L"Xxx.exe";
info.nShow = SW_SHOW;
return ShellExecuteEx (&info);
}
如果系统上不存在 .NET,是否有一种官方方法可以抑制错误消息?或者我是否必须在执行应用程序之前自己检查此特定条件(但我事先不知道它是 .NET 应用程序还是本机应用程序)。例如,如果我启动的应用程序缺少一些 DLL 怎么办?
The ShellExecuteEx
Win32 function call has a flag SEE_MASK_FLAG_NO_UI
in its SHELLEXECUTEINFO
structure, which should suppress any error dialogs which could be displayed because of an error when launching an application.
The MSDN documentation here is quite explicit about it:
SEE_MASK_FLAG_NO_UI
0x00000400. Do not display an error message box if an error occurs.
In my case, I am launching a .NET executable on a Windows XP system where no .NET is installed. I systematically receive the following message, displayed by Windows in a dialog window:
Xxx.exe - Application Error
The application failed to initialize properly (0xc0000135).
Click on OK to terminate the application.
[ OK ]
I don't want the user to have to deal with this message. I'd rather get back an error code from ShellExecuteEx
and be able to handle it gracefully in my program. Here is the code snippet which I am using to start the external executable:
#include <windows.h>
int wmain(int argc, wchar_t* argv[])
{
SHELLEXECUTEINFO info;
memset(&info, 0, sizeof(SHELLEXECUTEINFO));
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.fMask = SEE_MASK_FLAG_NO_UI;
info.lpVerb = L"open";
info.lpFile = L"Xxx.exe";
info.nShow = SW_SHOW;
return ShellExecuteEx (&info);
}
Is there an official way of suppressing the error message if .NET is not present on the system? Or do I have to check for this specific condition myself before executing the application (but I do not know in advance if it is a .NET app or a native app). And what if the app I am starting is missing some DLLs, for instance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该标志仅告诉 shell 不要显示错误消息。它不会影响已启动进程的 UI。 .NET .exe 确实启动了,因此 ShellExecuteEx() 完成了它的工作并且没有看到任何错误。它决定事后轰炸并让用户知道它不是您可以轻松修复的事情。
The flag only tells the shell to not display an error message. It doesn't affect the UI of the process that got started. The .NET .exe really did get started so ShellExecuteEx() did it's job and saw no errors. That it decided to bomb afterwards and let the user know about it is not something you can easily fix.
为什么不使用 CreateProcess功能
why don't you use the CreateProcess function