⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registrykey.cpp

📁 Visual C++下的界面设计
💻 CPP
📖 第 1 页 / 共 3 页
字号:
      DWORD classLen = m_Class.GetSize();

      LONG result = RegEnumKeyEx(
                     m_pKey->GetCounted(), 
                     m_index,
                     m_Name, 
                     &nameLen,
                     NULL,
                     m_Class,
                     &classLen,
                     &m_lastWriteTime);

      if (ERROR_NO_MORE_ITEMS == result)
      {
         ok = false;
      }
      else if (ERROR_MORE_DATA == result)
      {
         // Size has changed since we started 

         DWORD dwNumSubKeys = 0;
         DWORD dwMaxNameLen = 0;
         DWORD dwMaxClassLen = 0;

         result = RegQueryInfoKey(
            m_pKey->GetCounted(),
            NULL,    // Not interested in this key's class 
            NULL,    // ditto
            NULL,    // Reserved
            &dwNumSubKeys,    
            &dwMaxNameLen,    
            &dwMaxClassLen,    
            NULL,    // Not interested in number of values 
            NULL,    // Not interested in max length of value name
            NULL,    // Not interested in max length of value buffer
            NULL,    // Not interested in length of security descriptor
            NULL);   // Not interested in last write time

         if (ERROR_SUCCESS != result)
         {
            throw Exception(_T("CRegistryKey::SubkeyIterator::GetSubkeyInfo()"),
               result);
         }

         if (0 != dwNumSubKeys)
         {
            // Allow for NULL character...

            m_Name.Resize(dwMaxNameLen + 1);
            m_Class.Resize(dwMaxClassLen + 1);
         }
         else
         {
            done = true;
         }
      }
      else if (ERROR_SUCCESS != result)
      {
         throw Exception(_T("CRegistryKey::SubkeyIterator::GetSubkeyInfo()"),
            result);
      }
      else
      {
         done = true;
      }
   }
   return ok;
}
///////////////////////////////////////////////////////////////////////////////
// CRegistryKey::ValueIteratorImpl
///////////////////////////////////////////////////////////////////////////////

CRegistryKey::ValueIteratorImpl::ValueIteratorImpl(CCountedRegKey *pKey) 
   :  CRegKeyIterator(pKey),
      m_Name(0),
      m_Buffer(0),
		m_dwType(REG_NONE),
		m_dwBufUsed(0),
		m_pStringRep(0)
{
   if (m_pKey)
   {
      DWORD dwNumValues = 0;
      DWORD dwMaxValueNameLen = 0;
      DWORD dwMaxValueLen = 0;

      LONG result = RegQueryInfoKey(
         m_pKey->GetCounted(),
         NULL,    // Not interested in this key's class 
         NULL,    // ditto
         NULL,    // Reserved
         NULL,    // Not interested in number of sub keys
         NULL,    // Not interested in max sub key name length
         NULL,    // Not interested in max sub key class length
         &dwNumValues,
         &dwMaxValueNameLen,
         &dwMaxValueLen,
         NULL,    // Not interested in length of security descriptor
         NULL);   // Not interested in last write time

      if (ERROR_SUCCESS != result)
      {
         throw Exception(_T("CRegistryKey::ValueIterator::ValueIterator()"),
            result);
      }

      if (0 != dwNumValues)
      {
         // Allow for NULL character...

         m_Name.Resize(dwMaxValueNameLen + 1);
         m_Buffer.Resize(dwMaxValueLen + 1);
			m_dwBufUsed = 0;
			m_dwType = REG_NONE;
      }
   }
}

bool CRegistryKey::ValueIteratorImpl::operator==(
      const ValueIteratorImpl &rhs) const
{
   return CRegKeyIterator::operator==(rhs);
}

LPCTSTR CRegistryKey::ValueIteratorImpl::GetName() const
{
   return m_Name;
}

LPCTSTR CRegistryKey::ValueIteratorImpl::AsString() const
{
	if (!m_pStringRep)
	{
		m_pStringRep = Value::AsString(m_dwType, m_Buffer, m_dwBufUsed);
	}

	return m_pStringRep;
}

CRegistryKey::ValueIteratorImpl::operator CRegistryKey::Value() const
{
	return Value(m_Name, m_Buffer, m_dwBufUsed, m_dwType);
}

