by_library/windows_registry/enum_values/enum_values.cpp
[C++ only] Illustrates use of the
Windows Registry library component
reg_value_sequence (and the associated component
reg_value), by enumerating the environment variables defined for the current user. The name (obtained via
name()) of each registry value is printed out, followed by its value, as follows:
- If the value is a path list (it contains two or more paths separated by semicolons), then the value (obtained via value_sz()) is tokenised into individual paths that are printed in an indented list.
- If the value contains an environment variable (it contains the % character), then the value (obtained via value_sz()) is printed, followed by the actual value (obtained via value_expand_sz()), wherein all environment variable references are resolved.
- Otherwise the value (obtained via value_sz()) is printed.
#include <winstl/registry/reg_value_sequence.hpp>
#include <stlsoft/iterators/ostream_iterator.hpp>
#include <stlsoft/string_tokeniser.hpp>
#include <algorithm>
#include <exception>
#include <iostream>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
#include <stdlib.h>
int main()
{
try
{
cout << "This program demonstrates use of the basic_reg_value_sequence" << endl
<< " class template by enumerating all the values of the environment" << endl
<< " for the current user." << endl;
winstl::reg_value_sequence values(HKEY_CURRENT_USER, "Environment");
winstl::reg_value_sequence::const_iterator b = values.begin();
winstl::reg_value_sequence::const_iterator e = values.end();
size_t n;
for(n = 0; b != e; ++b, ++n)
{
cout << "\t" << (*b).name();
winstl::reg_value val(*b);
winstl::reg_string_t value = val.value_sz();
if(value.end() == std::find(value.begin(), value.end(), ';'))
{
if(value.end() == std::find(value.begin(), value.end(), '%'))
{
cout << "=" << value << endl;
}
else
{
cout << "=" << value << " => " << val.value_expand_sz() << endl;
}
}
else
{
stlsoft::string_tokeniser<winstl::reg_string_t, TCHAR> tokens(value, ';');
cout << endl;
std::copy(tokens.begin(), tokens.end()
, stlsoft::ostream_iterator<winstl::reg_string_t>(cout, "\t\t", "\n"));
}
}
cout << " " << n << " value(s)" << endl;
return EXIT_SUCCESS;
}
catch(std::exception &x)
{
cerr << "Error: " << x.what() << endl;
return EXIT_FAILURE;
}
catch(...)
{
cerr << "Unknown error" << endl;
return EXIT_FAILURE;
}
}