Working with the Windows Registry in C++ - Basic Registry Functions
(Page 2 of 4 )
During this tutorial we are going to learn how to read from, modify, and write to the registry. In this section we’ll cover some of the basic functions for opening subkeys and working with them, while in the next section we’ll code our registry dump application. Launch your Visual Studio IDE and create a new Win32 console application. Don’t forget to check the checkboxes of precompiled header files as well as MFC.
The functions we’re going to work with can be found in the following places: windows.h header file, Advapi32.lib library file requiring Advapi32.dll. And they work with any NT-based Windows OS such as 2000, XP, 2003, Vista, and 2008 Server.
Our first function is RegOpenKeyEx(). This function opens a specified registry key and returns ERROR_SUCCESS on success. It’s important to understand that this function only opens the desired key if it already exists; it does not create a new key if the specified key is nonexistent. Let’s see how to use it.
HKEY hKey;
If (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SoftwareDevShed TutorialTest"), 0, KEY_ALL_ACCESS, &hKey)!=ERROR_SUCCESS)
printf("nError opening the desired subkey (doesn't exist?).n");
Else
Printf("nSucceess!");
In the above example we’ve also declared an hKey variable that’s HKEY type. It will serve us for the handle of the specified key. Also, as you can see we’ve opted for the KEY_ALL_ACCESS type of permission so that we could have full access.
Now let’s add some functionality to it. We’ll use the RegSetValueEx() function to create a new value in our hKey. For the purpose of this article, we are going to create a new string value.
unsigned char szStr[2];
szStr[0]='1'; szStr[1]='';
if (RegSetValueEx(hKey, TEXT("String Value"), NULL, REG_SZ, szStr, sizeof(szStr))==ERROR_SUCCESS)
printf("nThe value of the key was set successfully.n");
else
printf("nError setting the value of the key.n");
RegCloseKey(hKey);
The above code snippet is pretty straightforward. We declared an unsigned char array with two elements and we built it up with a ‘1’ and the EOF character. This variable will serve as the data in our string value. We create the new “String Value” value using the RegSetValueEx() function. Reg_SZ stands for the null-terminated string value data type. At the end we also close this hKey because we’ve finished dealing with it.
Now let’s see how can we create a new subkey ourselves and then do some deleting too. The RegCreateKeyEx() function will be used to create a new key, while for deleting we’ll opt for RegDeleteValue(). If you are using Vista or Win2008 Server then you may also use RegDeleteTree(). With it you delete a whole tree. It’s not present in earlier versions of Windows. But we’ll still present its usage.
DWORD dwDisposition;
HKEY hKey;
RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("SoftwareDevShed TutorialTestAnother SubKey"), 0, NULL, 0, 0, NULL, &hKey, &dwDisposition);
if (dwDisposition != REG_CREATED_NEW_KEY && dwDisposition != REG_OPENED_EXISTING_KEY)
printf("nError creating the desired key (permissions?).n");
else
printf("nThe key was successfully created.n");
RegCloseKey(hKey);
The dwDisposition DWORD variable is used to identify whether the key has been created (if it was nonexistent) or if it already existed, and whether it could be opened successfully with writing permissions. After this you may add the required actions. Don’t forget to close the key.
Finally, let’s find out how to “clean up” the registry by learning delete functions.
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SoftwareDevShed TutorialTest"), 0, KEY_ALL_ACCESS, &hKey)==ERROR_SUCCESS)
{
if (RegDeleteValue(hKey, TEXT("String Value"))==ERROR_SUCCESS)
printf("nString Value value successfully removed.n");
else
printf("nError removing the specified value. n");
//--->>> requires Windows VISTA and/or Windows 2008 Server <<<---
#if WINVER >= 0x0600
{
printf("nWindows Vista or Win2008 Server Platform detected.");
if (RegDeleteTree(hKey, NULL)==ERROR_SUCCESS)
printf("nThe subkey we created was removed as a tree.");
else
printf("nError removing the specified subkey tree.");
}
#else
printf("nSkipping this part - no Vista or Win2008 Server.n");
#endif
//--->>> requires Windows VISTA and/or Windows 2008 Server <<<---
}
else
printf("nError opening the specified subkey path (doesn't exist?).n");
RegCloseKey(hKey);
The above code snippet opens our DevShed TutorialTest subkey and then deletes our “String Value” value that we created a while ago if you still remember. Of course, as you can see we followed these actions with error handling so that if something goes wrong you can immediately locate the source of problem. Ultimately, if you are on a Vista-based OS then you can use the RegDeleteTree() too.
By now you should understand how to open and create keys and values, work with them, and delete them. Now we can move on and create a recursive method that exports each and every subkey of a particular (sub)key. Additionally, we’ll code our registry dumper, too. There we will also learn about privileges and tokens!
Next: More Registry Stuff >>
More Windows Scripting Articles
More By Barzan "Tony" Antal