Could someone please explain to me how to use these functions? (Examples).
RegCreateKey( );
RegLoadKey( );
RegDeleteKey( );
Thanks in advance.
Could someone please explain to me how to use these functions? (Examples).
RegCreateKey( );
RegLoadKey( );
RegDeleteKey( );
Thanks in advance.
My suggestion is that if you need to ask, then you need to start with a really good backup of your system.
Because one code mistake on your part is going to hose your system.
Most people start with searching say msdn.microsoft.com if they don't have MSDN already installed.
For sure, I'm sure a google search will show plenty of examples of these functions being used.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I allready tried a google search.
In a nother wards, it is a lot safer to use an ini file than the registry?Originally Posted by Salem
Sample registry writer/deleter:
Code:#include <windows.h> #include <stdio.h> int AddRegistryStringValue(HKEY basekey, LPSTR subkey, LPSTR name, LPSTR value) { HKEY key; DWORD dis; int ret; /*The disposition DWORD can be either REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY. The former means it didn't exist and was created, the latter means it did exist and was opened*/ if(RegCreateKeyEx(basekey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &key, &dis) == ERROR_SUCCESS) { if(RegSetValueEx(key, name, 0, REG_EXPAND_SZ, //REG_EXPAND_SZ specifies it is a string type to store such things as paths (LPBYTE)value, (DWORD)lstrlen(value)+1) == ERROR_SUCCESS) ret = 0; else ret = 2; RegCloseKey(key); } else ret = 1; return ret; } int DeleteRegistryStringValue(HKEY basekey, LPSTR subkey, LPSTR name) { HKEY key; if(RegOpenKeyEx(basekey, subkey, 0, KEY_WRITE, &key) == ERROR_SUCCESS) { RegDeleteValue(key, name); RegCloseKey(key); return 0; } else return 1; } int main(int argc, char *argv[]) { if(!AddRegistryStringValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "Nothing", argv[0])) printf("Write successful\n"); else printf("Write failure\n"); if(!DeleteRegistryStringValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "Nothing")) printf("Delete Successful"); else printf("Delete failure"); return 0; }
You'll probably want to read up on RegQueryValueEx. A quick and dirty example with a REG_SZ follows.
Code:#include <cstdlib> #include <iostream> #include <windows.h> using namespace std; int main(int argc, char *argv[]) { HKEY myHKEY1 = NULL, myHKEY2 = NULL; DWORD dwType, dwDisposition; BYTE buffer[MAX_PATH]; DWORD bufferSize = sizeof(buffer); // just hard coded this so RegOpenKeyEx call isn't so long char szKeyName[ ] = "Software\\Microsoft\\Internet Explorer\\"; // Open an existing key if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_QUERY_VALUE, &myHKEY1)== ERROR_SUCCESS) //MessageBox(NULL, "Open", "Title", MB_OK); // query a know value in existing key if(RegQueryValueEx(myHKEY1, "Build", NULL, &dwType, buffer, &bufferSize)==ERROR_SUCCESS) MessageBox(NULL, (char*)buffer, "The Build # is:", MB_OK); // create a new key if(RegCreateKeyEx(HKEY_CURRENT_USER, "Temp", 0, NULL, 0, DELETE | KEY_SET_VALUE, NULL, &myHKEY2, &dwDisposition)== ERROR_SUCCESS) //MessageBox(NULL, "Temp", "Title", MB_OK); // set value in new key from previously query key if(RegSetValueEx(myHKEY2, "Build", 0, dwType, buffer, bufferSize)==ERROR_SUCCESS) //MessageBox(NULL, "Build", "Title", MB_OK); // Clean up, remove testing key if(RegDeleteKey(HKEY_CURRENT_USER, "Temp")==ERROR_SUCCESS) ;//MessageBox(NULL, "Delete", "Title", MB_OK); if(myHKEY1) RegCloseKey(myHKEY1); if(myHKEY2) RegCloseKey(myHKEY2); cout << "Press `Enter' to continue . . . "; cin.sync(); cin.ignore(); return(EXIT_SUCCESS); }