Using registry has become a regular
practice for all the programmers. The best way to store all
configurations in the old days was Registry only. Even now
System programs use registry for storing the configuration
values. But the renewed vigor of XML might replace the whole
concept of the Registry Configurations.
Whatever be the case, at time Registry comes as a
handy tool for a lot of jobs. Use this link to create
registry key, set registry value.
Enumerating the values under a Registry Key:
There are three major steps involved in
Enumerating values from registry.
1. Open the Registry key
2. Loop and print using RegEnumKey till the
return value is not equal to ERROR_SUCCESS.
3. Close the Registry key
#include <windows.h>
#include <stdio.h>
void main()
{
char lszValue[100];
LONG lRet, lEnumRet;
HKEY hKey;
DWORD dwLength=100;
int i=0;
lRet = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
"SOFTWARE\\ADOBE", 0L, KEY_READ , &hKey);
if(lRet == ERROR_SUCCESS)
{
lEnumRet =
RegEnumKey (hKey, i,lszValue,dwLength);
while(lEnumRet == ERROR_SUCCESS)
{
i++;
printf ("%s\n",lszValue);
lEnumRet = RegEnumKey (hKey, i,lszValue,dwLength);
}
}
RegCloseKey(hKey);
}
Accessing a REG_DWORD value from Registry:
Accessing a DWORD from registry is simple. Open
the Registry key and read it using RegQueryValueEx.
#include <windows.h>
#include <stdio.h>
void main()
{
DWORD dwVersion;
HKEY hKey;
LONG returnStatus;
DWORD dwType=REG_DWORD;
DWORD dwSize=sizeof(DWORD);
returnStatus =
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Path", 0L,
KEY_ALL_ACCESS, &hKey);
if (returnStatus = =
ERROR_SUCCESS)
{
returnStatus =
RegQueryValueEx(hKey, "Value", NULL, &dwType,(LPBYTE)&dwVersion,
&dwSize);
if (returnStatus ==
ERROR_SUCCESS)
{
printf("REG_DWORD value is %d\n", dwVersion);
}
}
RegCloseKey(hKey);
}
Note: The only error that might crop up during the usage of
this function is due to assigning a wrong value for the dwSize.
If the dwSize is smaller than the queried value, then the
RegQueryEx will fail. This happens especially during repeated
mixed usage of the same variables for reading DWORD as well as
character arrays. This is very much untraceable and might also
get missed during testing. So it is better to check this
problem during coding itself.
Accessing a REG_SZ from Registry:
#include <windows.h>
#include <stdio.h>
void main()
{
char lszValue[255];
HKEY hKey;
LONG returnStatus;
DWORD dwType=REG_SZ;
DWORD dwSize=255;
returnStatus =
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MICROSOFT\\INETMGR\\PARAMETERS",
0L, KEY_ALL_ACCESS, &hKey);
if (returnStatus == ERROR_SUCCESS)
{
returnStatus = RegQueryValueEx(hKey, "HelpLocation",
NULL, &dwType,(LPBYTE)&lszValue, &dwSize);
if (returnStatus
== ERROR_SUCCESS)
{
printf("Value Read is %s\n", lszValue);
}
}
RegCloseKey(hKey);
}