Code Snippets
Code Samples

The Blog
Our Projects

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

Tutorial - Registry
Uploaded at:29-May-03
The structure of the Registry:
The Registry has a hierarchal structure, like the directories and files on your hard disk. Each directory is called a Key.
Each key can contain other keys, as well as Values. Each value contains the actual information stored in the Registry.
There are three types of values; String, Binary, and DWORD - the use of these depends upon the context.
There are six main keys, each containing a specific portion of the information stored in the Registry.

HKEY_CLASSES_ROOT - Contains all of your file types and OLE information for all your OLE-aware applications.
HKEY_CURRENT_USER - Points to the part of HKEY_USERS appropriate for the current user.
HKEY_LOCAL_MACHINE - Contains information about all of the hardware and software installed on your computer.
Since you can specify multiple hardware configurations, the current hardware configuration is specified in HKEY_CURRENT_CONFIG.
HKEY_USERS - Contains certain preferences (such as colors and control panel settings) for each of the users of the computer.
HKEY_CURRENT_CONFIG - Points to the part of HKEY_LOCAL_MACHINE appropriate for the current hardware configuration.
HKEY_DYN_DATA - This branch points to the part of HKEY_LOCAL_MACHINE, for use with the Plug-&-Play feature.

Now, why do we need the Registry? Well, remember those old .INI files?
They all are now gathered in the registry!
Ofcourse the .INI files are still exist.
It's more orginized, it's faster and it's easier to access!
So if you are used to save your app's settings in messy .INI files, meet the neat usage of the registry:

//Every key you open/create you have to close it, like a file.

 HKEY hregkey; // Declaring a key pointer for usage.

 // Here, we create a directory called: "TESTING"
 long res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "Software\\TESTING",
                           NULL, NULL, NULL, KEY_WRITE, NULL, &hregkey, &IsNew);
 if (res == 0) // Creatation  was successfull
  // Storing a string
  char str[256];
  strcpy(str, "Hello Registry");
  // REG_SZ means a string is to be saved
  RegSetValueEx(hregkey, "String", 0, REG_SZ, (const unsigned char *)str, strlen(str));
  // Now a number
  int num = 128;
  // REG_DWORD is a long type number
  RegSetValueEx(hregkey, "Number", 0, REG_DWORD, (const unsigned char *)&num, sizeof(num));
  //Don't forget to close the key...

 res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\TESTING", 0, KEY_ALL_ACCESS, &hregkey);
 //Notice we now call RegOPENKeyEx...

 if (res == 0)
  // Let's read the string first
  int datasize = 256; // The maximum length of temp;
  RegQueryValueEx(hregkey, "String",
                  NULL, NULL, (unsigned char *)&temp, (unsigned long *)&datasize);
  // Now the number
  datasize = sizeof(int); // The maximum length of getnum
  RegQueryValueEx(hregkey, "Number",
                  NULL, NULL, (unsigned char *)&getnum, (unsigned long *)&datasize);

  //Now let's delete the Number item
  RegDeleteValue(hregkey, "Number");

  //Now let's delete the directory we created, "TESTING"
  RegDeleteKey(HKEY_LOCAL_MACHINE, "Software\\TESTING");

  // Well we don't need to call RegCloseKey 'cause the key is deleted already..
  // But if you don't delete the key don't forget to call it

  // RegCloseKey(hregkey);

That's it...Very easy and useful.

Source Code:
Visual C++ 6 - Registry.cpp

Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
::Top 5 Tutorials::
Embedded Python[119195]
2D Rotated Rectangles Collision Detection[90098]
Keyboard Hook[78635]
HTTP Proxy[41966]

::Top 5 Samples::
2D ColDet Rotated Rectangles[11602]
PS2 Mouse Driver[7002]
Wave Format Player[5828]
Reading FAT12[5652]

All rights reserved to RageStorm © 2009