How can I check the exsistence of a Registry Key?
-
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
RegOpenKeyEx() API returns ERROR_SUCCESS if reg key exist
-
RegOpenKeyEx() API returns ERROR_SUCCESS if reg key exist
-
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?
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
-
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
And see A Registry Class[^] for more info about registry functions.
-
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
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; }
-
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; }
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
-
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; }
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
-
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
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?
-
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
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?
-
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?
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
-
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?
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:~
-
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:~
: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); }
-
: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); }
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.