Creating Registry Entries for File Association in C
Introduction
Integrating your application with specific file types requires associating them via registry entries. This ensures that double-clicking a supported file opens your program and passes the filename as an argument.
Step 1: Registering the ProgID (File Type)
At the core of file association is the ProgID, which identifies the file type within the registry. Use the SetValue function to create the ProgID in HKEY_CURRENT_USERSoftwareClasses.
Step 2: Associating the File Extension
Next, associate the target file extension with the ProgID. This is achieved by creating a subkey with the extension name under HKEY_CURRENT_USERSoftwareClasses and setting its value to the ProgID.
Example Code in C
#include <windows.h> // Header for registry functions // Register the ProgID HKEY hkey; LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Classes\YourProgID", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL); // Set the value of the ProgID key RegSetValueEx(hkey, NULL, 0, REG_SZ, (const BYTE *)"Your Description", sizeof("Your Description") + 1); // Associate the file extension RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Classes\.YourExtension", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL); // Set the value of the extension key RegSetValueEx(hkey, NULL, 0, REG_SZ, (const BYTE *)"YourProgID", sizeof("YourProgID") + 1);
Cleanup Considerations
Uninstalling the application will not automatically remove the registry entries. To prevent orphan entries, consider adding a cleanup routine during uninstallation to delete the associated keys.
Additional Resources:
The above is the detailed content of How Do I Create Registry Entries for File Association in C ?. For more information, please follow other related articles on the PHP Chinese website!