| 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 |
#include "stdafx.h" |
|---|
| 26 |
#include <string> |
|---|
| 27 |
using namespace std; |
|---|
| 28 |
|
|---|
| 29 |
#include "regkey.h" |
|---|
| 30 |
|
|---|
| 31 |
RegKey::RegKey(HKEY startkey) |
|---|
| 32 |
{ |
|---|
| 33 |
hkey=NULL; |
|---|
| 34 |
this->startkey = startkey; |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
bool RegKey::CreateKey(const char *keyname) |
|---|
| 38 |
{ |
|---|
| 39 |
if ( ! keyname ) return false; |
|---|
| 40 |
if (hkey) RegCloseKey(hkey); |
|---|
| 41 |
|
|---|
| 42 |
LONG res = RegCreateKeyEx(startkey, keyname, |
|---|
| 43 |
0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hkey, 0 ) ; |
|---|
| 44 |
|
|---|
| 45 |
return res == ERROR_SUCCESS; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
bool RegKey::OpenKey(const char *keyname, REGSAM samdesired ) |
|---|
| 49 |
{ |
|---|
| 50 |
if ( ! keyname ) return false; |
|---|
| 51 |
if (hkey) RegCloseKey(hkey); |
|---|
| 52 |
#if 0 |
|---|
| 53 |
LONG res = RegOpenKeyEx (startkey, |
|---|
| 54 |
(const char *)keyname, |
|---|
| 55 |
0, |
|---|
| 56 |
samdesired, |
|---|
| 57 |
&hkey); |
|---|
| 58 |
#endif |
|---|
| 59 |
DWORD disposition; |
|---|
| 60 |
LONG res = RegCreateKeyEx( |
|---|
| 61 |
startkey, |
|---|
| 62 |
(const char *)keyname, |
|---|
| 63 |
0, |
|---|
| 64 |
NULL, |
|---|
| 65 |
REG_OPTION_NON_VOLATILE, |
|---|
| 66 |
samdesired, |
|---|
| 67 |
NULL, |
|---|
| 68 |
|
|---|
| 69 |
&hkey, |
|---|
| 70 |
&disposition |
|---|
| 71 |
); |
|---|
| 72 |
|
|---|
| 73 |
return res == ERROR_SUCCESS; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
bool RegKey::OpenKey(const char *keyname) |
|---|
| 77 |
{ |
|---|
| 78 |
return OpenKey(keyname, KEY_ALL_ACCESS ); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
bool RegKey::SetValue(const char *valuename, const char *value) |
|---|
| 82 |
{ |
|---|
| 83 |
if ( ! valuename || ! value ) return false; |
|---|
| 84 |
LONG res = RegSetValueEx( hkey, valuename, 0, REG_SZ, |
|---|
| 85 |
(const unsigned char *)value, lstrlen(value)+1); |
|---|
| 86 |
return res == ERROR_SUCCESS; |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
string RegKey::QueryValue(const char *valuename) |
|---|
| 91 |
{ |
|---|
| 92 |
DWORD dwType = REG_SZ; |
|---|
| 93 |
|
|---|
| 94 |
if (! hkey ) return NULL; |
|---|
| 95 |
|
|---|
| 96 |
LPBYTE lpData = NULL; |
|---|
| 97 |
DWORD cbData = 0; |
|---|
| 98 |
|
|---|
| 99 |
LONG res = RegQueryValueEx( hkey, valuename, 0, &dwType, NULL, &cbData ); |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
lpData = (LPBYTE) malloc( cbData ); |
|---|
| 108 |
|
|---|
| 109 |
if ( !lpData ) { |
|---|
| 110 |
::MessageBox( NULL, "Out of memory error occured in QueryValue function.", |
|---|
| 111 |
"SwishCtl Error", MB_OK | MB_ICONINFORMATION); |
|---|
| 112 |
|
|---|
| 113 |
return *(new string()); |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
res = RegQueryValueEx( hkey, valuename, 0, &dwType, lpData, &cbData ); |
|---|
| 117 |
|
|---|
| 118 |
if (res != ERROR_SUCCESS) { |
|---|
| 119 |
free( lpData ); |
|---|
| 120 |
return *(new string()); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
string *temp = new string( (char *)lpData ) ; |
|---|
| 124 |
free( lpData ); |
|---|
| 125 |
|
|---|
| 126 |
return *temp; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
RegKey::~RegKey() |
|---|
| 130 |
{ |
|---|
| 131 |
if ( hkey ) ::RegCloseKey(hkey); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
bool RegKey::DeleteKey(const char *subkey) |
|---|
| 135 |
{ |
|---|
| 136 |
return ::RegDeleteKey(hkey, subkey ) == ERROR_SUCCESS; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|