libcruft-util/win32/registry.cpp

96 lines
2.8 KiB
C++
Raw Normal View History

2015-08-10 15:47:30 +10:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2015-08-10 15:47:30 +10:00
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "registry.hpp"
#include "error.hpp"
2015-08-10 15:47:30 +10:00
#include "../debug.hpp"
#include "../except.hpp"
#include <string>
#include <cstdint>
using cruft::win32::key;
2015-08-10 15:47:30 +10:00
///////////////////////////////////////////////////////////////////////////////
typedef uint64_t QWORD;
//-----------------------------------------------------------------------------
template <typename T> constexpr DWORD type_to_id (void);
template <> constexpr DWORD type_to_id<DWORD> (void) { return REG_DWORD; }
template <> constexpr DWORD type_to_id<QWORD> (void) { return REG_QWORD; }
template <> constexpr DWORD type_to_id<void> (void) { return REG_NONE; }
template <> constexpr DWORD type_to_id<std::string> (void) { return REG_SZ; }
//-----------------------------------------------------------------------------
template <typename T> constexpr DWORD restrict_to_id (void);
template <> constexpr DWORD restrict_to_id<DWORD> (void) { return RRF_RT_DWORD; }
template <> constexpr DWORD restrict_to_id<QWORD> (void) { return RRF_RT_QWORD; }
template <> constexpr DWORD restrict_to_id<void> (void) { return RRF_RT_REG_NONE; }
template <> constexpr DWORD restrict_to_id<std::string> (void) { return RRF_RT_REG_SZ; }
///////////////////////////////////////////////////////////////////////////////
key::key (HKEY root, const char *child, REGSAM rights)
{
auto err = RegOpenKeyEx (root, child, 0, rights, &m_handle);
win32::error::try_code (err);
2015-08-10 15:47:30 +10:00
}
//-----------------------------------------------------------------------------
key::~key ()
{
auto err = RegCloseKey (m_handle);
win32::error::try_code (err);
2015-08-10 15:47:30 +10:00
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
T
key::data (const char *name) const
{
T value;
DWORD type;
DWORD size = sizeof (value);
auto err = RegGetValue (m_handle, name, &value, restrict_to_id<T> (), nullptr, &value, &size);
win32::error::try_code (err);
2015-08-10 15:47:30 +10:00
return value;
}
//-----------------------------------------------------------------------------
std::set<std::string>
key::values (void) const
{
std::set<std::string> all;
for (DWORD i = 0; ; ++i) {
std::string name (255, '\0');
DWORD size = name.size ();
auto err = RegEnumValue (m_handle, i, &name[0], &size, nullptr, nullptr, nullptr, nullptr);
if (ERROR_NO_MORE_ITEMS == err)
return all;
if (ERROR_SUCCESS != err)
win32::error::throw_code (err);
2015-08-10 15:47:30 +10:00
2016-11-17 18:30:27 +11:00
CHECK_GT (size, 0u);
2015-08-10 15:47:30 +10:00
name.resize (size - 1);
all.insert (std::move (name));
}
}