HRESULT CTCPropBagOnRegKey::_ReadVariantFromValue(CRegKey& key,
  const _bstr_t& strPropName, DWORD nType, int cbData, CComVariant& v,
  IErrorLog* pErrorLog)
{
  // Read the value from the registry based on the value type
  switch (nType)
  {
    case REG_DWORD:
    {
      key.GetDWord(strPropName, (DWORD&)V_UI4(&v));
      v.vt = VT_UI4;
      break;
    }

    case REG_SZ:
    {
      _bstr_t strValue;
      key.GetString(strPropName, strValue);
      v = strValue;
      break;
    }

    case REG_BINARY:
    {
      BYTE* pData = new BYTE[cbData];
      key.GetBinary(strPropName, pData, cbData);
      CMemFile file(pData, cbData);
      CArchive ar(&file, CArchive::load, 0);
      ar >> v;
      ar.Close();
      delete [] file.Detach();
      break;
    }

    default:
    {
      // Use local resources
      MCLibRes res;

      // Format a description string
      _bstr_t strDesc;
      strDesc.Format(IDS_FMT_UNSUPPORTED_REGTYPE, nType);

      // Log the error
      USES_CONVERSION;
      return LogError("ReadVariantFromValue", strDesc, E_FAIL,
        T2COLE(strPropName), pErrorLog);
    }
  }

  // Indicate success
  return S_OK;
}
VARTYPE CTCPropBagOnRegKey::_GetSubkeyVarType(CRegKey& key,
  const _bstr_t& strPropName)
{
  ASSERT(key.Exists(strPropName));

  // Open the subkey with the specified name
  CRegKey subkey;
  if (!subkey.Open(key, strPropName))
    return VT_EMPTY;

  // Read the subkey's variant type value
  DWORD dwVT;
  if (!subkey.GetDWord(m_szVariantType, dwVT))
    return VT_EMPTY;

  // Return the subkey's variant type value
  return VARTYPE(dwVT);
}
HRESULT CTCPropBagOnRegKey::_ReadSafeArray(CRegKey& key,
  const _bstr_t& strPropName, VARIANT* pVar, IErrorLog* pErrorLog)
{
  // Open the subkey with the specified name
  CRegKey subkey;
  VERIFY(subkey.Open(key, strPropName));

  // Read the variant type of the registry value
  VARTYPE vt = GetSubkeyVarType(key, strPropName);
  ASSERT(vt & VT_ARRAY);

  // Remove the VT_ARRAY bit flag from the variant type
  vt &= ~VT_ARRAY;

  // Check for supported variant types
  switch (vt)
  {
    case VT_BOOL:
    case VT_I1:
    case VT_I2:
    case VT_I4:
    case VT_UI1:
    case VT_UI2:
    case VT_UI4:
    case VT_ERROR:
    case VT_R4:
    case VT_R8:
    case VT_DECIMAL:
    case VT_CY:
    case VT_DATE:
    case VT_BSTR:
    case VT_UNKNOWN:
    case VT_DISPATCH:
    case VT_VARIANT:
      break;
    default:
    {
      // Use local resources
      MCLibRes res;

      // Format a description string
      _bstr_t strDesc;
      strDesc.Format(IDS_FMT_UNSUP_ARRAY_VARTYPE, strPropName, vt, vt);

      // Log the error
      USES_CONVERSION;
      return LogError("ReadSafeArray", strDesc, E_UNEXPECTED,
        T2COLE(strPropName), pErrorLog);
    }
  }

  // Read the element count of the safe array
  DWORD dwElements = 0;
  if (!subkey.GetDWord(m_szElementCount, dwElements))
  {
    // Use local resources
    MCLibRes res;

    // Format a description string
    _bstr_t strDesc;
    strDesc.Format(IDS_FMT_VALUE_NOT_EXIST, m_szElementCount, strPropName);

    // Log the error
    USES_CONVERSION;
    return LogError("ReadSafeArray", strDesc, E_UNEXPECTED,
      T2COLE(strPropName), pErrorLog);
  }

  // Read the lower bound of the safe array, defaults to zero
  LONG lLBound = 0;
  subkey.GetDWord(m_szLowerBound, (DWORD&)lLBound);

  // Read each array element into a temporary array
  _bstr_t strText;
  CComVariant var;
  CArray<CComVariant, CComVariant&> arrayTemp;
  for (DWORD i = 0; i < dwElements; i++)
  {
    // Prepare the variant
    V_VT(&var) = vt;

    // Format the value name
    strText.Format(m_szElementFmt, i);

    // Read the variant
    HRESULT hr = ReadVariant(subkey, strText, var, pErrorLog);
    if (FAILED(hr))
      return hr;

    // Add the variant to the temporary array
    arrayTemp.Add(var);
  }

  // Create a safe array and copy the temporary array elements
  SAFEARRAY* psa = SafeArrayCreateVector(vt, lLBound, dwElements);
  if (NULL == psa)
  {
    // Use local resources
    MCLibRes res;

    // Format a description string
    _bstr_t strDesc(LPCSTR(IDS_FAIL_SAFEARRAY_CREATE));

    // Log the error
    USES_CONVERSION;
    return LogError("ReadSafeArray", strDesc, E_OUTOFMEMORY,
      T2COLE(strPropName), pErrorLog);
  }

  // Copy the temporary array elements into the safe array
  for (long iElement = 0; iElement < arrayTemp.GetSize(); iElement++)
  {
    CComVariant& v = arrayTemp[iElement];
    void* pvData;
    switch (vt)
    {
      case VT_VARIANT:
        pvData = &v;
        break;
      case VT_UNKNOWN:
      case VT_DISPATCH:
      case VT_BSTR:
        pvData = V_BSTR(&v);
        break;
      default:
        pvData = &V_NONE(&v);
    }

    HRESULT hr = SafeArrayPutElement(psa, &iElement, pvData);
    if (FAILED(hr))
    {
      // Use local resources
      MCLibRes res;

      // Format a description string
      _bstr_t strDesc(LPCSTR(IDS_FAIL_SAFEARRAY_PUT));

      // Log the error
      USES_CONVERSION;
      return LogError("ReadSafeArray", strDesc, hr, T2COLE(strPropName),
        pErrorLog);
    }
  }

  // Put the safe array into the specified variant
  V_VT(pVar) = vt | VT_ARRAY;
  V_ARRAY(pVar) = psa;

  // Indicate success
  return S_OK;
}