/////////////////////////////////////////////////////////////// // addvalue.cpp - demonstrate adding value to registry // // ver 1.0 // // // // Jim Fawcett, CST 2-187, Syracuse Univ // // fawcett@ecs.syr.edu, (315) 443-3948 // // // // Modified from Win32 System Services, Marshall Brain, // // Prentice Hall, 1996 // /////////////////////////////////////////////////////////////// // Note: // // View result with regedit.exe (don't modify anything!) // // // /////////////////////////////////////////////////////////////// #include #include #include #include using namespace std; void main(int argc, char *argv[]) { LONG ret; HKEY keyHandle1, keyHandle2; DWORD disposition; cout << "\n Demonstrating Adding Value to Registry " << "\n ========================================\n"; // get key and value from command line if(argc == 1) { cout << "\n please enter key and value\n\n"; return; } char *key = argv[argc-2]; char *value = argv[argc-1]; cout << "\n do you want to add the value:\n \"" << value << "\"\n to the subkey \"Sample Key\" of the key:\n " << key << " ? "; char ch = _getche(); cout << "\n"; if(tolower(ch) == 'n') { return; } // add or open key ret=RegCreateKeyEx( HKEY_CURRENT_USER, key, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &keyHandle1, &disposition ); // if (ret != ERROR_SUCCESS) { cerr << "\n Unable to create key or open key" << endl; return; } else cout << "\n opened key: " << key << "\n"; // add or open subkey char *subkey = "Sample Key"; ret=RegCreateKeyEx( keyHandle1, subkey, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &keyHandle2, &disposition ); if (ret != ERROR_SUCCESS) { cerr << "Unable to create or open subkey" << endl; return; } else cout << "\n opened subkey: " << subkey << "\n"; // Add new value to subkey ret=RegSetValueEx( keyHandle2, // handle of key with value "sampleValue", // name of value to set 0, // reserved REG_SZ, // value type (CONST BYTE *) value, // location of value data strlen(value)+1 // size of value data ); if (ret != ERROR_SUCCESS) { cerr << "Unable to modify subkey: " << subkey << "\n\n"; return; } else cout << "\n modified subkey: " << subkey << "\n\n"; // close the keys ret=RegCloseKey(keyHandle1); ret=RegCloseKey(keyHandle2); }