Back to Amir Israeli Homepage

Registry Information

Every program may need to save some of it's data from time to time. even from ativation to another. Programs may save their data into external files or Registry database.
Windows encourages program - designers to use the registry as the target of their data . As long as data objects are small than 2KB. (or else you should store that item inside an external file)
This site provides 2 classes :
CRegistry : wrapper for common registry actions .
CRegistrySerialize : wrapper for "Serialization" of data into registry.

Overview
The registry is a binary database which is a hyrarchical database and is located at "System.da0" and "System.dat" file under the Windows directory .
This database is used in windows operating systems (95, 98, NT 3.51 +, 2000). Every program should use the registry as a target of saving its persistent data , instead of using an external file , by that we achieve a single standard way of saving data , sharing this data between multiple programs , a mechanism protocol of accessing data , saving it (deleing it etc ...)

Whenever you installed a program in prehistoric days ... the new program used to add line to "ini" files such as "win.ini" , whithin some time , those file became huge and loading these file have become a mission .
data wasn't installed in proper order : though "ini" files are devided to "sections" and each section includes keys and their values the order of sections is undefined and it may cause problems (some drivers require others to be loaded before others) .
sharing data was a problem and simultaneous write into files to a file was even bigger .
Another problem : Curious users tend to modify the contents as regular habit (well maybe not as a regular habit) , Having a text file in front of you , it simply makes me change it. Cahnging values of such files especially system files as "system.ini" and "win.ini" may dammage the system ... . (try it...)


Structure
The registry is a hyrerchical data structure , the basic unit is a "Key" any key has a handle , we use this handle to do any action ... (exactly like in any API function).
any key has values : these are the entries where we save data. each value has a name (we can give it any name we want) and the data that is assigned to it . we can bind : we can save many types of data , and a generic type (a binary representation of our data -simply the memory - ) if we can't find a suitable registry type . under any key each value name has a unique name , or has no name at all ( a special case of value : called the default value ) .



