EXT Win Library - registry

The win::registry encapsulates an direct access to a registry key.


Interface:

The win::registry provides an access to standard storage provided by the Windows operating system. This storage is dedicated to intrinsic data types and short strings.

The win::registry class provide following interface:

class thread {
    private:
        class intermediate;
        class intermediate_c;

    public:
        registry (bool local, const std::string & str);
        registry (bool local, const std::wstring & str);
        registry (const registry &);
        registry & operator = (const registry &);

        ~registry () throw ();

        const intermediate_c operator [] (const std::wstring &) const;
        intermediate operator [] (const std::wstring &);

        std::string sub (unsigned int) const;
        std::wstring wsub (unsigned int) const;

        void rem (const std::string &);
        void rem (const std::wstring &);
};

First constructor parameter specifies the user-locality of opened registry key (true = HKCU, false = HKLM), the second is the path of the key (for example "Software\Company\Product").

To access the values, use the [] operator with the value name as a parameter. You may assign this expression a value or variable, or assign it to a variable. The registry value types are preserved, thus assigning a registry value to a variable of incompatible type results in ext::runtime_error exception.

Compatibility of variable types is defined as follows:

You can also use the .sub () or .wsub () member function that returns name of n-th subkey. The n is unsigned numeric parameter to the function. If there is less than n subkeys, this member function throws ext::object_not_exists exception.

To delete the subkey, use the .rem () member function and pass the subkey name as a parameter.

The win::registry class will throw the ext::access_denied exception everytime an access is denied to perform requested operation. The ext::runtime_error exception is thrown on another errors, the description is provided in .what () of what could not be done.

You will need to include "ext/win_bits/registry.cpp" into your project and link to shlwapi.dll library in order to use win::registry.

Examples:

#include <ext/win>
#include <ext/c++>

int main () {
    try {
        win::registry reg (true, "Software\\EXT\\Test");
        ...
        try {
            reg ["string"] = "string value";
            reg ["bool"] = true;
            reg ["number"] = 123;
            reg ["double"] = 12.3456;

        } catch (const ext::access_denied &) {
            ... // no write access right
        };
        ...
        try {
            int n = reg ["number"];
            bool b = reg ["bool"];
            double d = reg ["double"];
            std::string s = reg ["string"];

            ...

        } catch (const ext::access_denied &) {
            ... // no read access right
        } catch (const ext::object_not_exists &) {
            ... // no such registry value
        } catch (const std::bad_cast &) {
            ... // value of incompatible type
        };

    } catch (const ext::runtime_error &) {
        ... // cannot open, create or insufficient rights
    };

    return 0;
};

Remarks

If your application is intended to be portable to Windows 95/98/Me, you will need to ensure that the operating system has the Microsoft Layer for Unicode installed before you can use wide-characted strings.

Although both std::string and std::wstring can be used to describe subkey or value name, you cannot mix them when reading or writting the value. For example you cannot use constructions like reg [L"name"] = "value"; or reg ["name"] = L"value";

The const win::registry objects are possible. Such object can be used to read data from the registry but not to update it.


[index]