Windows Registry (1/1)

Introduction

The Windows Registry is a system-defined database in which applications and system components store and retrieve configuration data [1].

In this tutorial we will show a few examples of how to use the Windows API to query and update the Windows Registry.

RegGetValue Example 1

The full example is available from our Codeplex repository at Windows/Registry/RegGetValue1.

File: Windows/Registry/RegGetValue1/Source/Main.cpp

#include <Windows.h>
#include <iostream>

int main(int argc, char* argv[])
{
    DWORD type;
    wchar_t data[10*1024];
    DWORD dataSize = 10*1024 * sizeof(wchar_t);
    
    LONG result = RegGetValue(
        HKEY_CURRENT_USER,    // Key
        L"Environment",       // Subkey
        L"PATH",              // Name of the registry value
        RRF_RT_REG_SZ,        // Flags: Restrict type to REG_SZ 
        &type,                // Type of the value
        data,                 // Buffer were the data will be stored
        &dataSize             // Size of the buffer. Contains the size
                              // of the data copied in the buffer when 
                              // the function returns.
        );

    if (result == ERROR_SUCCESS)
    {
        std::wcout << L"Key type: " << type << std::endl;
        std::wcout << L"Key data: " << data << std::endl;
    }
    else
    {
        std::wcout << L"Failed to get value" << std::endl;
    }

    return 0;
}

RegGetValue Example 2

The full example is available from our Codeplex repository at Windows/Registry/RegGetValue2.

File: Windows/Registry/RegGetValue2/Source/Main.cpp

#include <Windows.h>
#include <iostream>

int main(int argc, char* argv[])
{
    DWORD type;
    wchar_t data[10*1024];
    DWORD dataSize = 10*1024 * sizeof(wchar_t);

    HKEY systemRegKey;
    LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SYSTEM", 
        0, KEY_QUERY_VALUE, &systemRegKey);
    if (result != ERROR_SUCCESS)
    {
        std::wcout << "Failed to open SYSTEM key" << std::endl;
        return 0;
    }

    HKEY currentControlSetRegKey;
    result = RegOpenKeyEx(systemRegKey, L"CurrentControlSet", 
        0, KEY_QUERY_VALUE, &currentControlSetRegKey);
    RegCloseKey(systemRegKey);
    if (result != ERROR_SUCCESS)
    {
        std::wcout << "Failed to open CurrentControlSet key" << std::endl;
        return 0;
    }

    HKEY controlRegKey;
    result = RegOpenKeyEx(currentControlSetRegKey, L"Control", 
        0, KEY_QUERY_VALUE, &controlRegKey);
    RegCloseKey(currentControlSetRegKey);
    if (result != ERROR_SUCCESS)
    {
        std::wcout << "Failed to open Control key" << std::endl;
        return 0;
    }

    HKEY sessionManagerRegKey;
    result = RegOpenKeyEx(controlRegKey, L"Session Manager", 
        0, KEY_QUERY_VALUE, &sessionManagerRegKey);
    RegCloseKey(controlRegKey);
    if (result != ERROR_SUCCESS)
    {
        std::wcout << "Failed to open Session Manager key" << std::endl;
        return 0;
    }

    result = RegGetValue(
        sessionManagerRegKey,  // Key
        L"Environment",        // Subkey
        L"Path",               // Name of the registry value
        RRF_RT_REG_SZ,         // Flags: Restrict type to REG_SZ 
        &type,                 // Type of the value
        data,                  // Buffer were the data will be stored
        &dataSize              // Size of the buffer. Contains the size
                               // of the data copied in the buffer when 
                               // the function returns.
        );

    RegCloseKey(sessionManagerRegKey);

    if (result == ERROR_SUCCESS)
    {
        std::wcout << L"Key type: " << type << std::endl;
        std::wcout << L"Key data: " << data << std::endl;
    }
    else
    {
        std::wcout << L"Failed to get value" << std::endl;
    }

    return 0;
}

References

  1. MSDN: Registry (Windows)

Copyright(c) 2006-2017 Xavier Leclercq | Privacy policy

Home
Contact Us
Search