Programming the windows registries
Windows registries are central database which contains various information. Programming windows registry is one task you need to be extra careful about, as a slight error in the programming technique could lead to dangerous results. Before I biggen with the various programming technique it is always advisable for a novice programmer to store the registry before working with registries.
Storing windows in windows 98 window me is pretty simple all you need to do is to boot your system in ms-dos mode and make a back up of the following files.
- %winDir%\system.dat
- %WinDir%\user.dat
Where as storing registries in window 2000 / XP and server 2003 is little trivial either you can create a system restore point or another option is to log on to recovery console. Go to %systemroot%\system32\config and back up the all the files in that folder. That folder would contain registry file, user files, log files and alternate registry files. And in case of a crash first try to recover from the system restore in safe mode, if that's not working go to recovery console and recover the files that you have stored.
Programming registries using win32 api's
Win32 api's are core function provided by Microsoft which are used by end programmers to program windows operating system. These api's are packed in various dll's (dynamic linking library ) which are shipped in windows operating system. Windows itself uses these set of api for running their operating system smoothly.
Registry api's are those set of function provided by Microsoft for programming windows registries. These fuction are provided are defined in winreg.h and are contained in Advapi32.lib. This article would briefly describe these api's.
Below given are the registry api's with their brief dricption.
| RegCloseKey |
Releases a handle to the specified registry key. |
| RegConnectRegistry |
Establishes a connection to a predefined registry handle on another computer. |
| RegCreateKeyEx |
Creates the specified registry key. |
| RegDeleteKey |
Deletes a subkey. |
| RegDeleteValue |
Removes a named value from the specified registry key. |
| RegDisable
PredefinedCache |
Disables the predefined registry handle table of HKEY_CURRENT_USER for the specified process. |
| RegEnumKeyEx |
Enumerates subkeys of the specified open registry key. |
| RegEnumValue |
Enumerates the values for the specified open registry key. |
| RegFlushKey |
Writes all the attributes of the specified open registry key into the registry. |
| RegGetKeySecurity |
Retrieves a copy of the security descriptor protecting the specified open registry key. |
| RegLoadKey |
Creates a subkey under HKEY_USERS or HKEY_LOCAL_MACHINE and stores registration information from a specified file into that subkey. |
| RegNotifyChangeKeyValue |
Notifies the caller about changes to the attributes or contents of a specified registry key. |
| RegOpenCurrentUser |
Retrieves a handle to the HKEY_CURRENT_USER key for the user the current thread is impersonating. |
| RegOpenKeyEx |
Opens the specified registry key. |
| RegOpenUserClassesRoot |
Retrieves a handle to the HKEY_CLASSES_ROOT key for the specified user. |
| RegOverridePredefKey |
Maps a predefined registry key to a specified registry key. |
| RegQueryInfoKey |
Retrieves information about the specified registry key. |
| RegQueryMultipleValues |
Retrieves the type and data for a list of value names associated with an open registry key. |
| RegQueryValueEx |
Retrieves the type and data for a specified value name associated with an open registry key. |
| RegReplaceKey |
Replaces the file backing a registry key and all its subkeys with another file. |
| RegRestoreKey |
Reads the registry information in a specified file and copies it over the specified key. |
| RegSaveKey |
Saves the specified key and all of its subkeys and values to a new file. |
| RegSetKeySecurity |
Sets the security of an open registry key. |
| RegSetValueEx |
Sets the data and type of a specified value under a registry key. |
| RegUnLoadKey |
Unloads the specified registry key and its subkeys from the registry. |
Above mentioned api will return ERROR_SUCCESS on success. Below Given is an example of registry programming done in vc ++ using above mention api's The following example uses the GetVersionEx function to display the version of the currently running operating system
#include
#include
#define BUFSIZE 80
int main() {
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
// Try calling GetVersionEx using the OSVERSIONINFOEX structure.
// If that fails, try using the OSVERSIONINFO structure.
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
return FALSE;
}
switch (osvi.dwPlatformId)
{
// Test for the Windows NT product family.
case VER_PLATFORM_WIN32_NT:
// Test for the specific product family.
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
printf ("Microsoft Windows Server 2003 family, ");
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
printf ("Microsoft Windows XP ");
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
printf ("Microsoft Windows 2000 ");
if ( osvi.dwMajorVersion <= 4 )
printf("Microsoft Windows NT ");
// Test for specific product on Windows NT 4.0 SP6 and later.
if( bOsVersionInfoEx )
{
// Test for the workstation type.
if ( osvi.wProductType == VER_NT_WORKSTATION )
{
if( osvi.dwMajorVersion == 4 )
printf ( "Workstation 4.0 " );
else if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
printf ( "Home Edition " );
else
printf ( "Professional " );
}
// Test for the server type.
else if ( osvi.wProductType == VER_NT_SERVER )
{
if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
printf ( "Datacenter Edition " );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
printf ( "Enterprise Edition " );
else if ( osvi.wSuiteMask == VER_SUITE_BLADE )
printf ( "Web Edition " );
else
printf ( "Standard Edition " );
}
else if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
printf ( "Datacenter Server " );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
printf ( "Advanced Server " );
else
printf ( "Server " );
}
else // Windows NT 4.0
{
if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
printf ("Server 4.0, Enterprise Edition " );
else
printf ( "Server 4.0 " );
}
}
}
else // Test for specific product on Windows NT 4.0 SP5 and earlier
{
HKEY hKey;
char szProductType[BUFSIZE];
DWORD dwBufLen=BUFSIZE;
LONG lRet;
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return FALSE;
lRet = RegQueryValueEx( hKey, "ProductType", NULL, NULL,
(LPBYTE) szProductType, &dwBufLen);
if( (lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE) )
return FALSE;
RegCloseKey( hKey );
if ( lstrcmpi( "WINNT", szProductType) == 0 )
printf( "Workstation " );
if ( lstrcmpi( "LANMANNT", szProductType) == 0 )
printf( "Server " );
if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
printf( "Advanced Server " );
printf( "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion );
}
// Display service pack (if any) and build number.
if( osvi.dwMajorVersion == 4 &&
lstrcmpi( osvi.szCSDVersion, "Service Pack 6" ) == 0 )
{
HKEY hKey;
LONG lRet;
// Test for SP6 versus SP6a.
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009",
0, KEY_QUERY_VALUE, &hKey );
if( lRet == ERROR_SUCCESS )
printf( "Service Pack 6a (Build %d)\n", osvi.dwBuildNumber & 0xFFFF );
else // Windows NT 4.0 prior to SP6a
{
printf( "%s (Build %d)\n",
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
}
RegCloseKey( hKey );
}
else // Windows NT 3.51 and earlier or Windows 2000 and later
{
printf( "%s (Build %d)\n",
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
}
break;
// Test for the Windows 95 product family.
case VER_PLATFORM_WIN32_WINDOWS:
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
printf ("Microsoft Windows 95 ");
if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
printf("OSR2 " );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
printf ("Microsoft Windows 98 ");
if ( osvi.szCSDVersion[1] == 'A' )
printf("SE " );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
printf ("Microsoft Windows Millennium Edition\n");
}
break;
case VER_PLATFORM_WIN32s:
printf ("Microsoft Win32s\n");
break;
}
return TRUE;
}
Programming Registries on .net platform v1 .1
Programming windows registries on a .net platform is not as trivial as it is on core win32 api's .NET provide classes for working with windows registries. Class provided under namespace called Microsoft.win32. In this name space there are two classes
1. Microsoft.win32.Registry
2. Microsoft.win32.Registrykey
Microsoft.win32.registry
This class work with standard root keys (Hives) found on the system. Members of this class is are given below.
Back |