Algorithms
Electronics
File Formats
Games Programming
Hardware/Architecture
Humour
Java/Javascript
Link Site,Library
Linux,GNU
Magazines/Books
Networks
New/Latest Stuff
Programming Languages
Protocols
Servers/ISAPI/ IO Completion Ports
Sound and Music
Source Code
Visual Basic
Windows
Winsock

A Simple Registry Class

I haven't finished this C++ class yet, but it can do quite a few things already. It can open up a registry on another computer, create a new key, query a value and set a value at the moment.

Registry.h

#define WIN32_LEAN_AND_MEAN
#include 
class CRegistry
{
public:
   CRegistry(HKEY root,LPSTR path = NULL,REGSAM samDesired = KEY_ALL_ACCESS);
   CRegistry(CRegistry & reg, REGSAM samDesired = KEY_ALL_ACCESS);
   CRegistry(CRegistry & reg,LPSTR path, REGSAM samDesired = KEY_ALL_ACCESS);
   CRegistry(LPTSTR machineName,HKEY hKey);
   CRegistry * createKey(LPCTSTR subKey,DWORD dwOptions = REG_OPTION_NON_VOLATILE, 
  REGSAM samDesired = KEY_ALL_ACCESS,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL,LPSTR lpClass = NULL);
   LONG notifyChangeKeyValue(BOOL bWatchSubtree,DWORD dwNotifyFilter);
   LONG deleteKey(LPCTSTR lpSubKey);
   LONG setValue(LPCTSTR valueName,DWORD type,CONST BYTE *lpData,DWORD cbData);
   LONG setValue(LPCTSTR valueName,DWORD value);
   LONG setValue(LPCTSTR valueName,LPCTSTR value);
   LONG queryValue(LPTSTR valueName,LPDWORD type,LPBYTE data,LPDWORD len);
   LONG queryValue(LPTSTR valueName,LPCTSTR data,LPDWORD len);
   LONG queryValue(LPTSTR valueName,LPDWORD data);
   ~CRegistry();
private:
   HKEY getRegHandle();
   HKEY hKey;
};

registry.cpp

#include "registry.h"
#include 
// #define TESTREG // define to compile test program that stress's Registry functions
HKEY CRegistry::getRegHandle()
{
return hKey;
}

CRegistry::CRegistry(LPTSTR machineName,HKEY hKey)
{
if(RegConnectRegistry(machineName,hKey,&hKey)!=ERROR_SUCCESS)
  {
  printf("Error [ RegConnectRegistry]\n");
  }
}

CRegistry::CRegistry(HKEY root,LPSTR path,REGSAM samDesired)
{
if(RegOpenKeyEx(root,path,0,samDesired,&hKey)!=ERROR_SUCCESS)
  {
  printf("Error [ RegOpenKeyEx]\n");
  }
}

CRegistry::CRegistry(CRegistry & reg, REGSAM samDesired)
{
HKEY temp_hKey = reg.getRegHandle();
if(temp_hKey == NULL)
  {
  printf("Error [ NULL temp_hKey]\n");
  }
if(RegOpenKeyEx(temp_hKey,NULL,0,samDesired,&hKey)!=ERROR_SUCCESS)
  {
  printf("Error [ RegOpenKeyEx]\n");
  }
}

CRegistry::CRegistry(CRegistry & reg,LPSTR path,REGSAM samDesired)
{
HKEY temp_hKey = reg.getRegHandle();
if(temp_hKey == NULL)
  {
  printf("Error [ NULL temp_hKey]\n");
  }
if(RegOpenKeyEx(temp_hKey,path,0,samDesired,&hKey)!=ERROR_SUCCESS)
  {
  printf("Error [ RegOpenKeyEx]\n");
  }
}

LONG CRegistry::notifyChangeKeyValue(BOOL bWatchSubtree,DWORD dwNotifyFilter)
{
return RegNotifyChangeKeyValue(hKey,bWatchSubtree,dwNotifyFilter,NULL,FALSE);
}

CRegistry * CRegistry::createKey(LPCTSTR subKey,DWORD dwOptions,REGSAM samDesired,
	LPSECURITY_ATTRIBUTES lpSecurityAttributes,LPSTR lpClass)
{
DWORD disposition = 0;
CRegistry * returnRegistry = NULL;
HKEY temp_hKey;
if(RegCreateKeyEx(hKey,subKey,0,lpClass,dwOptions,samDesired,
    lpSecurityAttributes,&temp_hKey,&disposition) !=ERROR_SUCCESS) return NULL;

returnRegistry = new CRegistry(hKey);
RegCloseKey(hKey);
return returnRegistry;
}

LONG CRegistry::deleteKey(LPCTSTR lpSubKey)
{
return RegDeleteKey(hKey,lpSubKey);
}

LONG CRegistry::setValue(LPCTSTR valueName,DWORD type,CONST BYTE *lpData,DWORD cbData)
{
return RegSetValueEx(hKey,valueName,0,type,lpData,cbData);
}

LONG CRegistry::setValue(LPCTSTR valueName,LPCTSTR value)
{
return RegSetValueEx(hKey,valueName,0,REG_SZ,value,lstrlen(value));
}

LONG CRegistry::setValue(LPCTSTR valueName,DWORD value)
{
return RegSetValueEx(hKey,valueName,0,REG_DWORD,(const unsigned char *)&value,4);
}

LONG CRegistry::queryValue(LPTSTR valueName,LPDWORD type,LPBYTE data,LPDWORD len)
{
return RegQueryValueEx(hKey,valueName,NULL,type,data,len);
}

LONG CRegistry::queryValue(LPTSTR valueName,LPCTSTR data,LPDWORD len)
{
DWORD type = 0;
LONG rtc = queryValue(valueName,&type,(LPBYTE)data,len);
if(rtc!=ERROR_SUCCESS) return rtc;
if(type!=REG_SZ)
  {
  printf("Not of Type Queried\n");
  return !ERROR_SUCCESS;
  }
return rtc;
}

LONG CRegistry::queryValue(LPTSTR valueName,LPDWORD data)
{
DWORD type = 0;
DWORD len = 4;
LONG rtc = queryValue(valueName,&type,(LPBYTE)data,&len);
if(rtc!=ERROR_SUCCESS) return rtc;
if(type!=REG_DWORD)
  {
  printf("Not of Type Queried\n");
  return !ERROR_SUCCESS;
  }
return rtc;
}


CRegistry::~CRegistry()
{
RegCloseKey(hKey);
}

#ifdef TESTREG
int main()
{
DWORD version =  NULL ;

printf("Registry Test Program\n");
CRegistry test(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
test.queryValue("InstallType",&version);
printf("Version : %d\n",version);
}
#endif

Copyright Geoffrey Smith 2000