Search code examples
windowswinapiwindows-10shellexecuteex

ShellExecuteEx with SEE_MASK_FLAG_NO_UI displays message anyway


On my system the .xyz extension is not registered at all. So when I double click on an .xyz file in the Explorer, Windows 10 pops up the standard "How do you want to open this file" dialog which is expected.

Now when I run this short snippet, the same dialog as above is displayed, even though I put the SEE_MASK_FLAG_NO_UI flag in sei.fMask:

  SHELLEXECUTEINFO sei = { 0 };
  sei.cbSize = sizeof(SHELLEXECUTEINFO) ;
  sei.fMask = SEE_MASK_FLAG_NO_UI;
  sei.hwnd = AfxGetMainWnd()->GetSafeHwnd();
  sei.lpVerb = _T("open");
  sei.lpFile = _T("C:\\Users\\Test\\Documents\\temp\\Temp.xyz");
  sei.lpParameters = NULL;
  sei.lpDirectory = appdir;
  sei.nShow = SW_SHOW;
  ShellExecuteEx(&sei); 
  • The file "C:\\Users\\Test\\Documents\\temp\\Temp.xyz" exists.

  • The same code with sei.lpFile pointing to an existing .txt opens it with Notepad as expected.

The documentation pretends that no error message should be displayed with the SEE_MASK_FLAG_NO_UI.

So what can I do so ShellExecuteEx does not pop up any message but returns an error code instead?

I only checked this on Windows 10, I don't know what the behaviour is on older Windows versions.


Solution

  • Use FindExecutable() or AssocQueryString() before calling ShellExecute(). This allows to check if an association exists. SEE_MASK_FLAG_NO_UI prevents error displaying, however this situation is not treated as an error.