Enumerating, Deleting and Creating Registry Entries in Windows C++

The aim of this example is to demonstrate programatic registry manipulation in C++ on Windows. This will use functions such as RegCreateKey, RegSetValueEx and RegDeleteValue. Leave any questions or problems as comments and I will endeavour to answer them.

This article assumes that you have VC++ installed and configured. See Problems and Solutions Installing VC++ Express Edition, to install and run vcvars32.bat.

Versions used in this example
Windows XP SP2
Visual Studio Express Editions 2008 VC++
Links to these files can be found here

You can use either regedit or regedt32 to view the results.

Write and compile the application
  1. This example will display the registry under SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run. It will then add and delete registry values. Save the code below as registryhack.cpp.
     1. #include <windows.h>
     2. #include <iostream>
     4. using namespace std;
     6. void ShowCurrentVersionRun();
     7. void ModifyCurrentVersionRun(int);
     9. void main(char** argc, int argv){
    11.     ModifyCurrentVersionRun(1);    //add a key
    12.     ShowCurrentVersionRun();
    13.     cout<<"----------------------------------------"<<endl;
    14.     ModifyCurrentVersionRun(0);    //remove the key
    15.     ShowCurrentVersionRun();
    16. }
    18. void ShowCurrentVersionRun(){
    19.     HKEY hKey;
    21.     if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
    22.                 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
    23.                 0, NULL, 0, KEY_ALL_ACCESS, NULL,
    24.                 &hKey, NULL) == ERROR_SUCCESS) {
    26.         TCHAR rval[256];
    27.         DWORD  rval_len = 256 ;
    29.         unsigned char rdata[256];
    30.         DWORD rdata_len = 256;
    32.         DWORD rtype = REG_SZ ;
    33.         int i = 0;
    35.         while( RegEnumValue(hKey, i, rval, &rval_len, NULL, &rtype, 
    36.                 rdata, &rdata_len) != ERROR_NO_MORE_ITEMS){
    38.             cout<<rval<<" : "<<rdata<<endl;
    39.             i++;
    40.             rval_len=256;
    41.             rdata_len=256;
    42.             memset(rval, '\0', rval_len);
    43.             memset(rdata, '\0', rdata_len);
    44.         }
    45.         RegCloseKey (hKey);    
    46.     }
    47. }
    49. void ModifyCurrentVersionRun(int install){
    50.     HKEY hKey;
    51.     if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
    52.                 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
    53.                 0, NULL, 0, KEY_ALL_ACCESS, NULL,
    54.                 &hKey, NULL) == ERROR_SUCCESS) {
    56.         TCHAR* rval = "My Dummy Key";
    57.         TCHAR* rdata = "************* My Dummy Data ************* ";
    58.         if(install)
    59.             RegSetValueEx (hKey, rval, 0, REG_SZ, (PBYTE)rdata,    strlen(rdata));
    60.         else
    61.             RegDeleteValue(hKey, rval);
    62.         RegCloseKey (hKey);    
    63.     }
    64. }
    Hide line numbers

  2. Open a prompt to the working directory and compile the code using the windows cl.exe compiler. You just need to include the advapi32.lib.

    ..workspace\RegistryHack>cl registryhack.cpp advapi32.lib

  3. Now run the program in the command prompt. You will see two emumerations. The fisrt one has the "dummy" entry added and in the second one it is removed. You can modify the code and check the results using regedit or regedt32


vitamin k said...

The most advanced Windows CE developers guide ever published!
Build world-class Windows CE apps with Visual C++ and MFC!
Goes far beyond graphical interfaces! In-depth coverage of networking, database access, desktop interoperability, and much more!
CD-ROM contains all sample code from the book -- including easy-to-reuse networking, communications, and synchronization code!

Anonymous said...

I am not able to run you program but getting error for 'RegCreateKeyEx' in ModifyCurrentVersionRun Function.

unused CXX0030: Error: expression cannot be evaluated

righteous said...

It's been too long since I've even looked at a bit of windows code. Have you tried googling the error?

sauravsharma said...

what if i want an .exe file to get attached to that entry so that the .exe automatically starts up??

Naviya Nair said...

Very interesting and good Explanation
ASP NET Training
ASP NET Training
ASP NET Online Training
C-Sharp Training
Dot Net Training in Chennai
Online .Net Training

MVC Training
WCF Training
Web-API Training
LINQ Training
Entity Framework

Dot Net Interview Questions