Template based Registry class with serialization | CodeGuru

Template based Registry class with serialization

It was inspired by the registry API wrapper class by Shane Martin which you can find here at codeguru. Environment: VC6 SP3, NT4 SP5 I first used the registry class provided by Shane. But – for my purposes – I needed other functionality. So I wrote my own code. This class now provides the capability […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 5, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

It was inspired by the registry API wrapper class by
Shane Martin
which you can find
here
at codeguru.

Environment: VC6 SP3, NT4 SP5

I first used the registry class provided by Shane. But – for my purposes – I needed other functionality. So I wrote my own code.
This class now provides the capability of reading and writing any kind of object which supports archive operation (template based).
Some of these are:

  • BYTE
  • WORD
  • int
  • LONG
  • DWORD
  • float
  • double
  • SIZE and CSize
  • CString
  • POINT and CPoint
  • RECT and CRect
  • CTime and CTimeSpan

It further provides the capability to serialize object structures to the registry using the objects “Serialize(CArchive& ar)” function.
(Be sure that these structures are smaller than 2048 bytes – otherwise an *.ini file or something would be better.)

Another feature is that you can save and resore the settings of the application window(s) by simply passing a pointer to the “Read” or “Write” Function.

Usage is very simple…

	CRegistry reg;
	CString sRegPath("YourApp\Path\Settings");

	CString 	sString("I am a String");
	CString 	sStringBin("I am a binary String");
	int		nValue = -1234567890;
	double		dValue = -1234567890.1234;
	DWORD		dwValue = 1234567890;
	DWORD		dwValueBin = 1234567890;
	CRect		rcWnd;
	CYourObject 	object;

	// Fill the rcWnd...
	GetWindowRect(rcWnd);

	// Check the key...
	if (!reg.VerifyKey(HKEY_CURRENT_USER, sRegPath))
		reg.CreateKey(HKEY_CURRENT_USER, sRegPath);

	// Open...
	reg.Open(HKEY_CURRENT_USER, strRegPath);

	// Writing/Reading as "String"...
	reg.WriteString("CString", sString);
	reg.ReadString("CString", sString);

	// Writing/Reading String as "binary" (template function)...
	reg.WriteType("CStringBin", sStringBin);
	reg.ReadType("CStringBin", sStringBin);

	// Writing/Reading int as "binary" (template function)...
	reg.WriteType("int", nValue);
	reg.ReadType("int", nValue);

	// Writing/Reading double as "binary" (template function)...
	reg.WriteType("double", dValue);
	reg.ReadType("double", dValue);

	// Writing/Reading DWORD...
	reg.WriteDWORD("DWORD", dwValue);
	reg.ReadDWORD("DWORD", dwValue);

	// Writing/Reading DWORD as "binary" (template function)...
	reg.WriteType("DWORDBin", dwValueBin);
	reg.ReadType("DWORDBin", dwValueBin);

	// Writing/Reading CRect as "binary" (template function)...
	reg.WriteType("CRect", rcWnd);
	reg.ReadType("CRect", rcWnd);

	// Writing/Reading Window (WINDOWPLACEMENT)...
	reg.Write("Wnd", this);
	reg.Read("Wnd", this);

	// Writing/Reading CObject...
	reg.Write("Object", object);
	reg.Read("Object", object);

	// Not necessary (object is automatically closed at destruction)
        reg.Close();

For further explanation look at the sample application.
If you have any questions or some ideas how to improve this, bugs, errors, etc. feel free to send me a mail.

Downloads

Download demo project – 24 Kb
Download source – 3 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.