Read commonly used functions in the registry
RegOpenKeyEx is used to open a key
RegSetValueEx is used to set the key value of a key
RegQueryValueEx is used to read the value of a key
RegCloseKey The key used to close RegOpen to open
RegQueryValueEx usually uses twice in a row to read the key value, the first time it only reads the key size, then allocates memory, and then uses RegQueryValueEx to read the key value to the buffer.
The following is an example of reading the path key value under Windows
HKEY hk; int result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SYSTEM\\ControlSet001\\Control\\Session Manager\\Environment"), NULL, KEY_READ, &hk ); if(result != ERROR_SUCCESS) { if(result == ERROR_FILE_NOT_FOUND) { MessageBox(,_T("no such key in RegOpenKeyEx"),_T("title"),MB_ICONWARNING); } else return; } DWORD lsize; result = RegQueryValueEx(hk, _T("Path"),NULL,NULL, NULL, &lsize); if(result != ERROR_SUCCESS) { if(result == ERROR_FILE_NOT_FOUND) { MessageBox(,_T("no such key in RegQueryValue"),_T("title"),MB_ICONWARNING); } else if(result == ERROR_MORE_DATA) { MessageBox(,_T("need more buffer"),_T("title"),MB_ICONWARNING); } else return; } TCHAR *value = (TCHAR * )malloc(lsize * sizeof(TCHAR)); result = RegQueryValueEx(hk, _T("Path"),NULL,NULL, (LPBYTE)value, &lsize); if(result != ERROR_SUCCESS) { if(result == ERROR_FILE_NOT_FOUND) { MessageBox(,_T("no such key in RegQueryValue"),_T("title"),MB_ICONWARNING); } else if(result == ERROR_MORE_DATA) { MessageBox(,_T("need more buffer"),_T("title"),MB_ICONWARNING); } else return; } SetWindowText(,value); free(value); RegCloseKey(hk);
Frequently Asked Questions for Newbie
RegSetValueEx failed to run, return code is 5,
Solution: This situation is due to insufficient access to the registry, which means denying access. Therefore, please pay attention to see if the permission is insufficient. If changed to
KEY_ALL_ACCESS is fine, so read and write permissions are OK.