?
Solved

querying and editing windows registry

Posted on 2006-04-11
8
Medium Priority
?
380 Views
Last Modified: 2010-04-01
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...
0
Comment
Question by:ncheeku14
  • 5
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16427257
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
 
LVL 86

Expert Comment

by:jkr
ID: 16427273
>>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
 

Author Comment

by:ncheeku14
ID: 16427714
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 16427741
Err, a key can have more than one value, thus you need to specify a name. It can be anything you want...
0
 
LVL 86

Expert Comment

by:jkr
ID: 16427769
BTW, if you are referring to a key's default value, use either NULL or an empty string as the value name parameter.
0
 

Author Comment

by:ncheeku14
ID: 16427821
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
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 16427891
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
 

Author Comment

by:ncheeku14
ID: 16427989
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question