Registry Functions

The registry is a hierarchical database used by Microsoft Windows to store configuration settings for the operating system, hardware, and applications.

Overview

Registry functions allow applications to create, delete, query, and modify registry keys and values. These functions are part of the Windows API and are typically accessed using C or C++.

Note: Direct manipulation of the registry can be dangerous if not done correctly. Always ensure you have proper error handling and understand the implications of your changes.
Key Registry Functions

Accessing and Querying Keys

RegOpenKeyEx

Opens an existing registry key. If the key does not exist, the function fails. This is often the first step before performing other operations on a key.

LONG RegOpenKeyEx(
        HKEY hKey,
        LPCTSTR lpSubKey,
        DWORD ulOptions,
        REGSAM samDesired,
        PHKEY phkResult
    );

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key. Typically one of the predefined keys (e.g., HKEY_CLASSES_ROOT).
lpSubKey LPCTSTR The name of the subkey to open.
ulOptions DWORD Reserved; must be zero.
samDesired REGSAM An access mask that specifies the desired access rights to the key. Common values include KEY_READ, KEY_WRITE, KEY_ALL_ACCESS.
phkResult PHKEY A pointer to a variable that receives the handle to the opened key.

Return Value:

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

RegQueryValueEx

Retrieves the data and type of a specified registry value. Used to read the data associated with a specific value within a registry key.

LONG RegQueryValueEx(
        HKEY hKey,
        LPCTSTR lpValueName,
        LPDWORD lpReserved,
        LPDWORD lpType,
        LPBYTE lpData,
        LPDWORD lpcbData
    );

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key.
lpValueName LPCTSTR The name of the registry value to query.
lpReserved LPDWORD Reserved; must be NULL.
lpType LPDWORD A pointer to a variable that receives the type of the value.
lpData LPBYTE A pointer to a buffer that receives the value's data.
lpcbData LPDWORD A pointer to a variable that specifies the size, in bytes, of the buffer pointed to by lpData.

Return Value:

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

Modifying and Creating Keys

RegCreateKeyEx

Creates a new registry key or opens an existing one. This function provides more control than RegCreateKey, allowing specification of security attributes and key state.

LONG RegCreateKeyEx(
        HKEY hKey,
        LPCTSTR lpSubKey,
        DWORD Reserved,
        LPCTSTR lpClass,
        DWORD dwOptions,
        REGSAM samDesired,
        LPSECURITY_ATTRIBUTES lpSecurityAttributes,
        PHKEY phkResult,
        LPDWORD lpDisposition
    );

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key.
lpSubKey LPCTSTR The name of the subkey to create or open.
Reserved DWORD Reserved; must be zero.
lpClass LPCTSTR The user-defined class type of the key. Can be NULL.
dwOptions DWORD Special options for the key. E.g., REG_OPTION_VOLATILE.
samDesired REGSAM An access mask specifying the desired access rights.
lpSecurityAttributes LPSECURITY_ATTRIBUTES Security attributes for the key. Can be NULL.
phkResult PHKEY Pointer to the handle of the new or opened key.
lpDisposition LPDWORD Receives information about whether the key was created or opened.

Return Value:

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

RegSetValueEx

Creates or updates a registry value. This function allows you to set the data and type for a registry value.

LONG RegSetValueEx(
        HKEY hKey,
        LPCTSTR lpValueName,
        DWORD Reserved,
        DWORD dwType,
        const BYTE* lpData,
        DWORD cbData
    );

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key.
lpValueName LPCTSTR The name of the registry value to set.
Reserved DWORD Reserved; must be zero.
dwType DWORD The type of data for the value. Common types include REG_SZ, REG_DWORD, REG_BINARY.
lpData const BYTE* A pointer to the buffer containing the data to be stored.
cbData DWORD The size, in bytes, of the data pointed to by lpData.

Return Value:

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

Deleting Keys and Values

RegDeleteKeyEx

Deletes a subkey and its entries from the registry. This function can only delete keys that have no subkeys.

LONG RegDeleteKeyEx(
        HKEY hKey,
        LPCTSTR lpSubKey,
        REGSAM samDesired,
        DWORD Reserved
    );

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key.
lpSubKey LPCTSTR The name of the subkey to delete.
samDesired REGSAM An access mask that specifies the desired access rights to the key.
Reserved DWORD Reserved; must be zero.

Return Value:

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

RegDeleteValue

Deletes a registry value from a registry key.

LONG RegDeleteValue(
        HKEY hKey,
        LPCTSTR lpValueName
    );

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key.
lpValueName LPCTSTR The name of the registry value to delete.

Return Value:

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

Closing Registry Keys

RegCloseKey

Closes a handle to an open registry key. It is important to close keys when you are finished with them to free system resources.

LONG RegCloseKey(
        HKEY hKey
    );

Parameters:

Name Type Description
hKey HKEY A handle to the registry key to close.

Return Value:

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

Always call RegCloseKey on handles returned by functions like RegOpenKeyEx and RegCreateKeyEx to prevent resource leaks.