libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
RegistryKey.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #include "RegistryKey.hpp"
22 #include "DumpWinMsg.hpp"
23 
24 #include "../../../core/exception.hpp"
25 
26 using namespace libjmmcg;
27 using namespace NTUtils;
28 
29 /////////////////////////////////////////////////////////////////////////////
30 
32 
33 /////////////////////////////////////////////////////////////////////////////
34 
35 namespace {
36  const TCHAR *error_msgs[]={
37  _T("Registry key error."),
38  _T("~RegistryKey()"),
39  _T("Failed to create the registry key."),
40  _T("NULL parent key."),
41  _T("Failed to open the registry key."),
42  _T("NULL parent key."),
43  _T("Failed to set the value in the registry key.")
44  };
45 }
46 
47 /////////////////////////////////////////////////////////////////////////////
48 
49 RegistryKey::~RegistryKey(void) {
50  if (delete_key) {
51  if (key.m_hKey && ::RegDeleteKey(parent, name.c_str())!=ERROR_SUCCESS) {
52  throw exception_type(error_msgs[1],info::function(__LINE__,__PRETTY_FUNCTION__,typeid(*this),info::function::argument(_T(""),_T(""))),__REV_INFO__);
53  }
54  }
55 }
56 
57 inline
58 long RegistryKey::Create(HKEY p, const tstring &n) {
59  if (p) {
60  name=n;
61  long ret;
62  if ((ret=key.Create(parent=p, name.c_str()))!=ERROR_SUCCESS) {
63  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&RegistryKey::Create),info::function::argument(_T("HKEY parent"),tostring(p)));
64  fn.add_arg(_T("const tstring &n"),n);
65  throw exception_type(error_msgs[2],fn,__REV_INFO__);
66  }
67  delete_key=true;
68  return ret;
69  }
70  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&RegistryKey::Create),info::function::argument(_T("HKEY parent"),tostring(p)));
71  fn.add_arg(_T("const tstring &n"),n);
72  throw exception_type(error_msgs[3],fn,__REV_INFO__);
73 }
74 
75 inline
76 long RegistryKey::Open(HKEY parent, const tstring &n) {
77  delete_key=false;
78  if (parent) {
79  name=n;
80  long ret;
81  if ((ret=key.Open(parent, name.c_str()))!=ERROR_SUCCESS) {
82  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&RegistryKey::Create),info::function::argument(_T("HKEY parent"),tostring(parent)));
83  fn.add_arg(_T("const tstring &n"),n);
84  throw exception_type(error_msgs[4],fn,__REV_INFO__);
85  }
86  return ret;
87  }
88  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&RegistryKey::Create),info::function::argument(_T("HKEY parent"),tostring(parent)));
89  fn.add_arg(_T("const tstring &n"),n);
90  throw exception_type(error_msgs[5],fn,__REV_INFO__);
91 }
92 
93 inline
94 long RegistryKey::SetValue(const tstring &val, const tstring &key_name) {
95  long ret;
96  if ((ret=key.SetStringValue(val.c_str(), key_name.c_str()))!=ERROR_SUCCESS) {
97  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&RegistryKey::Create),info::function::argument(_T("const tstring &val"),val));
98  fn.add_arg(_T("const tstring &key_name"),key_name);
99  throw exception_type(error_msgs[6],fn,__REV_INFO__);
100  }
101  return ret;
102 }
103 
104 inline
105 long RegistryKey::SetValue(const tstring &val, const tstring &key_name, const DWORD type) {
106  long ret;
107  if ((ret=::RegSetValueEx(key.m_hKey, key_name.c_str(), NULL, type, reinterpret_cast<const BYTE *>(val.c_str()), val.size()))!=ERROR_SUCCESS) {
108  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&RegistryKey::Create),info::function::argument(_T("const tstring &val"),val));
109  fn.add_arg(_T("const tstring &key_name"),key_name);
110  fn.add_arg(_T("const DWORD type"),tostring(type));
111  throw exception_type(error_msgs[6],fn,__REV_INFO__);
112  }
113  return ret;
114 }
115 
116 inline
117 long RegistryKey::SetValue(const unsigned long val, const tstring &key_name) {
118  long ret;
119  if ((ret=key.SetDWORDValue(key_name.c_str(), val))!=ERROR_SUCCESS) {
120  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&RegistryKey::Create),info::function::argument(_T("const unsigned long val"),tostring(val)));
121  fn.add_arg(_T("const tstring &key_name"),key_name);
122  throw exception_type(error_msgs[6],fn,__REV_INFO__);
123  }
124  return ret;
125 }