Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. deleting a registry key

deleting a registry key

Scheduled Pinned Locked Moved C / C++ / MFC
windows-adminquestionlearning
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nss
    wrote on last edited by
    #1

    I want to check the registry to see if a certain key TestViewBox exists. If it does, then I want to delete it (so all the stuff under it vanishes too - correct? ). Then I want to recreate it, and put in fresh entries under that key (a subkey and then values). I'm thinking:

    	LONG res = RegOpenKeyEx(keyHandle1, "TestViewBox",
    	0, KEY\_ALL\_ACCESS, &keyHandle2);
               
                if (res)
    
                   {// means it exists, so delete it........`I need to know how` ); 
    
                //if its now been deleted or never existed then recreate it etc:
    
            
            RegCreateKeyEx(keyHandle1, "TestViewBox", 0, "", 
            REG\_OPTION\_NON\_VOLATILE, KEY\_ALL\_ACCESS, NULL,
            &keyHandle2, &disp);
    
         
           RegCreateKeyEx(keyHandle2, "MyTestApp", 0, "",
           REG\_OPTION\_NON\_VOLATILE, KEY\_ALL\_ACCESS, NULL,
           &keyHandle3, &disp);
    
           RegSetValueEx(keyHandle3, NumberOfBoxes,
       0, REG\_DWORD, (BYTE\*)&numberOfBoxes, sizeof(DWORD));
    

    {

    Of course I will be checking the returnvalues from each registry function....:)

    L S 2 Replies Last reply
    0
    • N nss

      I want to check the registry to see if a certain key TestViewBox exists. If it does, then I want to delete it (so all the stuff under it vanishes too - correct? ). Then I want to recreate it, and put in fresh entries under that key (a subkey and then values). I'm thinking:

      	LONG res = RegOpenKeyEx(keyHandle1, "TestViewBox",
      	0, KEY\_ALL\_ACCESS, &keyHandle2);
                 
                  if (res)
      
                     {// means it exists, so delete it........`I need to know how` ); 
      
                  //if its now been deleted or never existed then recreate it etc:
      
              
              RegCreateKeyEx(keyHandle1, "TestViewBox", 0, "", 
              REG\_OPTION\_NON\_VOLATILE, KEY\_ALL\_ACCESS, NULL,
              &keyHandle2, &disp);
      
           
             RegCreateKeyEx(keyHandle2, "MyTestApp", 0, "",
             REG\_OPTION\_NON\_VOLATILE, KEY\_ALL\_ACCESS, NULL,
             &keyHandle3, &disp);
      
             RegSetValueEx(keyHandle3, NumberOfBoxes,
         0, REG\_DWORD, (BYTE\*)&numberOfBoxes, sizeof(DWORD));
      

      {

      Of course I will be checking the returnvalues from each registry function....:)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If RegOpenKeyEx does not fail it means that the key exists. You can then delete it with RegDeleteKey

      1 Reply Last reply
      0
      • N nss

        I want to check the registry to see if a certain key TestViewBox exists. If it does, then I want to delete it (so all the stuff under it vanishes too - correct? ). Then I want to recreate it, and put in fresh entries under that key (a subkey and then values). I'm thinking:

        	LONG res = RegOpenKeyEx(keyHandle1, "TestViewBox",
        	0, KEY\_ALL\_ACCESS, &keyHandle2);
                   
                    if (res)
        
                       {// means it exists, so delete it........`I need to know how` ); 
        
                    //if its now been deleted or never existed then recreate it etc:
        
                
                RegCreateKeyEx(keyHandle1, "TestViewBox", 0, "", 
                REG\_OPTION\_NON\_VOLATILE, KEY\_ALL\_ACCESS, NULL,
                &keyHandle2, &disp);
        
             
               RegCreateKeyEx(keyHandle2, "MyTestApp", 0, "",
               REG\_OPTION\_NON\_VOLATILE, KEY\_ALL\_ACCESS, NULL,
               &keyHandle3, &disp);
        
               RegSetValueEx(keyHandle3, NumberOfBoxes,
           0, REG\_DWORD, (BYTE\*)&numberOfBoxes, sizeof(DWORD));
        

        {

        Of course I will be checking the returnvalues from each registry function....:)

        S Offline
        S Offline
        Stephane Rodriguez
        wrote on last edited by
        #3

        Use ::RegDeleteKey(). On Windows W9X, this deletes this key, subkeys and values. But on W2K/NT/XP, it doesn't, you have to delete the subkeys yourself (recursive key deletion before you can delete this key). PS : be sure to release the key handles as well. Like a lot of developers, you seem to mix all together and reuse handles without freeing them first.

        N 1 Reply Last reply
        0
        • S Stephane Rodriguez

          Use ::RegDeleteKey(). On Windows W9X, this deletes this key, subkeys and values. But on W2K/NT/XP, it doesn't, you have to delete the subkeys yourself (recursive key deletion before you can delete this key). PS : be sure to release the key handles as well. Like a lot of developers, you seem to mix all together and reuse handles without freeing them first.

          N Offline
          N Offline
          nss
          wrote on last edited by
          #4

          // Open the Software key under HKEY_CURRENT_USER.
          RegOpenKeyEx(HKEY_CURRENT_USER, "Software",
          0, KEY_ALL_ACCESS, &keyHandle1);

          // Open the MyCompany key under Software.
          	LONG res = RegOpenKeyEx(keyHandle1, "MyCompany4",
          	0, KEY\_ALL\_ACCESS, &keyHandle2);
          

          if (!res)
          {
          // Open the MyApplication key under MyCompany.
          res = RegOpenKeyEx(keyHandle2, "MyApplication4",
          0, KEY_ALL_ACCESS, &keyHandle3);
          }

          SO is this good? Am terrified I might erase the "SoftWare " key or something and destroy myself!!! So I need to know if its safe first....

                 if (!res) 
                      {
          
                       res = RegDeleteKey(keyHandle2,"MyApplication4");
          
                       }
          
                   if (!res)
                       {
                        res = RegDeleteKey(keyHandle1, "MyCompany4");
          
                       }
          
          	RegCloseKey(keyHandle1);
          	RegCloseKey(keyHandle2);
          	RegCloseKey(keyHandle3);
          

          (check res ) and Open the software key again and write the new subkeys.....

          S 1 Reply Last reply
          0
          • N nss

            // Open the Software key under HKEY_CURRENT_USER.
            RegOpenKeyEx(HKEY_CURRENT_USER, "Software",
            0, KEY_ALL_ACCESS, &keyHandle1);

            // Open the MyCompany key under Software.
            	LONG res = RegOpenKeyEx(keyHandle1, "MyCompany4",
            	0, KEY\_ALL\_ACCESS, &keyHandle2);
            

            if (!res)
            {
            // Open the MyApplication key under MyCompany.
            res = RegOpenKeyEx(keyHandle2, "MyApplication4",
            0, KEY_ALL_ACCESS, &keyHandle3);
            }

            SO is this good? Am terrified I might erase the "SoftWare " key or something and destroy myself!!! So I need to know if its safe first....

                   if (!res) 
                        {
            
                         res = RegDeleteKey(keyHandle2,"MyApplication4");
            
                         }
            
                     if (!res)
                         {
                          res = RegDeleteKey(keyHandle1, "MyCompany4");
            
                         }
            
            	RegCloseKey(keyHandle1);
            	RegCloseKey(keyHandle2);
            	RegCloseKey(keyHandle3);
            

            (check res ) and Open the software key again and write the new subkeys.....

            S Offline
            S Offline
            Stephane Rodriguez
            wrote on last edited by
            #5

            I would replace this :

            if (!res)
            {
            res = RegDeleteKey(keyHandle2,"MyApplication4");
            }
            if (!res)
            {
            res = RegDeleteKey(keyHandle1, "MyCompany4");
            }
            RegCloseKey(keyHandle1);
            RegCloseKey(keyHandle2);
            RegCloseKey(keyHandle3);

            with this :

            if (!res)
            {
            RegCloseKey(keyHandle3);
            RegDeleteKey(keyHandle2,"MyApplication4");
            }

            RegCloseKey(keyHandle2);
            res = RegDeleteKey(keyHandle1, "MyCompany4");

            RegCloseKey(keyHandle1);

            Don't use the same res variable for all keys. Add braces to combine the cases, instead of putting them sequentially.

            N 1 Reply Last reply
            0
            • S Stephane Rodriguez

              I would replace this :

              if (!res)
              {
              res = RegDeleteKey(keyHandle2,"MyApplication4");
              }
              if (!res)
              {
              res = RegDeleteKey(keyHandle1, "MyCompany4");
              }
              RegCloseKey(keyHandle1);
              RegCloseKey(keyHandle2);
              RegCloseKey(keyHandle3);

              with this :

              if (!res)
              {
              RegCloseKey(keyHandle3);
              RegDeleteKey(keyHandle2,"MyApplication4");
              }

              RegCloseKey(keyHandle2);
              res = RegDeleteKey(keyHandle1, "MyCompany4");

              RegCloseKey(keyHandle1);

              Don't use the same res variable for all keys. Add braces to combine the cases, instead of putting them sequentially.

              N Offline
              N Offline
              nss
              wrote on last edited by
              #6

              Thank you so much for the pointers....

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups