ShellExecuteEx can't open image while mspaint is default app since windows 10

Viewed 255

The execution of the following code will give an error if mspaint is chosen as the default app for images.

Setting default app for images to any other app, e.g. "Photos", will open the image with the chosen app without error.

Using ShExecInfo.lpVerb = L"edit" will open the image with mspaint without error.

The problem occurs since Windows 10.

#include <shlwapi.h>

int main()
{
    SHELLEXECUTEINFO ShExecInfo;
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = NULL;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = L"open"; //L"edit"
    ShExecInfo.lpFile = L"C:/test.jpg";
    ShExecInfo.lpParameters = NULL;
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_MAXIMIZE;
    ShExecInfo.hInstApp = NULL;

    ShellExecuteEx(&ShExecInfo);

    return 0;
}

enter image description here

Is it a known Microsoft bug?

1 Answers

This works:

SHELLEXECUTEINFOW ShExecInfo = {};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFOW);
ShExecInfo.lpVerb = L"edit";
ShExecInfo.lpFile = L"C:/test.jpg";
ShExecInfo.nShow = SW_MAXIMIZE;
ShExecInfo.fMask = SEE_MASK_NOASYNC;

if (!ShellExecuteExW(&ShExecInfo))
{
    // Error reported in GetLastError()
  • There's no open verb associated with JPEG's in this case. Either leave lpVerb as null or set it to edit.

  • You need to use SEE_MASK_NOASYNC since your example has no Win32 message loop.

You also likely want to add SEE_MASK_FLAG_NO_UI to avoid that message box in case of errors so your program can handle them instead.

Related