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

📄 bcampropertybag.cpp

📁 BCAM 1394 Driver
💻 CPP
📖 第 1 页 / 共 2 页
字号:

//------------------------------------------------------------------------------
// CPropertyBagPtr CIniFilePropertyBag::CreateBag(const CString& name)
// Author: 
// Date: 04.09.2002
//------------------------------------------------------------------------------
/**
 * Creates a new section in the .ini file and returns a property bag bound to it
 *
 * \param     name The section's name
 * \return    
 *
 * A property bag
 * 
 */
//------------------------------------------------------------------------------
CPropertyBagPtr CIniFilePropertyBag::CreateBag(const CString& name)
{
  return CPropertyBagPtr(new CIniFilePropertyBag(m_strFileName, name, true));
}

//------------------------------------------------------------------------------
// CPropertyBagPtr CIniFilePropertyBag::GetBag(const CString& name)
// Author: 
// Date: 04.09.2002
//------------------------------------------------------------------------------
/**
 * Returns a property bag bound to a given secition in the .ini file
 *
 * \param     name The section's name
 * \return    
 *
 * A property bag
 * 
 */
//------------------------------------------------------------------------------
CPropertyBagPtr CIniFilePropertyBag::GetBag(const CString& name)
{
  return CPropertyBagPtr(new CIniFilePropertyBag(m_strFileName, name, false));
}

//------------------------------------------------------------------------------
// DWORD CIniFilePropertyBag::WriteProperty(const CString& name, const CString& value)
// Author: 
// Date: 04.09.2002
//------------------------------------------------------------------------------
/**
 * Write a value to the section the bag is bound to
 *
 * \param     name  name of the value
 * \param     value value
 * \return    0 if the function succeeds, otherwise an error code
 *
 */
//------------------------------------------------------------------------------
DWORD CIniFilePropertyBag::WriteProperty(const CString& name, const CString& value)
{
  if ( ! WritePrivateProfileString(m_strSectionName, name, value, m_strFileName) )
    return GetLastError();
  return 0;
}

//------------------------------------------------------------------------------
// DWORD CIniFilePropertyBag::ReadProperty(const CString& name, CString& value)
// Author: 
// Date: 04.09.2002
//------------------------------------------------------------------------------
/**
 * Read a value from a section the bag is bound to.
 *
 * \param     name name of the property 
 * \param     value String which will receive the value
 * \return    
 *
 * 0 if the function succeeds, otherwise an error code
 * 
 * \see       <delete line if not used>
 * \todo      
 */
//------------------------------------------------------------------------------

DWORD CIniFilePropertyBag::ReadProperty(const CString& name, CString& value)
{
  TCHAR buffer[1024];
  if ( ! GetPrivateProfileString(m_strSectionName, name, _T( "" ), buffer, 1024, m_strFileName) )
    return BCAM_E_VALUE_NOT_FOUND;
  value = buffer;
  return 0;
}


//////////////////////////////////////////////////////////////////////////////////
///
///   Implementation of CIniFilePropertyBag
///
//////////////////////////////////////////////////////////////////////////////////

struct Bcam::RegKeyImpl
{
  CRegKey m_Key;

};

//------------------------------------------------------------------------------
// CRegistryPropertyBag::CRegistryPropertyBag(HKEY parent, const CString& bagName, bool create)
// Author: A.Happe
// Date: 06.09.2002
//------------------------------------------------------------------------------
/**
 * Constructor. It creates a property bag which is bound to a specified registry key
 *
 * \param     parent The parent registry key of the property bag
 * \param     bagName Name of the property bag's registry key (relative to the parent)
 * \param     create if set to true, a new bag will be created, otherwise an existing one will be returned
 */
//------------------------------------------------------------------------------
CRegistryPropertyBag::CRegistryPropertyBag(HKEY parent, const CString& bagName, bool create)
: m_pKeyImpl( new Bcam::RegKeyImpl )
{
  if (! m_pKeyImpl) throw BcamException( Bvc::ErrorNumber( E_OUTOFMEMORY ), _T( "CRegistryPropertyBag::CRegistryPropertyBag" ) );

  // check if the bag already exists;
  LONG error = m_pKeyImpl->m_Key.Open(parent, bagName);
  bool exist = error == ERROR_SUCCESS;
  if ( create && exist )
  {
    // the bag already exists
    throw BcamException(BCAM_E_BAG_ALREADY_EXISTS, _T( "CRegistryPropertyBag::CRegistryPropertyBag()" ) );
  }
  if ( ! create && ! exist )
  {
    throw BcamException(BCAM_E_BAG_DOES_NOT_EXIST, _T( "CRegistryPropertyBag::CRegistryPropertyBag()" ));
  }
  if ( create )
  {
    error = m_pKeyImpl->m_Key.Create(parent, bagName);
    if ( error != ERROR_SUCCESS )
      throw BcamException(error, _T( "CRegistryPropertyBag::CRegistryPropertyBag()" ) );
  }
}


