Registry
-
Hi All, Can anyone please explain me on how to write a variable(integer type) into the registry and also how to read it back Thanks Uday
-
Hi All, Can anyone please explain me on how to write a variable(integer type) into the registry and also how to read it back Thanks Uday
Check the API function with names starting with Reg prefix, like RegSetValueEx. If you're using MFC, you can also use CWinApp::WriteProfileXXX methods. Tomasz Sowinski -- http://www.shooltz.com
"Yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation.
-
Hi All, Can anyone please explain me on how to write a variable(integer type) into the registry and also how to read it back Thanks Uday
Following code isn't perfect but it works and should get you started. If you look at the documentation for RegSetValueEx() you can set the dwType param to REG_DWORD for numbers but I usually just deal with strings and use atoi() or similar to convert. bool bGetRegistryEntry(const HKEY hKey, const char* name, unsigned char* const dest) { bool bReturnVal = true; unsigned long lType = 0; unsigned long ulSize = MAX_PATH; if (ERROR_SUCCESS != RegQueryValueEx(hKey, name, NULL, &lType, dest, &ulSize)) { bReturnVal = false; } return (bReturnVal); } bool bSetRegistryEntry(const HKEY hKey, const char* name, unsigned char const* dest) { bool bReturnVal = true; unsigned long lType = 0; LONG lRESULT = 0; lRESULT = RegSetValueEx(hKey, name, 0, REG_SZ, dest, _mbslen(dest) + 1); //ERROR_ACCESS_DENIED indicates it already exists if ((lRESULT != ERROR_SUCCESS) && (lRESULT != ERROR_ACCESS_DENIED)) { bReturnVal = false; } return (bReturnVal); } // Open or create registry. void vRegOpenKey() { HKEY hKey=0; //char *my_App = "Registry_Example"; CString sAppId = "SOFTWARE\\RegKeyExample"; bool bReturnVal = false; DWORD dwDisposition = 0; if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, sAppId, 0, KEY_READ, &hKey)) { LONG lRESULT = 0; lRESULT = RegCreateKeyEx(HKEY_LOCAL_MACHINE, sAppId, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition); if (lRESULT != ERROR_SUCCESS) { MessageBox(NULL, "Registry not available!", NULL, NULL); return; } } const unsigned char name[5] = "this"; if (!bSetRegistryEntry(hKey, "value1", &name[0])) { MessageBox(NULL, "Unable to set registry value1", NULL, NULL); } else { unsigned char name2[5]; if (bGetRegistryEntry(hKey, "value1", name2)) { CString sText; sText.Format("Registry value1 is %s", name2); MessageBox(NULL, sText, NULL, NULL); } else { MessageBox(NULL, "Unable to get registry value1", NULL, NULL); } } RegCloseKey( hKey ); }