Click here to Skip to main content
13,050,904 members (64,481 online)
Click here to Skip to main content


34 bookmarked
Posted 24 Jun 2004

A class for reading and writing the Windows Registry using VB.NET

, 24 Jun 2004
Reading and writing the Windows Registry overcoming the restrictions imposed by GetSetting and SaveSetting.
// CRegEntry: interface for the CRegEntry class.

#if !defined(_REGENTRY_H_INCLUDED)

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

class CRegistry;

class CRegEntry

	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.


	// 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)

	// 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); }


	CRegistry*	__cregOwner;
	bool		__bConvertable;
	bool		__bStored;


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

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


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sinhue Baez
Web Developer
Mexico Mexico
Sinhue Baez is a systems engineer. He started with computers in 1990, since then, he has programmed in BASIC, C/C++, COBOL, Pascal, Visual Basic, Java, Visual C++ and currently he is working using VB .NET and C#.
In his spare time he likes to code videogame demos with Visual C++ and Direct X.
He works as a project leader developing software for the production area of a food company.
Currently he is studying his master degree in Management Engineering.
He enjoys playing basketball, running and writing short stories.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 25 Jun 2004
Article Copyright 2004 by Sinhue Baez
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid