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


Comments

  • Updated version available

    Posted by Legacy on 06/26/2001 12:00am

    Originally posted by: Frank Melber

    Hi folks,
    I have an updated version which features read/write raw data and implements optional encryption using the CryptoAPI. Further on access rights are added ;-).
    Send me an email if you're interested in it.

    Reply
  • Thank you!!!

    Posted by Legacy on 04/23/2001 12:00am

    Originally posted by: kyusun

    good source!!!
    Thank you very much~~~~*^^*

    Reply
  • Access rights should be added

    Posted by Legacy on 01/13/2000 12:00am

    Originally posted by: Stefan Migowsky

    Hi,

    access rights should be added as a default parameter to
    all the functions, instead of assumming KEY_ALL_ACCESS.
    This will cause an error if the owner of the process does
    not have admin rights on NT and wants to read initial values
    from HKEY_LOCAL_MACHINE.

    Stefan

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date