views:

184

answers:

2

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?

A: 

why don't you use the CreateProcess function

Remus Rigo
Thank you Remus for your suggestion. I don't use CreateProcess, because it also displays the error dialog. I was hoping that the SEE_MASK_FLAG_NO_UI option would help, somehow.
Pierre
+1  A: 

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.

Hans Passant
You are completely right. And the only way to work around this issue is to detect that there is no .NET and avoid starting the application altogether.
Pierre