A View into the registry
Registry Editor\nLooks into registry Registry Editor (Microsoft)
How to use
If you want to persist a single item into registry , there are several steps for that :
1. you need to access the key where the value is stored , you first need to open it (Unless this is a root key: that you dont open) , if you failed to get the handle (therefore didnt get the standard success message -ERROR_SUCCESS :(a DWORD) you may check the error . (::getLastError() ,FormatMessage(...) )
2.if you obtained a handle you can get the value set it , delete it etc.
3.Close the key . (you must close all the keys!Except for the root keys of registry. I must add that every developement framework includes its' own functions of using the registry :
API supplies a set of REG... functions (::RegOpenKey/Ex() ,RegClose() ...) There are about 13 functions , very easy to use ... (defined in advapi.dll)
MFC includes several basic functions inside CWinApp (GetPrivateProfileInt/String ...) these functions are good for accessing the sections of an "ini" file (if CWinApp doesn't sets the registry as the source of information (SetRegistryKey(...))
ATL
Atl includes a fine class "CRegKey" to access a single key , it has an advantage of :
1.closing keys on destruction (a minor advantage)
2.It includes a function RecursiveDelete ... to recursively delete a key that has sub keys .(Win NT doent allow you to delete a key that includes sub key so you had to build a recursive function by yourself (though in this case very simple should we reinvent the wheel : especially when most MFC apps use ATL.
Java & VB
Have their own sets of function , you may import API functions.(at least on VB) Java has a full featured class ("RegKey")
A full description of the registry and an excellent tutorial is found in RegEdit Co.

Registry Value Types

REG_BINARY: Binary data in any form.
REG_DWORD: A 32-bit number.
REG_DWORD_LITTLE_ENDIAN:
A 32-bit number in little-endian format. This is equivalent to REG_DWORD: In little-endian format, a multi-byte value is stored in memory from the lowest byte (the "little end") to the highest byte. For example, the value 0x12345678 is stored as (0x78 0x56 0x34 0x12) in little-endian format. Windows NT, Windows 95, and Windows 98 are designed to run on little-endian computer architectures. A user may connect to computers that have big-endian architectures, such as some UNIX systems.
REG_DWORD_BIG_ENDIAN: A 32-bit number in big-endian format. In big-endian format, a multi-byte value is stored in memory from the highest byte (the "big end") to the lowest byte. For example, the value 0x12345678 is stored as (0x12 0x34 0x56 0x78) in big-endian format.
REG_EXPAND_SZ: A null-terminated string that contains unexpanded references to environment variables (for example, "%PATH%"). It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions. To expand the environment variable references, use the "ExpandEnvironmentStrings" function.
REG_LINK: A Unicode symbolic link.
REG_MULTI_SZ An array of null-terminated strings, terminated by two null characters. REG_NONE No defined value type.
REG_RESOURCE_LIST A device-driver resource list.
REG_SZ: A null-terminated string. It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions. Do not use the
REG_SZ type to store hard-coded paths to the system root directory or to the drive containing the system files. Instead, use the
REG_EXPAND_SZ type with environment variables such as "%SystemRoot%". This ensures that the path will be up-to-date even if the user remaps the system drive to a different drive letter.
Above are short description of the types simply taken out from the MSDN knowledge Base. (www.msdn.microsoft.com) You will find that it’s a fantastic source of information. I Inserted sign to indicate that this type is widely used. (conversions to binary and from binary streams and these types are easy. Do prefer to use this type, (Another reason to use it is the
availability of an internal Binary editor (Automated) that you can call from anywhere.


Navigation
The Registry is a hyrerchical data structure . The Registry editor shows a tree of eys in the left pane and in a list-view (right pane) shows the corresponding values and data of that values it has many branches and leaves. The registry contains 6 root keys ("Key") . A key is a name which is reffered to as a "directory" . These keys may have sub key , you may even find Some places with 10 sub levels of keys . The main key in windows operating systems are :
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS Windows NT:
HKEY_PERFORMANCE_DATA Windows 95 and Windows 98:
HKEY_DYN_DATA
As you can see there are slight differences in the main keys between different windows operating systems , In general NT registry is much more complex than 95's (98+2000) series Registries because it keeps track on different user profiles settings etc. Each one of these 6 Root keys is defined as a constant in the internal library files of windows . (Kernel.lib+dll).

Not only that each key may contain sub keys but , it may Also contain a "Value" or several "Values" . a Value has a Name which is a String (In Windows 95+98+2000) : a single Byte char, and in NT series : a Unicode Char. The length of name is up to MAX_PATH (-1 or 2) constant ( about 253 characters) (don’t use long name in your keys).
A value also contain data . That data belongs to the value and is saved on registry as a binary stream . The type of data , is also saved and you should know the type in order to convert the binary stream into that type . doing this cast should be extremely carefull because there is no limit on how much you can store there , But youre discouraged from storing huge chunks of bytes . for example storing your WallPaper is a Very Bad practice ! (don’t store bitmaps) but storing its' file path (or its relative path) is recommended .
Navigte the registry by moving into key or key up . for example : HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\Current Version\\Fonts Locates you on this specified key . This key has many values : As a matter of fact this is where windows specifies the location of installed fonts .

My System Fonts
(If you're not a hebrew speaker , you'll see many unrecognized symboles in the right pane of picture : : no worry !! thats just hebrew ...


Api Samples : Persisting Data
If you ever used the AppWizard of Microsoft Visual C++ 6.0 to build an MFC application you see that by default CWinApp sets the default location for persisting data under
"HKEY_CURRENT_USER\Software\Local AppWizard-Generated Applications"
(look in InitInstance() to function
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
(it doesn't set a whole path : the whole path is defined within the framework (checking operating system etc. look ...)

Opening a key:

LONG RegOpenKeyEx(    // taken out from MSDN
	HKEY hKey,         // handle to open key
	LPCTSTR lpSubKey,  // address of name of subkey to open
	DWORD ulOptions,   // reserved
	REGSAM samDesired, // security access mask
	PHKEY phkResult)    // address of handle to open key

Opening the key where FrontPage (also of Microsoft) sets its data: 
"HKEY_CURRENT_USER\Software\Microsoft\FrontPage\Editor" HKEY hNewKey; DWORD err; CString strRegPath = _T("Software\Microsoft\FrontPage\Editor"); if (RegOpenKeyEx( HKEY_CURRENT_USER ,strRegPath,NULL,KEY_ALL_ACCESS,&hNewKey) == ERROR_SUCESS) { //There is a value named "Show Color Coding" } else { err = ::GetLastError(); ... //mistake error }

Code in the Downloads includes extra layers that simlify working with registry ...


This was just an introduction (not a tutorial!!!) to an object class which I wrote ("CRegBase") registry base services , it uses the API and simplifies working with registry. it also includes some remarks inside the code . now moveto Downloads

Home  |  Links  |  About