Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

S.I.V.: Simple registry config class

, 1 Nov 2004
Simplicity Is Virtue: How to store your app's settings in Windows registry with minimum effort.
reg_settings_demo.zip
RegSettingsDemo.dsp
RegSettingsDemo.dsw
Debug
Release
RegSettingsDemo.exe
reg_settings_src.zip
////////////////////////////////////////////////////////////////////////////
// RegSettingsDemo.cpp : A file to show how CRegSettings class works.
//

#include "stdafx.h"
#include <conio.h>
#include "RegSettingsDemo.h"
#include "RegSettings.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/*
	a stupid struct to demonstrate usage of binary setting
*/
typedef struct tagSTUPID
{
	int something;
	long something_else;
	char whatever;
} STUPID, *LPSTUPID;

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		cerr << _T("Fatal Error: EM-EF-SEE initialization failed") << endl;
		return 1;
	}
	
	// okay, let us begin by creating all the data types
	UCHAR _uchar; char _char;
	USHORT _ushort; short _short;
	UINT _uint; int _int;
	ULONG _ulong; long _long;
	float _float;
	double _double;
	CString _string;
	STUPID _binary;

	// assign some amazingly stupid values
	_uchar = 'A';
	_char = 'B';
	_ushort = 75;	// should be L
	_short = 76;	// should be K
	_uint = 547610;
	_int = 1501;
	_ulong = 55476521L;
	_long = 50501L;
	_float = 5.1f;
	_double = 50.44;
	// this is very subtle.. (-:
	_string = "Visit http://sprdsoft.cmar-net.org";
	
	_binary.something = 5;
	_binary.something_else = 45;
	_binary.whatever = 'p';

	// create our settings object and assign values
	CRegSettings settings;
	settings.SetCompanyName( "SprdSoft Inc." );
	settings.SetAppName( "RegSettingsDemo" );
	BOOL result = 0;

	cout << "\n\n*** Now testing Write instructions...\n\n";

	cout << "Attempting to write UCHAR: \"" << _uchar << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_uchar", _uchar );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write char: \"" << _char << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_char", _char );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write USHORT: \"" << _ushort << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_ushort", _ushort );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write short: \"" << _short << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_short", _short );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write UINT: \"" << _uint << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_uint", _uint );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write int: \"" << _int << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_int", _int );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write ULONG: \"" << _ulong << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_ulong", _ulong );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write long: \"" << _long << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_long", _long );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write float: \"" << _float << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_float", _float );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write double: \"" << _double << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_double", _double );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write string: \"" << (LPCTSTR)_string << "\" . . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_string", _string );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	cout << "Attempting to write binary data: \"";
	cout << ios::hex << _binary.something;
	cout << ios::hex << _binary.something_else;
	cout << ios::hex << _binary.whatever;
	cout << "\". . .";
	result = settings.Write( "SettingsTest/Stuff/Whatever", "my_binary", &_binary, sizeof(STUPID) );
	cout << (result ? "SUCCESS" : "FAILED") << "\n";

	// some new vars
	UCHAR uchar_; char char_;
	USHORT ushort_; short short_;
	UINT uint_; int int_;
	ULONG ulong_; long long_;
	float float_;
	double double_;
	char string_[MAX_PATH];
	ULONG str_len = MAX_PATH;
	STUPID binary_;
	ULONG binary_len = sizeof(STUPID);

	uchar_ = char_ = 0;
	ushort_ = short_ = 0;
	uint_ = int_ = 0;
	ulong_ = long_ = 0;
	float_ = 0.0f;
	double_ = 0.0;
	memset( (void*)string_, 0, MAX_PATH );
	memset( (void*)&binary_, 0, sizeof(binary_) );

	// pause a bit
	cout << "\n\n\tPress any key to continue\n\tor any other key to quit..";
	getch();

	// now do the read test
	cout << "\n\n*** Now testing Read instructions...\n\n";

	cout << "Attempting to read uchar...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_uchar", uchar_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << uchar_ << "\"\n";
	
	cout << "Attempting to read char...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_char", char_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << char_ << "\"\n";
	
	cout << "Attempting to read ushort...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_ushort", ushort_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << ushort_ << "\"\n";
	
	cout << "Attempting to read short...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_short", short_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << short_ << "\"\n";
	
	cout << "Attempting to read uint...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_uint", uint_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << uint_ << "\"\n";
	
	cout << "Attempting to read int...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_int", int_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << int_ << "\"\n";
	
	cout << "Attempting to read ulong...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_ulong", ulong_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << ulong_ << "\"\n";
	
	cout << "Attempting to read long...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_long", long_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << long_ << "\"\n";
	
	cout << "Attempting to read float...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_float", float_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << float_ << "\"\n";
	
	cout << "Attempting to read double...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_double", double_ );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << double_ << "\"\n";
	
	cout << "Attempting to read string...";
	result = settings.Read( "SettingsTest/Stuff/Whatever", "my_string", string_, str_len );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"") << string_ << "\"\n";
	
	////////////////////////////////////////////////////////
	// sadly, we need to do static_cast below, probably
	// because SOME compilers are STUPID (-:
	// VC wants a pointer to char there. Either the VC
	// compiler is stupid or I'm missing something here...
	// NOTE:	the class will remove "//" from the beggining of key
	//			string so no error occurs here
	cout << "Attempting to read binary...";
	result = settings.Read( "//SettingsTest/Stuff/Whatever", "my_binary", static_cast<void*>(&binary_), binary_len );
	cout << (result ? "SUCCESS: \"" : "FAILURE: \"");
	cout << ios::hex << _binary.something;
	cout << ios::hex << _binary.something_else;
	cout << ios::hex << _binary.whatever;
	cout << "\"\n";

	cout << "\nDeleting all settings...\n";
	result = settings.DeleteAll(TRUE);
	cout << (result ? "SUCCESS" : "FAILURE");
	
	// pause a bit
	cout << "\n\n\tPress any key to continue\n\tor any other key to quit..";
	getch();

	cout << "\n\n*** Now looking for trouble...(-:\n\n";
	CRegSettings eddie;

	cout << "Attempting to write without settings company/app name:\n";

	result = eddie.Write( "WaZZZAAAA/YEEEEHAAAA\\", "DOODE", 45 );

	if ( result != TRUE )	// this should always be true
	{
		CString sz = eddie.GetLastErrorText();
		cout << "Last error returns: " << (LPCTSTR)sz;
	}

	eddie.SetCompanyName( "SprdSoft Incorporated" );

	cout << "Attempting to read non existant key:\n";

	int useless = 0;
	result = eddie.Read( "Ye/CO\\UYA", "QUPUs", useless );

	if ( result != TRUE )
	{
		CString sz = eddie.GetLastErrorText();
		cout << "Last error returns: " << (LPCTSTR)sz;
	}

	cout << "\n\n*** End of all tests.";
	cout << "\n\tWhen the toughing gets go, the go get toughing.\n\n";

#ifndef _DEBUG
	// pause a bit
	cout << "\n\n\tPress any key to continue\n\tor any other key to quit..";
	getch();
#endif //_DEBUG

	return 0;
}


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

Share

About the Author

T1TAN
Web Developer
Croatia Croatia
A software developer for the masses..

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 2 Nov 2004
Article Copyright 2004 by T1TAN
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid