Click here to Skip to main content
15,885,091 members
Articles / Desktop Programming / MFC

CRegisterEx - a registry wrapper class

Rate me:
Please Sign up or sign in to vote.
2.71/5 (3 votes)
5 Feb 20021 min read 51.4K   825   9  
An easy to use all-in-one registry class
#include "StdAfx.h"
#include "registerex.h"
#include <stdlib.h>

#pragma warning ( disable : 4267 )

#define MAX_BUFFER 2048
char buffer[MAX_BUFFER];

CString pt;

CRegisterEx::CRegisterEx(CString path)
{
	pt = path;
}

CRegisterEx::~CRegisterEx(void)
{
}

// Writing strings to the register.

void CRegisterEx::WriteString(CString str, CString subPath, CString Key)
{
	HKEY hk;
	TCHAR szBuf[2048];
	CString insidePath = pt;

	if (Key)
	{
		insidePath = insidePath + "\\" + subPath;
	}

    if (RegCreateKey(HKEY_CURRENT_USER, _T(insidePath), &hk))
	{
		// Woops, you don't have privileges
        TRACE0("Could not create the registry key.\n\nDo you have the right privileges?\n");
	}

	strcpy(szBuf, str);

	if (RegSetValueEx(hk, _T(Key), 0, REG_EXPAND_SZ, (LPBYTE)szBuf, strlen(str) + 1))
	{
		// Hmm, you did something wrong
        TRACE0("Could not set the given String.\n\nDo you have the right privileges?\n");
	}
	
	RegCloseKey(hk);
}

// Reading strings from the register

CString CRegisterEx::ReadString(CString subPath, CString Key)
{
	HKEY hk;
	CString insidePath = pt;
	TCHAR str[MAX_BUFFER];
	DWORD length = MAX_BUFFER;

	if (subPath)
	{
		insidePath = insidePath + "\\" + subPath;
	}
	
	if (RegOpenKey(HKEY_CURRENT_USER, _T(insidePath), &hk))
	{
		// Hey there, you're regpath doesn't exist (I don't give an error, but just return NULL)
		return NULL;
	}

	if (RegQueryValueEx(hk, _T(Key), NULL, NULL, (LPBYTE)str, &length))
	{
		// woops, can't find the Key
		return NULL;
	}

	RegCloseKey(hk);
	
	return CString(str);
}

// Writing integers to the register

void CRegisterEx::WriteInteger(int n, CString subPath, CString Key)
{	
	_itoa(n, buffer, 10);
	CString str = (LPCSTR)buffer;
	WriteString(str, subPath, Key);
}

// Reading integers from the register

int CRegisterEx::ReadInteger(CString subPath, CString Key)
{
	CString str = ReadString(subPath, Key);
	int n = atoi(str);
	return n;
}

// writing POINT structures to the register

void CRegisterEx::WritePoint(CPoint pt, CString subPath, CString Key)
{
	CString keyX, keyY;
	keyX = Key + "_X";
	keyY = Key + "_Y";
	WriteInteger(pt.x, subPath, keyX);
	WriteInteger(pt.y, subPath, keyY);
}

// reading POINT structures from the register

CPoint CRegisterEx::ReadPoint(CString subPath, CString Key)
{
	CString keyX, keyY;
	keyX = Key + "_X";
	keyY = Key + "_Y";
	int x = ReadInteger(subPath, keyX);
	int y = ReadInteger(subPath, keyY);
	return CPoint(x, y);
}

// writing LPCRECT structures to the register

void CRegisterEx::WriteRect(CRect rect, CString subPath, CString Key)
{
	CString keyC, keyD;
	keyC = Key + "_C";
	keyD = Key + "_D";
	WritePoint(CPoint(rect.top, rect.left), subPath, keyC);
	WritePoint(CPoint(rect.bottom, rect.right), subPath, keyD);
}

// reading LPCRECT structures from the register

CRect CRegisterEx::ReadRect(CString subPath, CString Key)
{	
	CString keyC, keyD;
	keyC = Key + "_C";
	keyD = Key + "_D";
	CPoint ptC = ReadPoint(subPath, keyC);
	CPoint ptD = ReadPoint(subPath, keyD);
	return CRect(ptC, ptD);
}

// writing double values to the register

void CRegisterEx::WriteDouble(double d, CString subPath, CString Key)
{
	_gcvt(d, 100, buffer);
	CString str = (LPCSTR)buffer;
	CString doubleKey = Key + "_d";
	WriteString(str, subPath, doubleKey);
}

// reading double values from the register

double CRegisterEx::ReadDouble(CString subPath, CString Key)
{
	CString doubleKey = Key + "_d";
	CString str = ReadString(subPath, doubleKey);
	double d = atof(str);
	return d;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Netherlands Netherlands
I'm a developer in C++, however, I developed a lot in VB, Delphi and JAVA. Also I make web based applications using languages as PHP, ASP (JavaScript, VBScript), ASP.NET (C# and VB), JSP and Perl

Currently working in a small IT company.

Comments and Discussions