From 1a2aa64af60b7b7097b3aaf898cddd2cffdfead8 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 10 Aug 2015 15:47:30 +1000 Subject: [PATCH] win32: add registry wrapper --- Makefile.am | 4 +- win32/registry.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++ win32/registry.hpp | 42 +++++++++++++++++++ 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 win32/registry.cpp create mode 100644 win32/registry.hpp diff --git a/Makefile.am b/Makefile.am index 6f1ed575..14f5d4c0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -270,7 +270,9 @@ if PLATFORM_WIN32 UTIL_FILES += \ backtrace_null.cpp \ io_win32.cpp \ - time_win32.cpp + time_win32.cpp \ + win32/registry.hpp \ + win32/registry.cpp endif ############################################################################### diff --git a/win32/registry.cpp b/win32/registry.cpp new file mode 100644 index 00000000..62da94f7 --- /dev/null +++ b/win32/registry.cpp @@ -0,0 +1,102 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2015 Danny Robson + */ + +#include "registry.hpp" + +#include "../debug.hpp" +#include "../except.hpp" + +#include +#include + +using util::win32::key; + + +/////////////////////////////////////////////////////////////////////////////// +typedef uint64_t QWORD; + +//----------------------------------------------------------------------------- +template constexpr DWORD type_to_id (void); + +template <> constexpr DWORD type_to_id (void) { return REG_DWORD; } +template <> constexpr DWORD type_to_id (void) { return REG_QWORD; } +template <> constexpr DWORD type_to_id (void) { return REG_NONE; } +template <> constexpr DWORD type_to_id (void) { return REG_SZ; } + + +//----------------------------------------------------------------------------- +template constexpr DWORD restrict_to_id (void); + +template <> constexpr DWORD restrict_to_id (void) { return RRF_RT_DWORD; } +template <> constexpr DWORD restrict_to_id (void) { return RRF_RT_QWORD; } +template <> constexpr DWORD restrict_to_id (void) { return RRF_RT_REG_NONE; } +template <> constexpr DWORD restrict_to_id (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); +} + + +//----------------------------------------------------------------------------- +key::~key () +{ + auto err = RegCloseKey (m_handle); + win32_error::try_code (err); +} + + +/////////////////////////////////////////////////////////////////////////////// +template +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 (), nullptr, &value, &size); + win32_error::try_code (err); + + return value; +} + + +//----------------------------------------------------------------------------- +std::set +key::values (void) const +{ + std::set 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); + + CHECK_GT (size, 0); + name.resize (size - 1); + + all.insert (std::move (name)); + } +} diff --git a/win32/registry.hpp b/win32/registry.hpp new file mode 100644 index 00000000..18787f9a --- /dev/null +++ b/win32/registry.hpp @@ -0,0 +1,42 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2015 Danny Robson + */ + +#ifndef __UTIL_WIN32_REGISTRY_HPP +#define __UTIL_WIN32_REGISTRY_HPP + +#include + +#include +#include + +namespace util { namespace win32 { + class key { + public: + key (HKEY root, const char *child, REGSAM rights = KEY_READ); + ~key (); + + template + T data (const char *name = nullptr) const; + + std::set + values (void) const; + + private: + HKEY m_handle; + }; +} } + +#endif