Home  >  Article  >  Backend Development  >  How Do I Create Registry Entries for File Association in C ?

How Do I Create Registry Entries for File Association in C ?

Barbara Streisand
Barbara StreisandOriginal
2024-11-24 00:24:13714browse

How Do I Create Registry Entries for File Association in C  ?

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:

  • MSDN: Best Practices for File Association: https://docs.microsoft.com/en-us/windows/win32/shell/best-practices-for-file-associations
  • File Types and File Association: https://docs.microsoft.com/en-us/windows/win32/shell/file-types-and-file-associations

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn