| 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
 | /*
  $Id: reg.cpp,v 1.1.1.1 2005/07/07 15:05:59 oflebbe Exp $
  Copyright (C) 2003 Olaf Flebbe, Science and Computing AG
  o.flebbe@science-computing.de
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "reg.h"
Registry::Registry( const mystring key) {
  keyHandle = 0;
  if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE, key.c_str(), 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS , &keyHandle)) 
    return;
}
Registry::~Registry() {
  if (keyHandle != NULL)
    RegCloseKey( keyHandle);
}
mystring Registry::getValue( const mystring& value) const {
  wchar_t *retBuf = NULL;
  DWORD retBufSize = 128;
	
  while (1) {
    retBuf = new wchar_t[ retBufSize]; //o.k. not clean alloc twice as needed 
    DWORD type;
    long ret = RegQueryValueEx( keyHandle, value.c_str(), 0, &type, (LPBYTE) retBuf, &retBufSize);
    if (ret == ERROR_MORE_DATA) {
      delete[] retBuf;
      continue;
    }
    if (ret != ERROR_SUCCESS || type != REG_SZ)
      return mystring(L"");
    break;
  }
  mystring ret( retBuf);
  delete[] retBuf;
  return ret;
}
std::list<mystring> Registry::getValues( const mystring& value) const {
  wchar_t *retBuf = NULL;
  DWORD retBufSize = 128;
  std::list<mystring> list;
  while (1) {
    retBuf = new wchar_t[ retBufSize]; //o.k. not clean alloc twice as needed 
    DWORD type;
    long ret = RegQueryValueEx( keyHandle, value.c_str(), 0, &type, (LPBYTE) retBuf, &retBufSize);
    if (ret == ERROR_MORE_DATA) {
      delete[] retBuf;
      continue;
    }
    if (ret != ERROR_SUCCESS || type != REG_MULTI_SZ) {
      delete[] retBuf;
      return list;
    }
    break;
  }
	
  wchar_t *ptr = retBuf;
  while (*ptr != 0) {
    list.push_back( mystring( ptr));
    while (*ptr != 0)
      ptr++;
    // ptr points to terminating 0
    ptr++;
    // should point to new entry, or terminating 0
  }
  delete[] retBuf;
  return list;
}
std::list<mystring> Registry::getSubKeys() const {
  wchar_t *retBuf = NULL;
  DWORD retBufSize = 128;
  std::list<mystring> list;
  int numKey =0;
  long ret;
  do {
    while (1) {
      retBuf = new wchar_t[ retBufSize]; //o.k. not clean alloc twice as needed 
      FILETIME mod; 
      ret = RegEnumKeyEx( keyHandle, numKey, retBuf, &retBufSize, 0, NULL, NULL, &mod);
      if (ret == ERROR_MORE_DATA) {
	delete[] retBuf;
	retBufSize *= 2;
	continue;
      }
      if (!(ret == ERROR_NO_MORE_ITEMS || ret== ERROR_SUCCESS)){
	delete[] retBuf;	
	return list;
      }
      break;
    }
    if (ret == ERROR_SUCCESS) {
      list.push_back( mystring(retBuf));
    } 
    delete[] retBuf;
    numKey++;
  } while (ret != ERROR_NO_MORE_ITEMS);
  return list;
}
bool Registry::exists( const mystring& value) {
	if (keyHandle) {
		int ret =  RegQueryValueEx( keyHandle, value.c_str(), 0, NULL, NULL, NULL);
		return (ret == ERROR_SUCCESS);
	}
	return false;
}
#if 0
main() {
  Registry reg( L"SOFTWARE\\science + computing\\scap");
  printf("%S\n", reg.getValue( L"basedn").c_str());
  std::list<mystring> servers = reg.getValues( L"servers");
  for (std::list<mystring>::const_iterator  ptr = servers.begin(); ptr != servers.end(); ptr++) {
    printf("%S\n", ptr->c_str());
  }
  Registry zwo( L"System\\CurrentControlSet\\Control\\Lsa\\Kerberos\\Domains");
  std::list<mystring> realms = zwo.getSubKeys();
  for (std::list<mystring>::const_iterator  ptr = realms.begin(); ptr != realms.end(); ptr++) {
    printf("%S\n", ptr->c_str());
  }
}
#endif
 |