Search code examples
c++winapiwindows-10windows-shell

How to prevent "How do you want to open this file" dialog?


In my app I open a report using HTML file as such:

//pStrPath is file:///C:/Users/appts/AppData/Local/Temp/Report_View.htm
ShellExecute(hParentWnd, L"", pStrPath, NULL, NULL, SW_SHOW);

On my development machine it opens up in a web browser, but when I just tested it on a new installation of Windows 10, it showed this dialog instead:

enter image description here

So how can I prevent it from being shown and go with "keep using this app" option from the get-go? Otherwise it may be very confusing for my users.

PS. Note that Edge is installed and can open .htm files if I double-click them.


Solution

  • Referring to Launching Applications (ShellExecute, ShellExecuteEx, SHELLEXECUTEINFO) we note the text


    Object Verbs

    The verbs available for an object are essentially the items that you find on an object's shortcut menu. To find which verbs are available, look in the registry under HKEY_CLASSES_ROOT\CLSID{object_clsid}\Shell\verb

    Commonly available verbs include:

    • edit - Launches an editor and opens the document for editing.
    • find - Initiates a search starting from the specified directory.
    • open - Launches an application. If this file is not an executable file, its associated application is launched.
    • print - Prints the document file.
    • properties - Displays the object's properties.

    Given that a double-click is the generally equivalent to selecting "open" in the object's shortcut menu, if we supply the function with the open verb, we can expect the behaviour to mirror that of a user's double-click. - Please see Ken's comment below

    As such, we can expect the following code to achieve the desired result.

    //pStrPath is file:///C:/Users/appts/AppData/Local/Temp/Report_View.htm
    ShellExecute(hParentWnd, L"open", pStrPath, NULL, NULL, SW_SHOW);