Create a registry entry to associate a file extension with an application in C ++

I would like to know the cleanest way to register a file extension with my C ++ application, so when the data file associated with my program is double-clicked, the application opens and the file name is passed as a parameter to the application.

I am currently doing this through my wix installer, but there are some cases where the application will not be installed on the user's computer, so I also need the ability to create a registry key using the application.

In addition, will this also mean that if the application is deleted, unused entries in the registry will remain lying?

+40
c ++ file-extension registry
07 Sep '09 at 6:07
source share
4 answers

Your basic process overview is in this MSDN article . The key parts are at the bottom of the list:

  • Register ProgID

A progID (essentially a file type registry key) is what contains important file type properties, such as an icon, description, and context menu items, including the application used to double-click a file. Many extensions may have the same file type. This mapping is performed in the next step:

  • Register the file name extension for the file type

Here you set the registry value for your extension by setting the extension file type to ProgID, which you created in the previous step.

The minimum amount of work required to open a file in your application is to install / create two registry keys. In this example .reg file, I create a file type ( blergcorp.blergapp.v1 ) and associate the file extension ( blergcorp.blergapp.v1 ) with it.

 Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command] @="c:\path\to\app.exe \"%1\"" [HKEY_CURRENT_USER\Software\Classes\.blerg] @="blergcorp.blergapp.v1" 

Now you probably want to do this programmatically. To be absolutely kosher, you can check for the presence of these keys and change the behavior of your program accordingly, especially if you take control of some general file extension. However, the goal can be achieved by setting these two keys using the SetValue function.

I'm not sure about the exact syntax of C ++, but in C # the syntax looks something like this:

 Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command", null, @"c:\path\to\app.exe \"%1\""); Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\.blerg", null, "blergcorp.blergapp.v1"); 

Of course, you can manually open each nested key, manually create a ProgID and an additional extension key, and then set the key value, but the nice SetValue function of SetValue is that if the keys or values ​​do not exist, they will be created automatically., Very useful.

Now a short word about which hive to use. Many examples of file matching on the Internet, including on MSDN, show that these keys are set to HKEY_CLASSES_ROOT . I do not recommend doing this. This bush is a unified virtual view of HKEY_LOCAL_MACHINE\Software\Classes (system defaults) and HKEY_CURRENT_USER\Software\Classes (individual user settings), and entries in any subsection in the bush are redirected to the same key in HKEY_LOCAL_MACHINE\Software\Classes . Now there is no direct problem with this, but you may run into this problem: if you write in HKCR (redirected to HKLM), and the user specified the same keys with different values ​​in HKCU, the HKCU values ​​will take precedence. Therefore, your entries will be successful, but you will not see any changes, because the HKEY_CURRENT_USER settings take precedence over the HKEY_LOCAL_MACHINE settings.

Therefore, you should take this into account when developing your application. Now, on the other hand, you can only write to HKEY_CURRENT_USER , as my examples show. However, this file association option will only be downloaded for the current user, and if your application has been installed for all users, it will not start when this other user opens the file in Windows.

This should be a worthy tutorial for what you want to do. For further reading, I suggest

And see also my similar answer to a similar question:

  • Associating file extensions with a program
+74
Jan 15
source share

This is a two-step process:

  1. Define a program that would take care of extension: (unless you want to use existing one)
       1.1 create a key in "HKCU \\ Software \\ Classes \\" for example 
           "Software \\ Classes \\ YourProgramName.file.ext"
       1.2 create subkey "Software \\ Classes \\ YourProgramName.file.ext \\ DefaultIcon"
         1.2.1 set default value ("") to your application full path to get
               icon from resources
       1.3 create a subkey "Software \\ Classes \\ YourProgramName.file.ext \\ Shell \\ OperationName \\ Command"
           OperationName = for example Open, Print or Other
         1.3.1 set default value ("") to your application full path + optional runtime params (filename)

 2.Associate file extension with program.
   2.1 create a key HKCU \\ Software \\ Classes \\. Ext - here goes your extension
   2.2 set default value to the program definition key
     ("YourProgramName.file.ext")

The following is part of a program written in C # that binds a file extension. It is not C ++, but I think it is simple enough to explain itself and AFAIK, it is verv simmilar, if not identical to the code in C ++

