1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
// ----------------------------------------------------------------------------
// Copyright (C) 2002-2005 Marcin Kalicinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see www.boost.org
// ----------------------------------------------------------------------------
#ifndef BOOST_PROPERTY_TREE_REGISTRY_PARSER_HPP_INCLUDED
#define BOOST_PROPERTY_TREE_REGISTRY_PARSER_HPP_INCLUDED
// Include minimal version of windows.h if not included yet
#ifndef _WINDOWS_
#ifndef NOMINMAX
#define NOMINMAX
#endif
#define STRICT
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#define NOGDICAPMASKS
#define NOVIRTUALKEYCODES
#define NOWINMESSAGES
#define NOWINSTYLES
#define NOSYSMETRICS
#define NOMENUS
#define NOICONS
#define NOKEYSTATES
#define NOSYSCOMMANDS
#define NORASTEROPS
#define NOSHOWWINDOW
#define OEMRESOURCE
#define NOATOM
#define NOCLIPBOARD
#define NOCOLOR
#define NOCTLMGR
#define NODRAWTEXT
#define NOGDI
#define NOKERNEL
#define NOUSER
#define NONLS
#define NOMB
#define NOMEMMGR
#define NOMETAFILE
#define NOMSG
#define NOOPENFILE
#define NOSCROLL
#define NOSERVICE
#define NOSOUND
#define NOTEXTMETRIC
#define NOWH
#define NOWINOFFSETS
#define NOCOMM
#define NOKANJI
#define NOHELP
#define NOPROFILER
#define NODEFERWINDOWPOS
#define NOMCX
#include <windows.h>
#endif
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/detail/ptree_utils.hpp>
#include <boost/cstdint.hpp> // for 64 bit int
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <stdexcept>
namespace boost { namespace property_tree { namespace registry_parser
{
//! Registry parser error
class registry_parser_error: public ptree_error
{
public:
// Construct error
registry_parser_error(const std::string &message, DWORD windows_error):
ptree_error(format_what(message, windows_error)),
m_windows_error(windows_error)
{
}
// Get windows error
DWORD windows_error()
{
return m_windows_error;
}
private:
DWORD m_windows_error;
// Format error message to be returned by std::runtime_error::what()
std::string format_what(const std::string &message,
DWORD windows_error)
{
std::stringstream stream;
if (windows_error)
stream << message << " (windows error 0x" << std::hex << windows_error << ")";
else
stream << message;
return stream.str();
}
};
// Translate from binary buffer to string
template<class Ch>
std::basic_string<Ch> translate(DWORD type, const std::vector<BYTE> &data)
{
typedef std::basic_string<Ch> Str;
typedef std::basic_stringstream<Ch> Stream;
Str value;
switch (type)
{
// No data
case REG_NONE:
break;
// Binary data
case REG_BINARY:
if (!data.empty())
{
Stream stream;
stream << std::hex << std::setfill(Ch('0'));
for (std::vector<BYTE>::const_iterator it = data.begin(), end = data.end();
it != end; ++it)
stream << std::setw(2) << static_cast<int>(*it) << Ch(' ');
value = stream.str();
value.resize(value.size() - 1); // remove final space
}
break;
// DWORD value
case REG_DWORD:
if (!data.empty())
{
Stream stream;
stream << *reinterpret_cast<const DWORD *>(&data.front());
value = stream.str();
}
break;
// QWORD value
case REG_QWORD:
if (!data.empty())
{
Stream stream;
stream << *reinterpret_cast<const boost::uint64_t *>(&data.front());
value = stream.str();
}
break;
// Zero terminated string
case REG_SZ: case REG_EXPAND_SZ:
if (!data.empty())
value.assign(reinterpret_cast<const Ch *>(&data.front()));
break;
// Unknown data type
default:
throw registry_parser_error("unsupported data type", 0);
};
return value;
}
// Translate from string to binary buffer
template<class Ch>
std::vector<BYTE> translate(DWORD type, const std::basic_string<Ch> &s)
{
typedef std::basic_string<Ch> Str;
typedef std::basic_stringstream<Ch> Stream;
std::vector<BYTE> data;
switch (type)
{
// No data
case REG_NONE:
break;
// Binary data
case REG_BINARY:
{
int v;
Stream stream(s);
stream >> std::hex;
while (1)
{
stream >> v >> std::ws;
if (stream.fail() || stream.bad())
throw registry_parser_error("bad REG_BINARY value", 0);
data.push_back(v);
if (stream.eof())
break;
}
}
break;
// DWORD value
case REG_DWORD:
{
DWORD v;
Stream stream(s);
stream >> v >> std::ws;
if (!stream.eof() || stream.fail() || stream.bad())
throw registry_parser_error("bad REG_DWORD value", 0);
for (size_t i = 0; i < sizeof(v); ++i)
data.push_back(*(reinterpret_cast<BYTE *>(&v) + i));
}
break;
// QWORD value
case REG_QWORD:
{
boost::uint64_t v;
Stream stream(s);
stream >> v;
if (!stream.eof() || stream.fail() || stream.bad())
throw registry_parser_error("bad REG_QWORD value", 0);
for (size_t i = 0; i < sizeof(v); ++i)
data.push_back(*(reinterpret_cast<BYTE *>(&v) + i));
}
break;
// Zero terminated string
case REG_SZ: case REG_EXPAND_SZ:
{
const Ch *sz = s.c_str();
size_t len = (s.size() + 1) * sizeof(Ch);
for (size_t i = 0; i < len; ++i)
data.push_back(*(reinterpret_cast<const BYTE *>(sz) + i));
}
break;
// Unknown data type
default:
throw registry_parser_error("unsupported data type", 0);
};
return data;
}
/////////////////////////////////////////////////////////////////////////////
// Registry functions wrappers
template<class Ch>
inline LONG reg_create_key_ex(HKEY hkey, const Ch *subkey, REGSAM sam, HKEY *result);
template<>
inline LONG reg_create_key_ex<char>(HKEY hkey, const char *subkey, REGSAM sam, HKEY *result)
{
return RegCreateKeyExA(hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE, sam, NULL, result, NULL);
}
template<>
inline LONG reg_create_key_ex<wchar_t>(HKEY hkey, const wchar_t *subkey, REGSAM sam, HKEY *result)
{
return RegCreateKeyExW(hkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE, sam, NULL, result, NULL);
}
template<class Ch>
inline LONG reg_set_value_ex(HKEY hkey, const Ch *name, DWORD type, const BYTE *data, DWORD size);
template<>
inline LONG reg_set_value_ex<char>(HKEY hkey, const char *name, DWORD type, const BYTE *data, DWORD size)
{
return RegSetValueExA(hkey, name, 0, type, data, size);
}
template<>
inline LONG reg_set_value_ex<wchar_t>(HKEY hkey, const wchar_t *name, DWORD type, const BYTE *data, DWORD size)
{
return RegSetValueExW(hkey, name, 0, type, data, size);
}
template<class Ch>
inline LONG reg_open_key_ex(HKEY hkey, const Ch *subkey, REGSAM sam, HKEY *result);
template<>
inline LONG reg_open_key_ex<char>(HKEY hkey, const char *subkey, REGSAM sam, HKEY *result)
{
return RegOpenKeyExA(hkey, subkey, 0, sam, result);
}
template<>
inline LONG reg_open_key_ex<wchar_t>(HKEY hkey, const wchar_t *subkey, REGSAM sam, HKEY *result)
{
return RegOpenKeyExW(hkey, subkey, 0, sam, result);
}
template<class Ch>
inline LONG reg_enum_key_ex(HKEY hkey, DWORD index, Ch *name, DWORD *size);
template<>
inline LONG reg_enum_key_ex<char>(HKEY hkey, DWORD index, char *name, DWORD *size)
{
FILETIME ft;
return RegEnumKeyExA(hkey, index, name, size, 0, NULL, NULL, &ft);
}
template<>
inline LONG reg_enum_key_ex<wchar_t>(HKEY hkey, DWORD index, wchar_t *name, DWORD *size)
{
FILETIME ft;
return RegEnumKeyExW(hkey, index, name, size, 0, NULL, NULL, &ft);
}
template<class Ch>
inline LONG reg_enum_value(HKEY hkey, DWORD index, Ch *name, DWORD *name_size, DWORD *type, BYTE *data, DWORD *data_size);
template<>
inline LONG reg_enum_value<char>(HKEY hkey, DWORD index, char *name, DWORD *name_size, DWORD *type, BYTE *data, DWORD *data_size)
{
return RegEnumValueA(hkey, index, name, name_size, NULL, type, data, data_size);
}
template<>
inline LONG reg_enum_value<wchar_t>(HKEY hkey, DWORD index, wchar_t *name, DWORD *name_size, DWORD *type, BYTE *data, DWORD *data_size)
{
return RegEnumValueW(hkey, index, name, name_size, NULL, type, data, data_size);
}
template<class Ch>
inline LONG reg_query_info_key(HKEY hkey, DWORD *max_subkey_len, DWORD *max_name_len, DWORD *max_value_len);
template<>
inline LONG reg_query_info_key<char>(HKEY hkey, DWORD *max_subkey_len, DWORD *max_name_len, DWORD *max_value_len)
{
return RegQueryInfoKeyA(hkey, NULL, NULL, NULL, NULL, max_subkey_len, NULL, NULL, max_name_len, max_value_len, NULL, NULL);
}
template<>
inline LONG reg_query_info_key<wchar_t>(HKEY hkey, DWORD *max_subkey_len, DWORD *max_name_len, DWORD *max_value_len)
{
return RegQueryInfoKeyW(hkey, NULL, NULL, NULL, NULL, max_subkey_len, NULL, NULL, max_name_len, max_value_len, NULL, NULL);
}
/////////////////////////////////////////////////////////////////////////////
// Registry key handle wrapper
template<class Ch>
class reg_key
{
public:
typedef std::basic_string<Ch> Str;
reg_key(HKEY root, const std::basic_string<Ch> &key, bool create):
hkey(0)
{
if (create)
{
LONG result = reg_create_key_ex(root, key.c_str(), KEY_WRITE, &hkey);
if (result != ERROR_SUCCESS)
throw registry_parser_error("RegCreateKeyEx failed", result);
}
else
{
LONG result = reg_open_key_ex(root, key.c_str(), KEY_READ, &hkey);
if (result != ERROR_SUCCESS)
throw registry_parser_error("RegOpenKeyEx failed", result);
}
BOOST_ASSERT(hkey);
}
~reg_key()
{
BOOST_ASSERT(hkey);
RegCloseKey(hkey);
}
HKEY handle()
{
BOOST_ASSERT(hkey);
return hkey;
}
private:
HKEY hkey;
};
/////////////////////////////////////////////////////////////////////////////
// Registry parser
//! Read registry
template<class Ptree>
void read_registry(HKEY root,
const std::basic_string<typename Ptree::char_type> &key,
Ptree &pt)
{
typedef typename Ptree::char_type Ch;
typedef std::basic_string<Ch> Str;
typedef std::basic_stringstream<Ch> Stream;
Ptree local;
// Open key
reg_key<Ch> rk(root, key, false);
// Query key info
DWORD max_subkey_len, max_name_len, max_value_len;
LONG result = reg_query_info_key<Ch>(rk.handle(), &max_subkey_len, &max_name_len, &max_value_len);
if (result != ERROR_SUCCESS)
throw registry_parser_error("RegQueryInfoKey failed", result);
// For all subkeys
std::vector<Ch> subkey(max_subkey_len + 1);
for (DWORD index = 0; true; ++index)
{
// Get subkey name
DWORD size = static_cast<DWORD>(subkey.size());
LONG result = reg_enum_key_ex(rk.handle(), index, &subkey.front(), &size);
if (result == ERROR_NO_MORE_ITEMS)
break;
if (result != ERROR_SUCCESS)
throw registry_parser_error("RegEnumKeyEx failed", result);
// Parse recursively
Ptree &child = local.push_back(typename Ptree::value_type(&subkey.front(), Ptree()))->second;
read_registry<Ptree>(rk.handle(), &subkey.front(), child);
}
// For all values
for (DWORD index = 0; true; ++index)
{
// Resize data to max size
std::vector<Ch> name(max_name_len + 1);
std::vector<BYTE> data(max_value_len + 1);
// Get name and value from registry
DWORD name_size = static_cast<DWORD>(name.size());
DWORD data_size = static_cast<DWORD>(data.size());
DWORD type;
result = reg_enum_value<Ch>(rk.handle(), index, &name.front(), &name_size, &type, &data.front(), &data_size);
if (result == ERROR_NO_MORE_ITEMS)
break;
if (result != ERROR_SUCCESS)
throw registry_parser_error("RegEnumValue failed", result);
// Truncate data to actual size
name.resize(name_size + 1);
data.resize(data_size);
// Translate and put value in tree
Str value = translate<Ch>(type, data);
if (name_size > 0)
{
local.put(Str(detail::widen<Ch>("\\values.") + &name.front()), value);
local.put(Str(detail::widen<Ch>("\\types.") + &name.front()), type);
}
else
local.data() = value;
}
// Swap pt and local
pt.swap(local);
}
//! Write registry
template<class Ptree>
void write_registry(HKEY root,
const std::basic_string<typename Ptree::char_type> &key,
const Ptree &pt)
{
typedef typename Ptree::char_type Ch;
typedef std::basic_string<Ch> Str;
typedef std::basic_stringstream<Ch> Stream;
// Create key
reg_key<Ch> rk(root, key, true);
// Set default key value
if (!pt.data().empty())
{
std::vector<BYTE> data = translate<Ch>(REG_SZ, pt.data());
reg_set_value_ex<Ch>(rk.handle(), NULL, REG_SZ,
data.empty() ? NULL : &data.front(),
static_cast<DWORD>(data.size()));
}
// Create values
const Ptree &values = pt.get_child(detail::widen<Ch>("\\values"), empty_ptree<Ptree>());
const Ptree &types = pt.get_child(detail::widen<Ch>("\\types"), empty_ptree<Ptree>());
for (typename Ptree::const_iterator it = values.begin(), end = values.end(); it != end; ++it)
{
DWORD type = types.get(it->first, REG_SZ);
std::vector<BYTE> data = translate<Ch>(type, it->second.data());
reg_set_value_ex<Ch>(rk.handle(), it->first.c_str(), type,
data.empty() ? NULL : &data.front(),
static_cast<DWORD>(data.size()));
}
// Create subkeys
for (typename Ptree::const_iterator it = pt.begin(), end = pt.end(); it != end; ++it)
if (&it->second != &values && &it->second != &types)
write_registry(rk.handle(), it->first, it->second);
}
} } }
namespace boost { namespace property_tree
{
using registry_parser::read_registry;
using registry_parser::write_registry;
using registry_parser::registry_parser_error;
} }
#endif
|