Electroneum
reg_utils.h
Go to the documentation of this file.
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 #ifndef _MUSC_UTILS_EX_H_
29 #define _MUSC_UTILS_EX_H_
30 
31 namespace epee
32 {
33 namespace reg_utils
34 {
35  //-----------------------------------------------------------------------------------------------------------------------------------
36  template<class T>
37  bool RegSetPODValue(HKEY hParentKey, const char* pSubKey, const char* pValName, const T& valToSave, bool force_create = true)
38  {
39  HKEY hRegKey = 0;
40  DWORD dw = 0;
41 
42  if( ::RegOpenKeyExA(hParentKey, pSubKey, 0, KEY_WRITE, &hRegKey) != ERROR_SUCCESS )
43  if(force_create && (::RegCreateKeyExA(hParentKey, pSubKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hRegKey, &dw) != ERROR_SUCCESS) )
44  return false;
45 
46 
47  DWORD val_type = (sizeof(valToSave) == sizeof(DWORD)) ? REG_DWORD:REG_BINARY;
48 
49  BOOL res = ::RegSetValueExA( hRegKey, pValName, 0, val_type, (LPBYTE)&valToSave, sizeof(valToSave)) == ERROR_SUCCESS;
50 
51  ::RegCloseKey(hRegKey);
52  return ERROR_SUCCESS==res ? true:false;
53  }
54  //-----------------------------------------------------------------------------------------------------------------------------------
55  template<class T>
56  bool RegGetPODValue(HKEY hParentKey, const char* pSubKey, const char* pValName, T& valToSave)
57  {
58  HKEY hRegKey = 0;
59  LONG res = 0;
60 
61 
62  if(::RegOpenKeyExA(hParentKey, pSubKey, 0, KEY_READ, &hRegKey) == ERROR_SUCCESS )
63  {
64  DWORD dwType, lSize = 0;
65  res = ::RegQueryValueExA(hRegKey, pValName, 0, &dwType, NULL, &lSize);
66  if(ERROR_SUCCESS!=res || (sizeof(valToSave) < lSize) )
67  {
68  ::RegCloseKey(hRegKey);
69  return false;
70  }
71  res = ::RegQueryValueExA(hRegKey, pValName, 0, &dwType, (LPBYTE)&valToSave, &lSize);
72  }
73  return ERROR_SUCCESS==res ? true:false;
74  }
75  //-----------------------------------------------------------------------------------------------------------------------------------
76  inline
77  bool RegSetANSIString(HKEY hParentKey, const char* pSubKey, const char* pValName, const std::string& strToSave)
78  {
79  HKEY hRegKey = 0;
80  DWORD dw = 0;
81  DWORD res_ = 0;
82  if( (res_ = ::RegCreateKeyExA(hParentKey, pSubKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hRegKey, &dw)) != ERROR_SUCCESS )
83  if( (res_= ::RegOpenKeyExA(hParentKey, pSubKey, 0, KEY_WRITE, &hRegKey)) != ERROR_SUCCESS )
84  return false;
85 
86  DWORD valType = REG_SZ;
87  const char* pStr = strToSave.c_str();
88  DWORD sizeOfStr = (DWORD)strToSave.size()+1;
89  LSTATUS res = ::RegSetValueExA(hRegKey, pValName, 0, valType, (LPBYTE)pStr, sizeOfStr);
90 
91  ::RegCloseKey(hRegKey);
92  return ERROR_SUCCESS==res ? true:false;
93  }
94  //-----------------------------------------------------------------------------------------------------------------------------------
95  inline
96  bool RegGetANSIString(HKEY hParentKey, const char* pSubKey, const char* pValName, std::string& strToSave)
97  {
98  HKEY hRegKey = 0;
99  LONG res = 0;
100 
101 
102  if((res = ::RegOpenKeyExA(hParentKey, pSubKey, 0, KEY_READ, &hRegKey)) == ERROR_SUCCESS )
103  {
104  DWORD dwType, lSize = 0;
105  res = ::RegQueryValueExA(hRegKey, pValName, 0, &dwType, NULL, &lSize);
106  if(ERROR_SUCCESS!=res)
107  {
108 
109  ::RegCloseKey(hRegKey);
110  return false;
111  }
112  char* pTmpStr = new char[lSize+2];
113  memset(pTmpStr, 0, lSize+2);
114  res = ::RegQueryValueExA(hRegKey, pValName, 0, &dwType, (LPBYTE)pTmpStr, &lSize);
115  pTmpStr[lSize+1] = 0; //be happy ;)
116  strToSave = pTmpStr;
117  delete [] pTmpStr;
118  ::RegCloseKey(hRegKey);
119  }
120  return ERROR_SUCCESS==res ? true:false;
121  }
122  //-----------------------------------------------------------------------------------------------------------------------------------
123  template<class TMemoryObject>
124  bool RegSetRAWValue(HKEY hKey, const char* pValName, const TMemoryObject& valToSave, DWORD valType = REG_BINARY)
125  {
126  LONG res = ::RegSetValueExA( hKey, pValName, 0, valType, (CONST BYTE*)valToSave.get(0), (DWORD)valToSave.get_size());
127 
128  return ERROR_SUCCESS==res ? true:false;
129  }
130  //----------------------------------------------------------------------------------------------------------------------------------
131  bool RegSetRAWValue(HKEY hKey, const char* pValName, const std::string & valToSave, DWORD valType = REG_BINARY)
132  {
133  LONG res = ::RegSetValueExA( hKey, pValName, 0, valType, (CONST BYTE*)valToSave.data(), (DWORD)valToSave.size());
134 
135  return ERROR_SUCCESS==res ? true:false;
136  }
137  //-----------------------------------------------------------------------------------------------------------------------------------
138  template<class TMemoryObject>
139  bool RegGetRAWValue(HKEY hKey, const char* pValName, TMemoryObject& valToSave, DWORD* pRegType)
140  {
141  DWORD dwType, lSize = 0;
142  LONG res = ::RegQueryValueExA(hKey, pValName, 0, &dwType, NULL, &lSize);
143  if(ERROR_SUCCESS!=res || 0 >= lSize)
144  {
145  valToSave.release();
146  return false;
147  }
148  if(valToSave.get_size() < lSize)
149  valToSave.alloc_buff(lSize);
150  res = ::RegQueryValueExA(hKey, pValName, 0, &dwType, (LPBYTE)valToSave.get(0), &lSize);
151  if(pRegType) *pRegType = dwType;
152 
153  return ERROR_SUCCESS==res ? true:false;
154  }
155  //-----------------------------------------------------------------------------------------------------------------------------------
156  bool RegGetRAWValue(HKEY hKey, const char* pValName, std::string& valToSave, DWORD* pRegType)
157  {
158  DWORD dwType, lSize = 0;
159  LONG res = ::RegQueryValueExA(hKey, pValName, 0, &dwType, NULL, &lSize);
160  if(ERROR_SUCCESS!=res || 0 >= lSize)
161  {
162  return false;
163  }
164 
165  valToSave.resize(lSize);
166  res = ::RegQueryValueExA(hKey, pValName, 0, &dwType, (LPBYTE)valToSave.data(), &lSize);
167  if(pRegType) *pRegType = dwType;
168 
169  return ERROR_SUCCESS==res ? true:false;
170  }
171  //-----------------------------------------------------------------------------------------------------------------------------------
172  template<class TMemoryObject>
173  bool RegSetRAWValue(HKEY hParentKey, const char* pSubKey, const char* pValName, const TMemoryObject& valToSave, DWORD valType = REG_BINARY)
174  {
175  HKEY hRegKey = 0;
176  DWORD dw = 0;
177  bool res = false;
178 
179  if( ::RegCreateKeyExA(hParentKey, pSubKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hRegKey, &dw) != ERROR_SUCCESS )
180  if( ::RegOpenKeyExA(hParentKey, pSubKey, 0, KEY_WRITE, &hRegKey) != ERROR_SUCCESS )
181  return false;
182 
183  res = RegSetRAWValue(hRegKey, pValName, valToSave, valType);
184 
185  ::RegCloseKey(hRegKey);
186  return res;
187  }
188  //-----------------------------------------------------------------------------------------------------------------------------------
189  bool RegSetRAWValue(HKEY hParentKey, const char* pSubKey, const char* pValName, const std::string& valToSave, DWORD valType = REG_BINARY)
190  {
191  HKEY hRegKey = 0;
192  DWORD dw = 0;
193  bool res = false;
194 
195  if( ::RegCreateKeyExA(hParentKey, pSubKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hRegKey, &dw) != ERROR_SUCCESS )
196  if( ::RegOpenKeyExA(hParentKey, pSubKey, 0, KEY_WRITE, &hRegKey) != ERROR_SUCCESS )
197  return false;
198 
199  res = RegSetRAWValue(hRegKey, pValName, valToSave, valType);
200 
201  ::RegCloseKey(hRegKey);
202  return res;
203  }
204  //-----------------------------------------------------------------------------------------------------------------------------------
205  template<class TMemoryObject>
206  bool RegGetRAWValue(HKEY hParentKey, const char* pSubKey, const char* pValName, TMemoryObject& valToSave, DWORD* pRegType)
207  {
208  HKEY hRegKey = 0;
209  bool res = false;
210 
211  if(::RegOpenKeyExA(hParentKey, pSubKey, 0, KEY_READ, &hRegKey) == ERROR_SUCCESS )
212  {
213  res = RegGetRAWValue(hRegKey, pValName, valToSave, pRegType);
214  ::RegCloseKey(hRegKey);
215  }
216  return res;
217  }
218  //-----------------------------------------------------------------------------------------------------------------------------------
219  inline
220  bool RegGetRAWValue(HKEY hParentKey, const char* pSubKey, const char* pValName, std::string& valToSave, DWORD* pRegType)
221  {
222  HKEY hRegKey = 0;
223  bool res = false;
224 
225  if(::RegOpenKeyExA(hParentKey, pSubKey, 0, KEY_READ, &hRegKey) == ERROR_SUCCESS )
226  {
227  res = RegGetRAWValue(hRegKey, pValName, valToSave, pRegType);
228  ::RegCloseKey(hRegKey);
229  }
230  return res;
231  }
232  //-----------------------------------------------------------------------------------------------------------------------------------
233  inline
234  bool RegRemoveValue(HKEY hParentKey, const char* pValName)
235  {
236  //CHECK_AND_ASSERT(hParentKey&&pValName, false);
237  return ::RegDeleteValueA(hParentKey, pValName)==ERROR_SUCCESS ? true:false;
238  }
239  //-----------------------------------------------------------------------------------------------------------------------------------
240  inline
241  bool RegRemoveKey(HKEY hParentKey, const char* pKeyName)
242  {
243  //CHECK_AND_ASSERT(hParentKey&&pKeyName, false);
244  return ::RegDeleteKeyA(hParentKey, pKeyName)==ERROR_SUCCESS ? true:false;
245  }
246 
247 }
248 }
249 #endif //_MUSC_UTILS_EX_H_
const char * res
Definition: hmac_keccak.cpp:41
bool RegSetANSIString(HKEY hParentKey, const char *pSubKey, const char *pValName, const std::string &strToSave)
Definition: reg_utils.h:77
bool RegGetANSIString(HKEY hParentKey, const char *pSubKey, const char *pValName, std::string &strToSave)
Definition: reg_utils.h:96
const uint32_t T[512]
bool RegSetRAWValue(HKEY hKey, const char *pValName, const TMemoryObject &valToSave, DWORD valType=REG_BINARY)
Definition: reg_utils.h:124
::std::string string
Definition: gtest-port.h:1097
bool RegGetPODValue(HKEY hParentKey, const char *pSubKey, const char *pValName, T &valToSave)
Definition: reg_utils.h:56
bool RegSetPODValue(HKEY hParentKey, const char *pSubKey, const char *pValName, const T &valToSave, bool force_create=true)
Definition: reg_utils.h:37
bool RegRemoveValue(HKEY hParentKey, const char *pValName)
Definition: reg_utils.h:234
bool RegGetRAWValue(HKEY hKey, const char *pValName, TMemoryObject &valToSave, DWORD *pRegType)
Definition: reg_utils.h:139
bool RegRemoveKey(HKEY hParentKey, const char *pKeyName)
Definition: reg_utils.h:241