0

In my software I'm using ShellExecuteEx to open a report that is presented as a local .htm file. At times on some end-user systems there's no default file association for the .htm files.

To set such file association up on a pre-Windows 10 system I'd install the following registry keys to use IE:

Key: HKEY_CURRENT_USER\Software\Classes\htm.file\Shell\open\Command
REG_SZ name: ""
REG_SZ value: "C:\Program Files (x86)\Internet Explorer\iexplore.exe" "%1"

Key: HKEY_CURRENT_USER\Software\Classes\.htm
REG_SZ name: ""
REG_SZ value: htm.file

Key: HKEY_CURRENT_USER\Software\Classes\.htm
REG_SZ name: PerceivedType
REG_SZ value: Document

Then I notify Windows Explorer of the change:

SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);

It works well, but how do you do the same for Microsoft Edge on Windows 10?

PS.:
I don't want to use IE there because it always shows an extra tab with the nag to switch to Edge, which is very confusing for my end-users.

TylerH
  • 20,799
  • 66
  • 75
  • 101
c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • You could instead execute Edge with the html file as parameter – bolov Oct 17 '18 at 19:58
  • 2
    Having your software modify user's associations is very probably a bad idea. Likely your program will do harm in the eye's of some users. I'd be inclined to detect the failure of ShellExecuteEx and then open Edge directly. Of course Edge may not be present. – David Heffernan Oct 17 '18 at 20:09
  • Windows 10 does not allow applications to arbitrarily change file associations. It has to be done via Settings by the user. If you need evidence, install Google Chrome on a Windows 10 system where it has not been installed before and tell the installer you want to set Chrome as the default browser; in doing so, it opens the appropriate Settings page for you to do so yourself. In addition, I've just been involved in the manual migration of more than 25 systems from Windows 7 to Windows 10 Enterprise, including some fresh installs, with not a single one missing an .htm association. – Ken White Oct 17 '18 at 20:17
  • @KenWhite: I hope you're right. Because it's very frustrating to deal with missing file associations for the basic extensions like `.htm`. Although, please note that I'm not talking about changing the default web browser. This is about opening a local file with the local path that has `.htm` extension. PS. Going back to what you described, I guess that anti-trust lawsuit in the late 90's didn't teach MS anything. hah. – c00000fd Oct 17 '18 at 20:30
  • "*Windows 10 does not allow applications to arbitrarily change file associations*" - In fact, Raymond Chen just blogged about that yesterday: [Why does Windows ask me to pick a program with which to open a file, even when I already specified which program I want to use to open the file?](https://blogs.msdn.microsoft.com/oldnewthing/20181016-00/?p=99985) – Remy Lebeau Oct 17 '18 at 20:31
  • @DavidHeffernan: I wish it was that easy. ShellExecute is a hairball of hurt. It may or sometimes may not return an error code. [I can't even make it replicate](https://stackoverflow.com/questions/52845413/how-to-replicate-shellexecuteex-failure-with-error-no-association-error) the error message I received in a report. Plus, if there's no associations it returns success and instead shows [this popup](https://i.stack.imgur.com/8Q2OQ.png) and there's no way of knowing what the user chose or didn't choose there from my app. Or even worse, [it can be this](https://i.imgur.com/lvcxXn9.png) on Win7. – c00000fd Oct 17 '18 at 20:35
  • @RemyLebeau: Oh wow, an article from yesterday. I didn't know that he was still writing those! Quite a good timing on my part, hah :) – c00000fd Oct 17 '18 at 20:44
  • If I were you I'd pass the buck to the user to fix their associations. Sometimes the best way to solve a problem is assign it to the user!! You. Simply can't be expected to deal with this. Explain to your user that they need to configure the machine and get on with some real work. – David Heffernan Oct 17 '18 at 21:23
  • @c00000fd he still writes new blog posts very frequently, at least several a week, many times even daily – Remy Lebeau Oct 17 '18 at 21:47
  • @DavidHeffernan: If my end-users were anything like users on SO, I would not have any issues to do what you suggested. Unfortunately that's not the case. Plus, there's no reliable way to know whether ShellExecute would fail or return success and instead display that app picker window of its own that I showed above. And when the end-user expects to see a report (from my app) this may be very confusing to them. Like in this case, I got a support email saying that reports don't work. In that case ShellExecute returned ERROR_NO_ASSOCIATION that my app displayed as an error MsgBox. – c00000fd Oct 17 '18 at 22:35
  • Unfortunately there's also an oodles more of error codes that it can return if there's no file association. (As I learned in my testing yesterday.) – c00000fd Oct 17 '18 at 22:35
  • @RemyLebeau: You know it's funny. I've been reading his blog in the early 2000's when I was learning Win32. But I don't even know how the dude looks like. Usually MS personas are quite prominent: e.g. Mark Russinovich is all over the web. Something tells me that he is quite ancient. No Twitter or YouTube account. There's someone with that name on SO, but there's really no way to verify if it's actually him. – c00000fd Oct 17 '18 at 22:41
  • 1
    Yeah, it's him. – David Heffernan Oct 17 '18 at 22:55
  • @c00000fd yes, it is the same guy. And images of him are pretty easy to find. I just Google him and found several Microsoft dev videos of him. – Remy Lebeau Oct 18 '18 at 03:12
  • For what it's worth, you can help the end user do it by himself quite a bit with different tricks, depending on Windows version: https://stackoverflow.com/a/52198802/403671 Using UI Automation, I guess it's even possible to change associations visually but I've not been that far. – Simon Mourier Oct 18 '18 at 07:44

1 Answers1

0

On Windows 10, it is done with IApplicationAssociationRegistrationInternal ("2a848e25-d688-4aa3-8e55-0c16cb3a2dfb")

created with SHCreateAssociationRegistration

Set "AppX4hxtad77fbk3jkkeerkrm0ze94wjf3s9" for Microsoft Edge ProgId

(tested on Windows 10 - 1803, 17134.820)

Castorix
  • 1,465
  • 1
  • 9
  • 11