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\\MyAp p
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...
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\\MyAp
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...
>>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\\Te st", 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,"Valu eName",NUL L,REG_SZ,( LPBYTE)acV alue,&dwSi ze);
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,"ValueN ame",NULL, REG_SZ,(LP BYTE)acVal ue,strlen( acValue));
if (lRes != ERROR_SUCCESS) {
cout << "Setting value failed: " << lRes << endl;
}
RegCloseKey(hKey);
then...
Sorry, missed this part - make that
HKEY hKey;
char acValue[MAX_PATH];
DWORD dwSize = MAX_PATH;
DWORD dwDisp;
LONG lRes = RegCreateKeyEx(HKEY_LOCAL_
KEY_ALL_ACCESS, &sa, &hKey, &dwDisp);
if (lRes != ERROR_SUCCESS) {
cout << "Opening Key failed: " << lRes << endl;
return;
}
lRes = RegQueryValueEx(hKey,"Valu
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,"ValueN
if (lRes != ERROR_SUCCESS) {
cout << "Setting value failed: " << lRes << endl;
}
RegCloseKey(hKey);
then...
ASKER
hi jkr, this did the job. btw "SOFTWARE\\Apps\\MyApp\\Te st" 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\\Te st", 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,"Valu eName",NUL L,REG_SZ,( LPBYTE)acV alue,&dwSi ze); //What should be the ValueName parameter here.....
What should be the valueName parameter here in case I have opened it using RegCreateKeyEx
LONG lRes = RegCreateKeyEx(HKEY_LOCAL_
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,"Valu
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.
ASKER
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\\Te st", 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,(LPBYT E)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,"ValueN ame",NULL, REG_SZ,(LP BYTE)acVal ue,strlen( acValue));
if (lRes != ERROR_SUCCESS) {
cout << "Setting value failed: " << lRes << endl;
}
RegCloseKey(hKey);
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_
KEY_ALL_ACCESS, &sa, &hKey, &dwDisp);
if (lRes != ERROR_SUCCESS) {
cout << "Opening Key failed: " << lRes << endl;
return;
}
lRes = RegQueryValueEx(hKey,"Test
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,"ValueN
if (lRes != ERROR_SUCCESS) {
cout << "Setting value failed: " << lRes << endl;
}
RegCloseKey(hKey);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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..
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..
HKEY hKey;
char acValue[MAX_PATH];
DWORD dwSize = MAX_PATH;
DWORD dwDisp;
LONG lRes = RegCreateKeyEx(HKEY_LOCAL_
KEY_ALL_ACCESS, &sa, &hKey, &dwDisp);
if (lRes != ERROR_SUCCESS) {
cout << "Opening Key failed: " << lRes << endl;
return;
}
lRes = RegQueryValueEx(hKey,"Test
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",
if (lRes != ERROR_SUCCESS) {
cout << "Setting value failed: " << lRes << endl;
}
RegCloseKey(hKey);