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