Link to home
Start Free TrialLog in
Avatar of ncheeku14
ncheeku14

asked on

querying and editing windows registry

hi, i need to do the following....
1) Open a registry key
2) if the registry key doesnot exist then create it
3) query a value from the key
4) do a comparison of this registry value with a string
5) and in the end set the registry value to a given string value

say i have HKLM\\Software\\Apps\\MyApp
MyApp has a subkey of Test (and if not i want to create it)

query the value from there and set it. Any working example is much appreciated...

Thanks...
Avatar of jkr
jkr
Flag of Germany image

You'd do that like

   HKEY hKey;
   char acValue[MAX_PATH];
   DWORD dwSize = MAX_PATH;

   DWORD dwDisp;
   LONG lRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Apps\\MyApp", 0, NULL, REG_OPTION_NON_VOLATILE,
                                               KEY_ALL_ACCESS, &sa, &hKey, &dwDisp);
   if (lRes != ERROR_SUCCESS) {

       cout << "Opening Key failed: " << lRes << endl;

       return;
   }

   lRes = RegQueryValueEx(hKey,"Test",NULL,REG_SZ,(LPBYTE)acValue,&dwSize);

   if (lRes != ERROR_SUCCESS) {

       cout << "Value not present, creating new one"<< endl;

   } else {

       int nCmp = strcmp(acValue,"Something");
       cout << "Comparison result:" << nCmp << endl;
   }

   strcpy(acValue,"NewValue");

   lRes = RegSetValueEx(hKey,"Test",NULL,REG_SZ,(LPBYTE)acValue,strlen(acValue));

   if (lRes != ERROR_SUCCESS) {

       cout << "Setting value failed: " << lRes << endl;

   }

   RegCloseKey(hKey);
>>MyApp has a subkey of Test (and if not i want to create it)

Sorry, missed this part - make that

   HKEY hKey;
   char acValue[MAX_PATH];
   DWORD dwSize = MAX_PATH;

   DWORD dwDisp;
   LONG lRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Apps\\MyApp\\Test", 0, NULL, REG_OPTION_NON_VOLATILE,
                                               KEY_ALL_ACCESS, &sa, &hKey, &dwDisp);
   if (lRes != ERROR_SUCCESS) {

       cout << "Opening Key failed: " << lRes << endl;

       return;
   }

   lRes = RegQueryValueEx(hKey,"ValueName",NULL,REG_SZ,(LPBYTE)acValue,&dwSize);

   if (lRes != ERROR_SUCCESS) {

       cout << "Value not present, creating new one"<< endl;

   } else {

       int nCmp = strcmp(acValue,"Something");
       cout << "Comparison result:" << nCmp << endl;
   }

   strcpy(acValue,"NewValue");

   lRes = RegSetValueEx(hKey,"ValueName",NULL,REG_SZ,(LPBYTE)acValue,strlen(acValue));

   if (lRes != ERROR_SUCCESS) {

       cout << "Setting value failed: " << lRes << endl;

   }

   RegCloseKey(hKey);

then...
Avatar of ncheeku14
ncheeku14

ASKER

hi jkr, this did the job. btw  "SOFTWARE\\Apps\\MyApp\\Test" is the key i want to create (if it does not exist) and then query its value...so if i have

 LONG lRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Apps\\MyApp\\Test", 0, NULL, REG_OPTION_NON_VOLATILE,
                                               KEY_ALL_ACCESS, &sa, &hKey, &dwDisp);  //Key Created here if it did not exist earlier
   if (lRes != ERROR_SUCCESS) {

       cout << "Opening Key failed: " << lRes << endl;

       return;
   }

   lRes = RegQueryValueEx(hKey,"ValueName",NULL,REG_SZ,(LPBYTE)acValue,&dwSize); //What should be the ValueName parameter here.....

What should be the valueName parameter here in case I have opened it using RegCreateKeyEx

Err, a key can have more than one value, thus you need to specify a name. It can be anything you want...
BTW, if you are referring to a key's default value, use either NULL or an empty string as the value name parameter.
so in this case if i simply put "Test" there, the result would be the same...

i mean...

"SOFTWARE\\Apps\\MyApp" is my application
"Test" is a field in that (i think its called the subkey)

so Test is the name of the field and has a value say "Hi", can i still do the above...
i.e. i want to retrieve this Test field value (or create in case its not there) and then set its value to "Hello"...so..pls let me know if the code below is correct..

  HKEY hKey;
   char acValue[MAX_PATH];
   DWORD dwSize = MAX_PATH;

   DWORD dwDisp;
   LONG lRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Apps\\MyApp\\Test", 0, NULL, REG_OPTION_NON_VOLATILE,
                                               KEY_ALL_ACCESS, &sa, &hKey, &dwDisp);
   if (lRes != ERROR_SUCCESS) {

       cout << "Opening Key failed: " << lRes << endl;

       return;
   }

   lRes = RegQueryValueEx(hKey,"Test",NULL,REG_SZ,(LPBYTE)acValue,&dwSize);

   if (lRes != ERROR_SUCCESS) {

       cout << "Value not present, creating new one"<< endl;

   } else {

       int nCmp = strcmp(acValue,"hi");
       cout << "Comparison result:" << nCmp << endl;
   }

   strcpy(acValue,"Hello");

   lRes = RegSetValueEx(hKey,"ValueName",NULL,REG_SZ,(LPBYTE)acValue,strlen(acValue));

   if (lRes != ERROR_SUCCESS) {

       cout << "Setting value failed: " << lRes << endl;

   }

   RegCloseKey(hKey);

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually i have
Test  Hi
as a Name - Value pair. So test would be a subkey right....me not too sure of this...MyApp is the application and Test is the name of a field and Hi is its value..
Thanks..