how to get / set the Windows Registry

Example 1: Getting a string value from the Windows Registry

static str getRegistryStringValue(int _registryRoot, str _registryPath, str _registryKey)
{
   //gets the value of a string key in the registry
   //
   //sample call:
   //MyClass::getRegistryStringValue(#HKEY_CURRENT_USER,  @’Software\My Settings\Samples’, ‘SampleKey’);
   #winapi
   int         readReg;
   container   regRead;
   str         regKeyValue;
   readReg = winapi::regOpenKey(_registryRoot, _registryPath, #KEY_READ);
   if (readReg)
   {
    regRead = WinAPI::regGetValue(readReg, _registryKey);
    regKeyValue = conPeek(regRead, 1);
    WinAPI::regCloseKey(readReg);
   }
   return regKeyValue;
}
    Example 2: Setting a string value from the Windows Registry


    static void setRegistryStringValue(int _registryRoot, str _registryPath, str _registryKey, str _registryValue)
    {
       //writes a string value to a key in the registry
       //if the key doesn’t exist, it creates it and
       //then writes the value.
       //sample call:
       //MyClass::setRegistryStringValue(#HKEY_CURRENT_USER,  @’Software\ My Settings\Samples’, ‘SampleKey’, ’2′);
       #winapi
       int         writePathReg;
       container   regWrite;
       writePathReg = winapi::regOpenKey(_registryRoot, _registryPath, #KEY_WRITE);
       if (writePathReg)   //the parent key exists and we opened it
       {
        //write the key and its value
        WinAPI::regSetValueEx(writePathReg, _registryKey, #REG_SZ, _registryValue);
        WinAPI::regCloseKey(writePathReg);
       }
       else           //the parent key doesn’t exist, we have to create it
       {
        regWrite = winapi::regCreateKeyEx(_registryRoot, _registryPath);
        if (regWrite) //now the parent key exists
        {
         //open the parent key
         writePathReg = winapi::regOpenKey(_registryRoot, _registryPath, #KEY_WRITE);
         if (writePathReg)
         {
          //write the key and its value
          WinAPI::regSetValueEx(writePathReg, _registryKey, #REG_SZ, _registryValue);
          WinAPI::regCloseKey(writePathReg);
         }
        }
       }
    }

    Comments