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

A handy class to make use of Windows Registry

, 15 Aug 2004
Shows how simple accessing Windows Registry can be if you do not need bells and whistles.
simple_registry_demo.zip
a_better_example.exe
dirty_and_fast.exe
main.exe
with_trace.exe
simple_registry_src.zip
dirty_and_fast
dirty_and_fast.dsp
with_trace
with_trace.dsp
main.dsp
main.dsw
a_better_example
a_better_example.dsp
//Long story:

#include "registry.h"
#include "string.h"

struct ServerAddress{
	char name[64];
	char ip[15];
	short port;
};

int main(int,char*){

	//first step is to create our home registry 
	//record (or open it if exists)
	//the constructor accepts a string and if you do:
	registry settings("Software\\MyCoolApp");
	//you open HKEY_CURRENT_USER\Sowtware\MyCoolApp

	//Due to simplicity this class is desined to deal with only 
	//HKEY_CURRENT_USER registry group. 
	//You may, however, modify this behavior if you need

	//To make sure that the home key has been created or 
	//opened you can test it like this:

	if(!settings){
		//error code goes here...
		exit(1);
	}

	//The next step is to create (or open if exists) 
	//a value key:
	//the official way is like this: (for another, 
	//simpler way, read below.)

	registry::iterator lang=settings["Language"];

	//and then you can retrive what was selected as 
	//the default language as follows:

	printf("The selected language is: \"%s\"\n",(const char*)lang);

	//if you want to modify a value you can do simply like this:

	lang="Russian";

	//Internally this class stores values as 
	//binary data and therefore 
	//you are not limited to strings only.

	// Example:

	ServerAddress MyCoolServer;
	strcpy(MyCoolServer.name,"somewhere.example.com");
	strcpy(MyCoolServer.ip,"192.168.0.1");
	MyCoolServer.port=1024;

	//now let's put this record into registry key "server"
	//it can be simply done like this:

	registry::iterator server=settings["server"];
	server=std::string((const char *)(&MyCoolServer),sizeof(MyCoolServer));

	//now let's test by retriving the structure back:

	ServerAddress serAdr;
	const std::string &FromRegistry=server;
	//copy data only if it's of the size of the struct ServerAddress
	if(FromRegistry.length()==sizeof(ServerAddress)){		
		memcpy(&serAdr,FromRegistry.c_str(), sizeof(ServerAddress));
		//now let's see what we have:
		printf("Print out for my server:\n");
		printf("\tserver's host name is:  %s\n",    serAdr.name);
		printf("\tserver's ip address is: %s\n",    serAdr.ip);
		printf("\tserver's port is:       %u\n\n",  serAdr.port);
	}else{
		//error code goes here...
	}
	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

__PPS__

Canada Canada
Big Grin | :-D

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 16 Aug 2004
Article Copyright 2004 by __PPS__
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid