Windows Vista, the default program APIs, file format associations, and (un) installers are an explosive mixture!

My application is well-established Windows, so when I ported it to Windows Vista / 7, I replaced the user-defined file format association code with the default program support. However, I encountered a problem when trying to make an uninstaller for my application - it seems that there is no way to associate formats to delete through the default program APIs.

I tried calling IApplicationAssociationRegistration :: ClearUserAssociations, but actually deletes all associations, including for other applications, by completely restoring the OS to its default state (which, of course, is unacceptable).

I tried calling IApplicationAssociationRegistration :: SetAppAsDefault to return the file format associations to the previous "owner" - but this does not help, because my application processes many unique file formats that the OS does not support, and there are no previous "owners". And Windows doesn't allow passing empty strings to SetAppAsDefault ...

So what should I do? Any good solutions?

+4
source share
4 answers

Side note . All of the following considerations apply even if you directly modify file associations in the registry, rather than using the default program APIs.

At the first start, your application should collect the previous owners of all file types for which they exist, through IApplicationAssociationRegistration::QueryCurrentDefault and save them stored in your application.

When uninstalling, your application should use IApplicationAssociationRegistration::SetAppAsDefault to try to restore any file association to which it still belongs to the previous owner. For associations, your application still owns, but does not know the previous owners, go to the HKCR and delete the corresponding extension, protocol or MIME type entry. Do not touch any associations that your application is not the current owner - you will rewrite the user's choice.

Of course, I want the batch backup on first start and cleanup on uninstall to be provided as one call to the API programs API by default, but so far they have not decided to generalize this behavior for all applications, you are on your own.

Please note that cleaning up your application upon uninstallation will be specific to uninstalling the user. Any other users who could use the application and change their default values ​​will not be cleared.

You can automate the cleanup for each user by adding a simple task for each user that performs the above steps in the Task Scheduler. The task is scheduled to be performed once, and then removed from the task scheduler. The only potential problem with this approach is that since you do not know how many users will be there, you will not be able to find out when to remove the DLL for this task from the computer. Again, if you leave this DLL in the ProgramData folder, it does not really matter.

0
source

I think that you are using the default program API incorrectly. If I understand correctly, the functionality of the default programs was added by Microsoft due to legal requirements for replacing Internet Explorer as the default browser. It offers a different set of functionality than the usual file associations used in applications. If you have a simple association of files for registration, I would advise you to adhere to the old behavior.

From MSDN: Default Programs (Windows) :
The default programs are primarily intended for applications that use standard file types, such as .mp3 or .jpg files, or standard protocols, such as HTTP or mailto. Applications that use their own proprietary protocols and file associations usually do not use the functionality of Standard programs .

+1
source

The correct code to associate your file extension with the application uses the Windows registry settings. Usually this is done for the entire machine (regardless of the user) using the registry bush HKEY_LOCAL_MACHINE\SOFTWARE\Classes , which is also more conveniently accessible using the alias HKEY_CLASSES_ROOT (registry).

Your process includes three steps:

  • Keeping the "old" settings before installing the application (By the way, it's good that you do this. Many applications simply delete the display at all!)

  • Create your own associations. A good example of how to do this is Changing file associations using the registry editor . Detailed explanation relates to MSDN:

  • When deleting, restore the saved β€œold” settings from step (1) and overwrite the set values ​​using these initial ones. If there were no "old" settings, good citizenship would simply mean removing the class key from the registry.

One tool to facilitate debugging and view changes to class mappings (extensions) is in the FileAsoc Windows File Association Editor . It allows you to recover file extensions while debugging your application. The webpage also provides a brief description of how the values ​​are accurately stored.

Hope this helps!

0
source

Instead of creating file associations in your application, make them in the installer. Using WiX , you can create an installer that establishes file associations during installation and deletes them when deleted

0
source

Source: https://habr.com/ru/post/1304907/


All Articles