bool CRegistryKey::ValueIteratorImpl::GetItem()
{
   bool ok = true;
   bool done = false;

	// String representation cache is no longer valid

	delete[] m_pStringRep;
	m_pStringRep = 0;

   while (!done && ok)
   {

      DWORD nameLen = m_Name.GetSize();
      m_dwBufUsed = m_Buffer.GetSize();

      LONG result = RegEnumValue(
                     m_pKey->GetCounted(), 
                     m_index,
                     m_Name, 
                     &nameLen,
                     NULL,
                     &m_dwType,
                     m_Buffer,
                     &m_dwBufUsed);

      if (ERROR_NO_MORE_ITEMS == result)
      {
         ok = false;
      }
      else if (ERROR_MORE_DATA == result)
      {
         DWORD dwNumValues = 0;
         DWORD dwMaxValueNameLen = 0;
         DWORD dwMaxValueLen = 0;

         result = RegQueryInfoKey(
            m_pKey->GetCounted(),
            NULL,    // Not interested in this key's class 
            NULL,    // ditto
            NULL,    // Reserved
            NULL,    // Not interested in number of sub keys
            NULL,    // Not interested in max sub key name length
            NULL,    // Not interested in max sub key class length
            &dwNumValues,
            &dwMaxValueNameLen,
            &dwMaxValueLen,
            NULL,    // Not interested in length of security descriptor
            NULL);   // Not interested in last write time

         if (ERROR_SUCCESS != result)
         {
            throw Exception(_T("CRegistryKey::ValueIterator::GetValueInfo()"),
               result);
         }

         if (0 != dwNumValues)
         {
            // Allow for NULL character...

            m_Name.Resize(dwMaxValueNameLen + 1);
            m_Buffer.Resize(dwMaxValueLen + 1);
         }
         else
         {
            done = true;
         }
      }
      else if (ERROR_SUCCESS != result)
      {
         throw Exception(_T("CRegistryKey::ValueIterator::GetValueInfo()"),
            result);
      }
      else
      {
         done = true;
      }
   }
   return ok;
}

///////////////////////////////////////////////////////////////////////////////
// CRegistryKey::Exception
///////////////////////////////////////////////////////////////////////////////

CRegistryKey::Exception::Exception(
   const LPCTSTR pWhere, 
   LONG error)
   :  CWin32Exception(pWhere, (DWORD)error)
{
}

///////////////////////////////////////////////////////////////////////////////
// CRegistryKey::Value
///////////////////////////////////////////////////////////////////////////////

CRegistryKey::Value::Value(
   LPCTSTR pName, 
   const LPBYTE pBuffer, 
   DWORD bufSize, 
   DWORD dwType /*= REG_BINARY*/)
   :  m_pName((LPTSTR)_tcsdup(pName)),
      m_dwType(dwType), 
      m_pBuffer((LPBYTE)duplicateBuffer(pBuffer, bufSize)), 
      m_bufSize(bufSize),
      m_pStringRep(0)
{
}

CRegistryKey::Value::Value(
   LPCTSTR pName, 
   LPCTSTR pString, 
   DWORD dwType /*= REG_SZ */)
   :  m_pName((LPTSTR)_tcsdup(pName)),
      m_dwType(dwType),
      m_pBuffer((LPBYTE)_tcsdup(pString)),
      m_bufSize(_tcslen(pString)),
      m_pStringRep(0)
{
   if (m_dwType != REG_EXPAND_SZ && 
       m_dwType != REG_SZ)
   {
      throw Exception();
   }
}

CRegistryKey::Value::Value(
   LPCTSTR pName, 
   DWORD dwValue, 
   DWORD dwType /*= REG_DWORD*/)
   :  m_pName((LPTSTR)_tcsdup(pName)),
      m_dwType(dwType),
      m_pBuffer(new unsigned char[sizeof(DWORD)]),
      m_bufSize(sizeof(DWORD)),
      m_pStringRep(0)
{
   if (dwType != REG_DWORD &&
       dwType != REG_DWORD_LITTLE_ENDIAN && 
       dwType != REG_DWORD_BIG_ENDIAN)
   {
      throw Exception();
   }

   memcpy(m_pBuffer, &dwValue, sizeof(DWORD));
}

CRegistryKey::Value::Value(const Value &rhs)
   :  m_pName(_tcsdup(rhs.m_pName)), 
      m_dwType(rhs.m_dwType),
      m_pBuffer(rhs.duplicateBuffer()),
      m_bufSize(rhs.m_bufSize),
      m_pStringRep(0)
{
   memcpy(m_pBuffer, rhs.m_pBuffer, m_bufSize);
}

CRegistryKey::Value::~Value()
{
   delete[] m_pName;
   delete[] m_pBuffer;
   delete[] m_pStringRep;
}

