/* $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 Registry::getValues( const mystring& value) const { wchar_t *retBuf = NULL; DWORD retBufSize = 128; std::list 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 Registry::getSubKeys() const { wchar_t *retBuf = NULL; DWORD retBufSize = 128; std::list 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 servers = reg.getValues( L"servers"); for (std::list::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 realms = zwo.getSubKeys(); for (std::list::const_iterator ptr = realms.begin(); ptr != realms.end(); ptr++) { printf("%S\n", ptr->c_str()); } } #endif