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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How can I check the exsistence of a Registry Key?

How can I check the exsistence of a Registry Key?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++windows-admin
14 Posts 4 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.
  • J J_E_D_I

    Thanks, that was a prompt reply! How can I assign a certain value to a variable of my choice if the key does not exist?

    R Offline
    R Offline
    Rajkumar R
    wrote on last edited by
    #4

    J_E_D_I wrote:

    How can I assign a certain value to a variable of my choice if the key does not exist?

    MyChoiceVariableType MyChoiceVariable = MyChoiceInitialValue; :)
    if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE,
    "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", // u can replace with ur choice key
    0, KEY_QUERY_VALUE, &hKey ))
    {
    MyChoiceVariable = MY_CHOICE_VARIABLE_VALUE; :)
    }

    modified on Monday, February 18, 2008 9:11 AM

    J 1 Reply Last reply
    0
    • J J_E_D_I

      How can I check the exsistence of a Registry Key using C++? I want the program to perform a certain action if the key does not exist. Thanks

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #5

      And see A Registry Class[^] for more info about registry functions.

      1 Reply Last reply
      0
      • R Rajkumar R

        J_E_D_I wrote:

        How can I assign a certain value to a variable of my choice if the key does not exist?

        MyChoiceVariableType MyChoiceVariable = MyChoiceInitialValue; :)
        if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE,
        "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", // u can replace with ur choice key
        0, KEY_QUERY_VALUE, &hKey ))
        {
        MyChoiceVariable = MY_CHOICE_VARIABLE_VALUE; :)
        }

        modified on Monday, February 18, 2008 9:11 AM

        J Offline
        J Offline
        J_E_D_I
        wrote on last edited by
        #6

        I get error C2065: 'hKey' : undeclared identifier. Have I missed something? :doh: Here is my code: int MyChoiceVariable = 1; if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\ProgramX\\Key", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = 0; cout << MyChoiceVariable << endl; }

        R D 2 Replies Last reply
        0
        • J J_E_D_I

          I get error C2065: 'hKey' : undeclared identifier. Have I missed something? :doh: Here is my code: int MyChoiceVariable = 1; if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\ProgramX\\Key", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = 0; cout << MyChoiceVariable << endl; }

          R Offline
          R Offline
          Rajkumar R
          wrote on last edited by
          #7

          J_E_D_I wrote:

          error C2065: 'hKey' : undeclared identifier

          so u r now in this stage, to write a registry based program.:suss: I was already wondering in ur previous post asking how to assign a variable. :rolleyes: Any way let me complete the code. :(( declare hKey, HKEY hKey = NULL;

          modified on Monday, February 18, 2008 2:06 PM

          J 2 Replies Last reply
          0
          • J J_E_D_I

            I get error C2065: 'hKey' : undeclared identifier. Have I missed something? :doh: Here is my code: int MyChoiceVariable = 1; if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\ProgramX\\Key", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = 0; cout << MyChoiceVariable << endl; }

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #8

            J_E_D_I wrote:

            I get error C2065: 'hKey' : undeclared identifier. Have I missed something?

            Yes. You've failed to declare a variable.

            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            1 Reply Last reply
            0
            • R Rajkumar R

              J_E_D_I wrote:

              error C2065: 'hKey' : undeclared identifier

              so u r now in this stage, to write a registry based program.:suss: I was already wondering in ur previous post asking how to assign a variable. :rolleyes: Any way let me complete the code. :(( declare hKey, HKEY hKey = NULL;

              modified on Monday, February 18, 2008 2:06 PM

              J Offline
              J Offline
              J_E_D_I
              wrote on last edited by
              #9

              That's great, now it works! I am reporting below a simplified code for who's joined the post only now. //If you want to check if a Registry key exists: HKEY hKey = NULL; string MyChoiceVariable; if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\..", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = "KEY DOES NOT EXIST"; cout << MyChoiceVariable << endl; } if (ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\..", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = "KEY EXISTS"; cout << MyChoiceVariable << endl; } Now the next (and final!) question is...what if I want instead to check if a REG_DWORD value exists?

              D 1 Reply Last reply
              0
              • R Rajkumar R

                J_E_D_I wrote:

                error C2065: 'hKey' : undeclared identifier

                so u r now in this stage, to write a registry based program.:suss: I was already wondering in ur previous post asking how to assign a variable. :rolleyes: Any way let me complete the code. :(( declare hKey, HKEY hKey = NULL;

                modified on Monday, February 18, 2008 2:06 PM

                J Offline
                J Offline
                J_E_D_I
                wrote on last edited by
                #10

                That's great, now it works! I am reporting below a simplified code for who's joined the post only now. //If you want to check if a Registry key exists: HKEY hKey = NULL; string MyChoiceVariable; if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\..", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = "KEY DOES NOT EXIST"; cout << MyChoiceVariable << endl; } if (ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\..", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = "KEY EXISTS"; cout << MyChoiceVariable << endl; } Now the next (and final!) question is...what if I want instead to check if a REG_DWORD value exists?

                R 1 Reply Last reply
                0
                • J J_E_D_I

                  That's great, now it works! I am reporting below a simplified code for who's joined the post only now. //If you want to check if a Registry key exists: HKEY hKey = NULL; string MyChoiceVariable; if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\..", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = "KEY DOES NOT EXIST"; cout << MyChoiceVariable << endl; } if (ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\..", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = "KEY EXISTS"; cout << MyChoiceVariable << endl; } Now the next (and final!) question is...what if I want instead to check if a REG_DWORD value exists?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #11

                  J_E_D_I wrote:

                  ...I want instead to check if a REG_DWORD value exists?

                  Have you considered RegQueryValue()?

                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  • J J_E_D_I

                    That's great, now it works! I am reporting below a simplified code for who's joined the post only now. //If you want to check if a Registry key exists: HKEY hKey = NULL; string MyChoiceVariable; if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\..", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = "KEY DOES NOT EXIST"; cout << MyChoiceVariable << endl; } if (ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\..", 0, KEY_QUERY_VALUE, &hKey )) { MyChoiceVariable = "KEY EXISTS"; cout << MyChoiceVariable << endl; } Now the next (and final!) question is...what if I want instead to check if a REG_DWORD value exists?

                    R Offline
                    R Offline
                    Rajkumar R
                    wrote on last edited by
                    #12

                    i just want to confirn whether u are waiting for how to assign your choice variable when using RegQueryValueEx() API for checking REG_DWORD value:~

                    J 1 Reply Last reply
                    0
                    • R Rajkumar R

                      i just want to confirn whether u are waiting for how to assign your choice variable when using RegQueryValueEx() API for checking REG_DWORD value:~

                      J Offline
                      J Offline
                      J_E_D_I
                      wrote on last edited by
                      #13

                      :cool:Hi Rajkumar, many thanks indeed for your help. What a relief, after a few attempts I came to a solution (see below). If it wasn't for the help of you guys from the Code Project forum, I would probably be stuck on these painful registry functions for months.. The "help" on MSDN and VS2005 might be ok as a reference only, but is totally useless unless you are an advanced programmer!:mad: ///// This code checks if a Value belonging to the opened Registry Key exists or not unsigned long type=REG_SZ, size=1024; char res[1024]=""; string MyChoiceVariable2; HKEY key; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\..", NULL, KEY_READ, &key)==ERROR_SUCCESS) { if (ERROR_SUCCESS != RegQueryValueEx(key, "ValueName", NULL, &type, (LPBYTE)&res[0],&size)) { MyChoiceVariable2 = "VALUE DOES NOT EXIST"; cout << MyChoiceVariable2 << endl; } if (ERROR_SUCCESS == RegQueryValueEx(key, "ValueName", NULL, &type, (LPBYTE)&res[0],&size)) { MyChoiceVariable2 = "VALUE EXISTS"; cout << MyChoiceVariable2 << endl; } RegCloseKey(key); }

                      R 1 Reply Last reply
                      0
                      • J J_E_D_I

                        :cool:Hi Rajkumar, many thanks indeed for your help. What a relief, after a few attempts I came to a solution (see below). If it wasn't for the help of you guys from the Code Project forum, I would probably be stuck on these painful registry functions for months.. The "help" on MSDN and VS2005 might be ok as a reference only, but is totally useless unless you are an advanced programmer!:mad: ///// This code checks if a Value belonging to the opened Registry Key exists or not unsigned long type=REG_SZ, size=1024; char res[1024]=""; string MyChoiceVariable2; HKEY key; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\..", NULL, KEY_READ, &key)==ERROR_SUCCESS) { if (ERROR_SUCCESS != RegQueryValueEx(key, "ValueName", NULL, &type, (LPBYTE)&res[0],&size)) { MyChoiceVariable2 = "VALUE DOES NOT EXIST"; cout << MyChoiceVariable2 << endl; } if (ERROR_SUCCESS == RegQueryValueEx(key, "ValueName", NULL, &type, (LPBYTE)&res[0],&size)) { MyChoiceVariable2 = "VALUE EXISTS"; cout << MyChoiceVariable2 << endl; } RegCloseKey(key); }

                        R Offline
                        R Offline
                        Rajkumar R
                        wrote on last edited by
                        #14

                        J_E_D_I wrote:

                        if (ERROR_SUCCESS != RegQueryValueEx(key, "ValueName", NULL, &type, (LPBYTE)&res[0],&size)) { MyChoiceVariable2 = "VALUE DOES NOT EXIST"; cout << MyChoiceVariable2 << endl; } if (ERROR_SUCCESS == RegQueryValueEx(key, "ValueName", NULL, &type, (LPBYTE)&res[0],&size)) { MyChoiceVariable2 = "VALUE EXISTS"; cout << MyChoiceVariable2 << endl; }

                        have you heard about if...else.. clause in C++.

                        J_E_D_I wrote:

                        RegCloseKey(key);

                        I appreciate your attempt to use the undiscussed RegCloseKey() API which is necessary.

                        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