CRegistryKey::Value &CRegistryKey::Value::operator=(const Value &rhs)
{
   if (this != &rhs)
   {
      LPBYTE pNewBuffer = rhs.duplicateBuffer();
      LPBYTE pOldBuffer = m_pBuffer;
	  
      LPTSTR pNewName = _tcsdup(rhs.m_pName);
      LPTSTR pOldName = m_pName;

      m_dwType = rhs.m_dwType;
      m_bufSize = rhs.m_bufSize;

      m_pBuffer = pNewBuffer;
      m_pName = pNewName;

      delete[] pOldBuffer;
      delete[] pOldName;

      delete[] m_pStringRep;
      m_pStringRep = 0;
   }

   return *this;
}

CRegistryKey::Value::operator LPBYTE() const
{
   if (m_dwType != REG_BINARY && 
       m_dwType != REG_NONE)
   {
      throw Exception();
   }

   return m_pBuffer;
}

CRegistryKey::Value::operator LPCTSTR() const
{
   if (m_dwType != REG_EXPAND_SZ && 
       m_dwType != REG_SZ)
   {
      throw Exception();
   }

   return (LPCTSTR)m_pBuffer;
}

CRegistryKey::Value::operator DWORD() const
{
   if (m_dwType != REG_DWORD &&
       m_dwType != REG_DWORD_LITTLE_ENDIAN && 
       m_dwType != REG_DWORD_BIG_ENDIAN)
   {
      throw Exception();
   }

   return *(DWORD*)m_pBuffer;
}

LPCTSTR CRegistryKey::Value::Name() const
{
	return m_pName;
}

LPCTSTR CRegistryKey::Value::AsString() const
{
	if (!m_pStringRep)
	{
		m_pStringRep = AsString(m_dwType, m_pBuffer, m_bufSize);
	}

	return m_pStringRep;
}

LPTSTR CRegistryKey::Value::AsString(
	DWORD dwType, 
	const LPBYTE pBuffer, 
	DWORD bufSize)
{
	LPTSTR pStringRep;

   // TODO Convert all reps to a string rep

   if (dwType == REG_EXPAND_SZ || 
       dwType == REG_SZ)
   {
      // Already a string!

      pStringRep = _tcsdup((LPTSTR)pBuffer);
   }
	else if (dwType == REG_MULTI_SZ)
	{
		// multiple strings...

		// Should strip the \0's out...
		pStringRep = _tcsdup((LPTSTR)pBuffer);
	}
	else if (dwType == REG_BINARY || 
			   dwType == REG_NONE)
   {
		pStringRep = bytesAsString(pBuffer, bufSize);
   }
   else if (dwType == REG_DWORD ||
            dwType == REG_DWORD_LITTLE_ENDIAN || 
            dwType == REG_DWORD_BIG_ENDIAN)
   {
      pStringRep = _tcsdup(_T("A number"));
   }
	else
	{
		// Anything else...
      pStringRep = bytesAsString(pBuffer, bufSize);
	}

   return pStringRep;
}

LPTSTR CRegistryKey::Value::bytesAsString(
	const LPBYTE pBuffer, 
	DWORD bufSize)
{
	LPTSTR pStringRep = new TCHAR[(bufSize * 2) + 1];
	
	LPTSTR pHere = pStringRep;
	LPBYTE pBuf = pBuffer;

	for (DWORD i = 0; i < bufSize; i++)
	{
		_stprintf(pHere, _T("%2.2x"), *pBuf);

		pHere += 2;
		pBuf++;
	}

	return pStringRep;
}

LPBYTE CRegistryKey::Value::duplicateBuffer() const
{
	return duplicateBuffer(m_pBuffer, m_bufSize);
}

LPBYTE CRegistryKey::Value::duplicateBuffer(LPBYTE pBuffer, DWORD bufSize) const
{
	LPBYTE pNewBuffer = new unsigned char[bufSize];
	memcpy(pNewBuffer, pBuffer, bufSize);
	
	return pNewBuffer;
}


CRegistryKey::Value::Exception::Exception()
   :  CRegistryKey::Exception(_T("CRegistryKey::Value::Exception"),
         ERROR_INVALID_PARAMETER)
{
}

///////////////////////////////////////////////////////////////////////////////
// Namespace: JetByteTools
///////////////////////////////////////////////////////////////////////////////

} // End of namespace JetByteTools 

///////////////////////////////////////////////////////////////////////////////
// End of file...
///////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -