Click here to Skip to main content
15,896,269 members
Articles / Programming Languages / C++

Registry Wrapper Class (CRegistry)

Rate me:
Please Sign up or sign in to vote.
4.74/5 (72 votes)
2 Jan 200515 min read 214.3K   4.2K   95  
An article and helper class for the Windows Registry.
// CRegEntry: interface for the CRegEntry class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(_REGENTRY_H_INCLUDED)
#define _REGENTRY_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CRegistry;

class CRegEntry
{
public:

	CRegEntry(CRegistry* Owner = NULL);		
	virtual ~CRegEntry() { if (lpszName) delete [] lpszName; if (lpszStr) delete [] lpszStr; };

	/* -----------------------------------------*
	 *	Operators								*
	 * -----------------------------------------*/
	
	CRegEntry&	operator =( CRegEntry& cregValue );
	CRegEntry&	operator =( LPCTSTR lpszValue );
	CRegEntry&	operator =( LPDWORD lpdwValue );	
	CRegEntry&	operator =( DWORD dwValue ) { return (*this = &dwValue); }	
				operator LPTSTR();
				operator DWORD();

	
	// Data types without implemented conversions
	// NOTE: I realize these will only check asserts
	// when a value is set and retrieved during the
	// same session. But it is better than no check.

	REGENTRY_NONCONV_STORAGETYPE(POINT);
	REGENTRY_NONCONV_STORAGETYPE(RECT);

	// Numeric types with conversions
	// If you'd like to add more, follow this form:
	// data type, max string length + 1, format specification, from string, from DWORD

	REGENTRY_CONV_NUMERIC_STORAGETYPE(__int64, 28, %I64d, _ttoi64(lpszStr), (__int64)dwDWORD)
	REGENTRY_CONV_NUMERIC_STORAGETYPE(double, 18, %f, _tcstod(lpszStr, NULL), (double)dwDWORD)	
	REGENTRY_CONV_NUMERIC_STORAGETYPE(bool, 2, %d, (_ttoi(lpszStr) != 0), (dwDWORD != 0))
	REGENTRY_CONV_NUMERIC_STORAGETYPE(int, 12, %d, _ttoi(lpszStr), (int)dwDWORD)
	REGENTRY_CONV_NUMERIC_STORAGETYPE(UINT, 11, %d, (UINT)_tcstoul(lpszStr, NULL, NULL), (UINT)dwDWORD)

	// Types with conversions: type to/from string, type from unsigned long

	REGENTRY_CONV_STORAGETYPE(tstring, _R_BUF(_MAX_REG_VALUE); _tcscpy(buffer, Value.c_str());,
	lpszStr, _ultot(dwDWORD, lpszStr, NULL), _T(""))
		

	/* -----------------------------------------*
	 *	Member Variables and Functions			*
	 * -----------------------------------------*/
	
	LPTSTR		lpszName;	// The value name
	UINT		iType;		// Value data type
	
	void		InitData(CRegistry* Owner = NULL);	
	void		ForceStr();
	bool		Delete();	


	/* The following six functions handle REG_MULTI_SZ support: */

	void		SetMulti(LPCTSTR lpszValue, size_t nLen, bool bInternal = false);	
	void		MultiRemoveAt(size_t nIndex);
	void		MultiSetAt(size_t nIndex, LPCTSTR lpszVal);
	LPTSTR		GetMulti(LPTSTR lpszDest, size_t nMax = _MAX_REG_VALUE);
	LPCTSTR		MultiGetAt(size_t nIndex);	
	size_t		MultiLength(bool bInternal = false);
	size_t		MultiCount();
		

	void		SetBinary(LPBYTE lpbValue, size_t nLen);	
	void		GetBinary(LPBYTE lpbDest, size_t nMaxLen);
	size_t		GetBinaryLength();
	bool		Convertible() { return __bConvertable; }

	__inline	SetOwner(CRegistry* Owner) { __cregOwner = Owner; }
	
	template <class T>void SetStruct(T &type) { SetBinary((LPBYTE) &type, sizeof(T)); }
	template <class T>void GetStruct(T &type) { GetBinary((LPBYTE) &type, sizeof(T)); }
	
	__inline	IsString()		{ return (iType == REG_SZ); }
	__inline	IsDWORD()		{ return (iType == REG_DWORD); }
	__inline	IsBinary()		{ return (iType == REG_BINARY); }	
	__inline	IsMultiString() { return (iType == REG_MULTI_SZ); }
	
	__inline	IsStored()		{ return __bStored; }
	__inline	Exists()		{ return __bStored; }

	__inline	MultiClear()	{ SetMulti(_T("\0"), 2); }
	__inline	MultiAdd(LPCTSTR lpszVal) { MultiSetAt(MultiCount(), lpszVal); }

protected:

	CRegistry*	__cregOwner;
	bool		__bConvertable;
	bool		__bStored;

private:

	/* Create a variable for each prominent data type */

	DWORD		dwDWORD;	
	LPTSTR		lpszStr;
		
	std::vector<BYTE> vBytes;
	std::vector<tstring> vMultiString;
};


#endif

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
United States United States
Well first of all, it is fairly obvious my name is Stuart Konen (I'm sure 50% of you just took notice), all of my life I've lived on a farm located in Northern Idaho. What shatters the stereotype of rural residence however, is the fact that I'm very active in the technology and programming worlds. I took up the hobby of programming at age 9, at that point it was little language known as Quick Basic *sigh*. Fast forward another 9 years... (Woah... I just realized that's half of my existence. But that's something I'll have to contemplate later, as I have an autobiography to tell).

Now my experience in programming has improved vastly, I've released various technologies and programs and I'm continuing to pump out concepts and systems that are getting glances from all over the world. My weapon of choice is C++, the core language of the majority of freshly released software, it's sleak, mean, and incrediably powerful. On the side I venture into web application development and site design, where my interest lies in PHP. Over the years my project have included everything from Artificial Intelligence to Web Statistic Tracking systems, but that's the past. What matters is the future... Remember that question we were always asked in grade school? Where did we see ourselves in 10 years. Well that question was asked about 8 years ago in my life, so it looks as though I only have two more for planning stages. In two years I see myself plunging into the world of research, creating my own Research and Development firm, aiming to meet the never-ending need for new and superior software and technology. Soon after becoming a multi-billionare I'll pursue my dream of world domination. Nobody is perfect...

Actually when it comes down to things, the money has no meaning. But there you have it, a 5 minute slice of my thoughts and time... If you have any job opportunities or have the slight urge to initiate a conversation with me, it can be done via email: skonen [at] gmail [dot] com

Comments and Discussions