views:

58

answers:

1

I am referring specifically to windows 7.

I have code that associates a certain extension with my application as proposed by webJose on the following page: What registry keys are responsible for file extension association? (However i correctly write to HKEY_CURRENT_USER\Software\Classes instead of HKEY_CLASSES_ROOT as suggested)

The above works initially, or if there are no other programs associated with the extension. However after using the Windows 7 built-in "Choose default program..." (found under the file-right-click context menu under "Open with") it re-associates the extension with whatever new program you choose.

What happens at this point is that "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\\UserChoice" is changed by the system, and so the newly selected program takes over.

Running the above code, to regain control over the extension will not work. The only way to regain control, is by either:

  1. Editing the UserChoice -> Progid value, which is not allowed (neither programmatically nor using regedit.exe - access denied).
  2. Or deleting the UserChoice value and making sure your application is the first in the MRUList value under \OpenWithList (this can be achieved using regedit.exe but not programmatically)

My question is: Is there a way to achieve this programmatically? What registry values can be changed to regain control of an extension, after is associated with another program?

I know it might seem obvious that if a user through explorer sets the associated application to an extension, that it would be expected to do it the same way again to re-associate the extension to a different application.

The problem however is I have a button in my application that uses the above mentioned code to check for extension association with my application. Unfortunately with the above situation, my application displays a message confirming that the extension is already successfully associated when its not! So is there a way around this?

A: 

Deleting UserChoice should revert the default program to the standard file association keys (which starts with the ProgID in HKCU). Barring that you could also delete OpenWithList, which would be reverting with extreme prejudice.

Edit: Check out Registry Key Security and Access Rights on MSDN, particularly the RegSetKeySecurity function. Remember that you'll need to grant yourself administrative control to the key before you can delete it.

Factor Mystic
You cannot delete the key in any way programmatically, even with administrator writes. Which is the reason for my above question, how can i delete it?
Tamer
That's a peculiarity of your system, not a Windows standard. Some software you've installed has changed the acl on the key to prevent you from changing it... probably whatever has registered itself under UserChoice. You should be able to grant yourself access, however, then delete it.
Factor Mystic
Can u please confirm this? I have now tried this on several machines running the application as an administrator. Access to that specific key "UserChoice" is denied programmatically (neither write nor delete). Are u saying that u can programmatically edit the values of this key on ur machine? Can someone please confirm this?
Tamer
Try taking ownership of the key before changing the permissions. If it's owned by SYSTEM, there's no way to change permissions if you're not granted them in the first place. Taking ownership will fix that.
BillP3rd
Taking ownership of the key worked. I use the code at the following link: http://www.tomshardware.com/forum/170289-46-programmatically-inheritable-permissions specifically the function: BOOL setPermissionsExample1()
Tamer