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

Aim
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.

Assumptions
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
Sofware/Component
Image
Windows XP SP2
N/A
Visual Studio Express Editions 2008 VC++
N/A
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>
     3. 
     4. using namespace std;
     5. 
     6. void ShowCurrentVersionRun();
     7. void ModifyCurrentVersionRun(int);
     8. 
     9. void main(char** argc, int argv){
    10. 
    11.     ModifyCurrentVersionRun(1);    //add a key
    12.     ShowCurrentVersionRun();
    13.     cout<<"----------------------------------------"<<endl;
    14.     ModifyCurrentVersionRun(0);    //remove the key
    15.     ShowCurrentVersionRun();
    16. }
    17. 
    18. void ShowCurrentVersionRun(){
    19.     HKEY hKey;
    20. 
    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) {
    25.     
    26.         TCHAR rval[256];
    27.         DWORD  rval_len = 256 ;
    28.         
    29.         unsigned char rdata[256];
    30.         DWORD rdata_len = 256;
    31.         
    32.         DWORD rtype = REG_SZ ;
    33.         int i = 0;
    34.         
    35.         while( RegEnumValue(hKey, i, rval, &rval_len, NULL, &rtype, 
    36.                 rdata, &rdata_len) != ERROR_NO_MORE_ITEMS){
    37.                 
    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. }
    48. 
    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) {
    55. 
    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

5 comments:

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...

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

as:
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
Training

Dot Net Interview Questions