//------------------------------------------------------------------------------
// CPropertyBagPtr CRegistryPropertyBag::Open(const CString& bagName, HKEY hRoot /* = HKEY_CURRENT_USER */)
// Author: 
// Date: 06.09.2002
//------------------------------------------------------------------------------
/**
 * Opens an existing property bag
 *
 * \param     bagName Name of the registry key the bag is bound to
 * \param     hRoot handle to the parent key
 * \return    a smart pointer to the property bag
 */
//------------------------------------------------------------------------------
CPropertyBagPtr CRegistryPropertyBag::Open(const CString& bagName, HKEY hRoot /* = HKEY_CURRENT_USER */)
{
  return CPropertyBagPtr(new CRegistryPropertyBag(hRoot, bagName, false));
}

//------------------------------------------------------------------------------
// CPropertyBagPtr CRegistryPropertyBag::Create(const CString& bagName, HKEY hRoot /* = HKEY_CURRENT_USER */)
// Author: 
// Date: 06.09.2002
//------------------------------------------------------------------------------
/**
 * Create a new property bag. If the property bag already exists, it will be deleted!
 *
 * \param     bagName Name of the registry key the bag is bound to
 * \param     hRoot handle to the parent key
 * \return    a smart pointer to the created property bag
 *
 */
//------------------------------------------------------------------------------
CPropertyBagPtr CRegistryPropertyBag::Create(const CString& bagName, HKEY hRoot /* = HKEY_CURRENT_USER */)
{
  CRegKey key;
  LONG error = key.Create(hRoot, bagName );
  if ( error == ERROR_SUCCESS )
  {
    key.Close();
    // bag already exist. Delete it
    key.Attach(hRoot);
    key.RecurseDeleteKey(bagName);
  }
  return CPropertyBagPtr(new CRegistryPropertyBag(hRoot, bagName, true));
}

//------------------------------------------------------------------------------
// CPropertyBagPtr CRegistryPropertyBag::CreateBag(const CString& Name)
// Author: 
// Date: 06.09.2002
//------------------------------------------------------------------------------
/**
 * Create a new property bag as a child of the current one. If the bag already
 * exists, an exception will be thrown
 *
 * \param     Name name of the registry key representing the bag
 * \return    a smart pointer to the created bag.
 *
 */
//------------------------------------------------------------------------------
CPropertyBagPtr CRegistryPropertyBag::CreateBag(const CString& Name)
{
  return CPropertyBagPtr(new CRegistryPropertyBag(m_pKeyImpl->m_Key, Name, true));
}


//------------------------------------------------------------------------------
// CPropertyBagPtr CRegistryPropertyBag::GetBag(const CString& Name)
// Author: 
// Date: 06.09.2002
//------------------------------------------------------------------------------
/**
 * Opens a new property bag which is a child of the current one. I the bag doesn't 
 * exist, an exception will be thrown.
 *
 * \param     Name name of the registry key representing the child bag
 * \return    a smart pointer to the bag
 *
 */
//------------------------------------------------------------------------------
CPropertyBagPtr CRegistryPropertyBag::GetBag(const CString& Name)
{
  return CPropertyBagPtr(new CRegistryPropertyBag(m_pKeyImpl->m_Key, Name, false));
}

//------------------------------------------------------------------------------
// DWORD CRegistryPropertyBag::WriteProperty(const CString& name, const CString& value)
// Author: 
// Date: 06.09.2002
//------------------------------------------------------------------------------
/**
 * Write a value to the registry key the bag is bound to
 *
 * \param     name name of the value
 * \param     value the value
 * \return    0 if the function succeeds, otherwise an error code
 *
 */
//------------------------------------------------------------------------------
DWORD CRegistryPropertyBag::WriteProperty(const CString& name, const CString& value)
{
  return m_pKeyImpl->m_Key.SetValue(value, name);
}

//------------------------------------------------------------------------------
// DWORD CRegistryPropertyBag::ReadProperty(const CString& name, CString& value)
// Author: 
// Date: 06.09.2002
//------------------------------------------------------------------------------
/**
 * Read a value from the registry key the bag is bound to
 *
 * \param     name name of the value
 * \param     value the value
 * \return    0 if the function succeeds, otherwise an error code
 *
 */
//------------------------------------------------------------------------------
DWORD CRegistryPropertyBag::ReadProperty(const CString& name, CString& value)
{
  TCHAR buffer[1024];
  ULONG size = 1024;
  LONG error = m_pKeyImpl->m_Key.QueryValue( buffer, name, &size);
  if ( error == ERROR_SUCCESS )
    value = buffer;
  if ( error == ERROR_FILE_NOT_FOUND )
    error = BCAM_E_VALUE_NOT_FOUND;
  return error;
}

⌨️ 快捷键说明

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