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...
ncheeku14Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
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);
0
jkrCommented:
>>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...
0
ncheeku14Author Commented:
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

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jkrCommented:
Err, a key can have more than one value, thus you need to specify a name. It can be anything you want...
0
jkrCommented:
BTW, if you are referring to a key's default value, use either NULL or an empty string as the value name parameter.
0
ncheeku14Author Commented:
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);

0
jkrCommented:
You'll need to differentiate between a key and a value. The code in my 1st comment creates a value named 'Test', the code in the 2nd one a key named test. Which one meets your requirements?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ncheeku14Author Commented:
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..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.