one.

 RegistryKey keyPFCTExt0 = Registry.CurrentUser.OpenSubKey("Software\\Classes\\PFCT.file.enc", true); if (keyPFCTExt0 == null) { keyPFCTExt0 = Registry.CurrentUser.CreateSubKey("Software\\Classes\\PFCT.file.enc"); keyPFCTExt0.CreateSubKey("DefaultIcon"); RegistryKey keyPFCTExt0ext = Registry.CurrentUser.OpenSubKey("Software\\Classes\\PFCT.file.enc\\DefaultIcon", true); keyPFCTExt0ext.SetValue("", Application.ExecutablePath +",0"); keyPFCTExt0ext.Close(); keyPFCTExt0.CreateSubKey("Shell\\PFCT_Decrypt\\Command"); } keyPFCTExt0.SetValue("", "PFCT.file.enc"); keyPFCTExt0.Close();
RegistryKey keyPFCTExt0 = Registry.CurrentUser.OpenSubKey("Software\\Classes\\PFCT.file.enc", true); if (keyPFCTExt0 == null) { keyPFCTExt0 = Registry.CurrentUser.CreateSubKey("Software\\Classes\\PFCT.file.enc"); keyPFCTExt0.CreateSubKey("DefaultIcon"); RegistryKey keyPFCTExt0ext = Registry.CurrentUser.OpenSubKey("Software\\Classes\\PFCT.file.enc\\DefaultIcon", true); keyPFCTExt0ext.SetValue("", Application.ExecutablePath +",0"); keyPFCTExt0ext.Close(); keyPFCTExt0.CreateSubKey("Shell\\PFCT_Decrypt\\Command"); } keyPFCTExt0.SetValue("", "PFCT.file.enc"); keyPFCTExt0.Close(); 

2.

 RegistryKey keyPFCTExt1 = Registry.CurrentUser.OpenSubKey("Software\\Classes\\PFCT.file.enc\\Shell\\PFCT_Decrypt\\Command", true); if (keyPFCTExt1 == null) keyPFCTExt1 = Registry.CurrentUser.CreateSubKey("Software\\Classes\\PFCT.file.enc\\Shell\\PFCT_Decrypt\\Command"); keyPFCTExt1.SetValue("", Application.ExecutablePath + " !d %1"); //!d %1 are optional params, here !d string and full file path keyPFCTExt1.Close();
RegistryKey keyPFCTExt1 = Registry.CurrentUser.OpenSubKey("Software\\Classes\\PFCT.file.enc\\Shell\\PFCT_Decrypt\\Command", true); if (keyPFCTExt1 == null) keyPFCTExt1 = Registry.CurrentUser.CreateSubKey("Software\\Classes\\PFCT.file.enc\\Shell\\PFCT_Decrypt\\Command"); keyPFCTExt1.SetValue("", Application.ExecutablePath + " !d %1"); //!d %1 are optional params, here !d string and full file path keyPFCTExt1.Close(); 
+7
Sep 09 '09 at 20:30
source share

I don’t know why people keep saying that HKEY_CURRENT_USER\Software\Classes\<.ext> The default value (which will redirect you to another (software-created) class.

It works, but it will be redefined

 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\<.ext>\UserChoice 

And I believe that Microsoft recommends a second practice, because that is what the built-in "open with" does. The value of the Progid key "in this case is equal to the default value of HKEY_CURRENT_USER\Software\Classes\<.ext> .

+6
Aug 19 '14 at 17:35
source share

I found the following when trying to manipulate associations using C #:

  • hkcu \ software \ microsoft \ windows \ currentVersion \ explorer \ fileexts.reg \ userchoice β†’ for user settings. The values ​​in the openWithProgIds key point to the keys in hkcr.
  • The value hkcr \ xfile \ shell \ open \ muiVerb or hkcr \ xfile \ shell \ open \ command \ default value β†’ affects the open handler. This is the value that contains the path to the program.
  • hkcr \ .x β†’ affects the context menu (new x) among other things related to the menu.

I do not know the C ++ code, but given this information, you should be able to manipulate the registry using the registry API.

0
Apr 30 '19 at 10:56
source share



All Articles