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

📄 registry.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            RegClose (hSubKey);
          return true;
        }
      if (hSubKey != hKey)
        RegClose (hSubKey);
    }
  *pdwSubKeyCount = *pdwValueCount = 0;
  return false;
}

static LPTSTR g_pszValue = NULL;
static DWORD g_dwValueCnt, g_dwValue, g_dwValueMax;

bool RegFindFirstValue (HKEY hKey, LPCTSTR *ppszValue, RegVal *pValData);

/* walks to the first value */
bool
RegFindFirstValue (HKEY hKey, LPCTSTR *ppszValue, RegVal *pValData)
{
  if (RegQueryInfoKey (hKey, NULL, NULL, 0, NULL, NULL, NULL,
    &g_dwValueCnt, &g_dwValueMax, NULL, NULL, NULL) == ERROR_SUCCESS)
    {
      if (g_dwValueCnt)
        {
          if (g_pszValue)
            {
              free (g_pszValue);
              g_pszValue = NULL;
            }
          g_pszValue = (LPTSTR) malloc (g_dwValueMax += 1);
          if (g_pszValue)
            {
              DWORD dwMaxValue = g_dwValueMax;
              g_dwValue = 0;
              if (RegEnumValue (hKey, g_dwValue++, g_pszValue, &dwMaxValue, 0, NULL, NULL, NULL) == ERROR_SUCCESS)
                {
                  *ppszValue = g_pszValue;
                  return RegLoadVal (hKey, NULL, g_pszValue, pValData);
                }
            }
        }
    }
  return false;
}

/* walks to the next value */
bool RegFindNextValue (HKEY hKey, LPCTSTR *ppszValue, RegVal *pValData)
{
  DWORD dwMaxValue = g_dwValueMax;
  if (g_dwValue < g_dwValueCnt && RegEnumValue (hKey, g_dwValue++, g_pszValue, &dwMaxValue, 0, NULL, NULL, NULL) == ERROR_SUCCESS)
    {
      *ppszValue = g_pszValue;
      return RegLoadVal (hKey, NULL, g_pszValue, pValData);
    }
  return false;
}

/* closes registry walking */
void RegFindClose ()
{
  if (g_pszValue)
    {
      free (g_pszValue);
      g_pszValue = NULL;
    }
}

/*////////////////////////////////////////////////////////////////////////////*/

#ifdef __cplusplus

/* constructor - automatically initializes registry value data */
CRegVal::CRegVal ()
{
  Init ();
}

/* destructor - automatically frees registry value data */
CRegVal::~CRegVal ()
{
  Free ();
}

/* initializes registry value data */
void CRegVal::Init ()
{
  RegValInit (this);
}

/* frees registry value data */
void CRegVal::Free ()
{
  RegValFree (this);
}

/* get a number */
bool CRegVal::GetNumber (DWORD *pdwNumber) const
{
  return RegValGetNumber (this, pdwNumber);
}

/* get binary data */
bool CRegVal::GetBinary (LPBYTE pbyteData, DWORD dwSize) const
{
  return RegValGetBinary (this, pbyteData, dwSize);
}

/* get new binary data */
bool CRegVal::GetNewBinary (LPBYTE *pbyteData, DWORD *pdwSize) const
{
  return RegValGetNewBinary (this, pbyteData, pdwSize);
}

/* get a string */
bool CRegVal::GetString (LPTSTR pszString, DWORD dwLength) const
{
  return RegValGetString (this, pszString, dwLength);
}

/* get a new string */
bool CRegVal::GetNewString (LPTSTR *pszString, DWORD *pdwLength) const
{
  return RegValGetNewString (this, pszString, pdwLength);
}

/* get an array of strings */
bool CRegVal::GetStringArr (LPTSTR pszStrings[], DWORD dwCount) const
{
  return RegValGetStringArr (this, pszStrings, dwCount);
}

/* get a new array of strings */
bool CRegVal::GetNewStringArr (LPTSTR **pszStrings, DWORD *pdwCount) const
{
  return RegValGetNewStringArr (this, pszStrings, pdwCount);
}

#ifdef REG_WITH_MFC

/* get a string */
bool CRegVal::GetString (CString &sString) const
{
  return RegValGetString (this, sString);
}

/* get an array of strings */
bool CRegVal::GetStringArr (CStringArray &arrString) const
{
  return RegValGetStringArr (this, arrString);
}

#endif /* REG_WITH_MFC */

/* set a number */
void CRegVal::SetNumber (DWORD dwNumber)
{
  RegValSetNumber (this, dwNumber);
}

/* set binary data */
bool CRegVal::SetBinary (const LPBYTE pbyteData, DWORD dwSize)
{
  return RegValSetBinary (this, pbyteData, dwSize);
}

/* set a string */
bool CRegVal::SetString (LPCTSTR pszString)
{
  return RegValSetString (this, pszString);
}

/* set an array of strings */
bool CRegVal::SetStringArr (const LPCTSTR pszStrings[], DWORD dwCount)
{
  return RegValSetStringArr (this, pszStrings, dwCount);
}

#ifdef REG_WITH_MFC

/* set an array of strings */
bool CRegVal::SetStringArr (const CStringArray &arrString)
{
  return RegValSetStringArr (this, arrString);
}

#endif /* REG_WITH_MFC */

/* constructor - automatically initializes registry data */
CReg::CReg ()
{
  Open ();
}

/* destructor - automatically frees registry data */
CReg::~CReg ()
{
  Close ();
}

/* connect to remote computer registry */
HKEY CReg::Connect (HKEY hNewKey, LPCTSTR pszRemote)
{
  return hKey = RegConnect (hNewKey, pszRemote);
}

/* connect to registry key */
HKEY CReg::Open (HKEY hNewKey /*= NULL*/)
{
  return hKey = hNewKey;
}

/* open computer registry */
HKEY CReg::Open (HKEY hNewKey, LPCTSTR pszSubKey, DWORD dwRights)
{
  return hKey = RegOpen (hNewKey, pszSubKey, dwRights);
}

/* close computer registry */
void CReg::Close ()
{
  if (hKey != NULL) // MAB 8 Nov 1999 - added NULL test
    {
      RegClose (hKey);
      //*** MIPO 07-12-1999 - After Closing the Key, hKey must by NULL ****
      // RegClose - dont do that - it must be done manualy
      hKey = NULL;
    }
}

/* create computer registry */
HKEY CReg::Create (HKEY hNewKey, LPCTSTR pszSubKey, DWORD dwRights)
{
  return hKey = RegCreate (hNewKey, pszSubKey, dwRights);
}

/* load data of any type */
bool CReg::LoadVal (LPCTSTR pszValName, RegVal *pValData)
{
  return RegLoadVal (hKey, NULL, pszValName, pValData);
}

/* load data of any type from subkey */
bool CReg::LoadVal (LPCTSTR pszSubKey, LPCTSTR pszValName, RegVal *pValData)
{
  return RegLoadVal (hKey, pszSubKey, pszValName, pValData);
}

/* load a number */
bool CReg::LoadNumber (LPCTSTR pszValName, DWORD *pdwNumber)
{
  return RegLoadNumber (hKey, NULL, pszValName, pdwNumber);
}

/* load a number from subkey */
bool CReg::LoadNumber (LPCTSTR pszSubKey, LPCTSTR pszValName, DWORD *pdwNumber)
{
  return RegLoadNumber (hKey, pszSubKey, pszValName, pdwNumber);
}

/* load binary data */
bool CReg::LoadBinary (LPCTSTR pszValName, LPBYTE pbyteData, DWORD dwSize)
{
  return RegLoadBinary (hKey, NULL, pszValName, pbyteData, dwSize);
}

/* load binary data from subkey */
bool CReg::LoadBinary (LPCTSTR pszSubKey, LPCTSTR pszValName, LPBYTE pbyteData, DWORD dwSize)
{
  return RegLoadBinary (hKey, pszSubKey, pszValName, pbyteData, dwSize);
}

/* load new binary data */
bool CReg::LoadNewBinary (LPCTSTR pszValName, LPBYTE *pbyteData, DWORD *pdwSize)
{
  return RegLoadNewBinary (hKey, NULL, pszValName, pbyteData, pdwSize);
}

/* load new binary data from subkey */
bool CReg::LoadNewBinary (LPCTSTR pszSubKey, LPCTSTR pszValName, LPBYTE *pbyteData, DWORD *pdwSize)
{
  return RegLoadNewBinary (hKey, pszSubKey, pszValName, pbyteData, pdwSize);
}

/* load a string */
bool CReg::LoadString (LPCTSTR pszValName, LPTSTR pszString, DWORD dwLength)
{
  return RegLoadString (hKey, NULL, pszValName, pszString, dwLength);
}

/* load a string from subkey */
bool CReg::LoadString (LPCTSTR pszSubKey, LPCTSTR pszValName, LPTSTR pszString, DWORD dwLength)
{
  return RegLoadString (hKey, pszSubKey, pszValName, pszString, dwLength);
}

/* load a new string */
bool CReg::LoadNewString (LPCTSTR pszValName, LPTSTR *pszString, DWORD *pdwLength)
{
  return RegLoadNewString (hKey, NULL, pszValName, pszString, pdwLength);
}

/* load a new string from subkey */
bool CReg::LoadNewString (LPCTSTR pszSubKey, LPCTSTR pszValName, LPTSTR *pszString, DWORD *pdwLength)
{
  return RegLoadNewString (hKey, pszSubKey, pszValName, pszString, pdwLength);
}

/* load an array of strings */
bool CReg::LoadStringArr (LPCTSTR pszValName, LPTSTR pszStrings[], DWORD dwCount)
{
  return RegLoadStringArr (hKey, NULL, pszValName, pszStrings, dwCount);
}

/* load an array of strings from subkey */
bool CReg::LoadStringArr (LPCTSTR pszSubKey, LPCTSTR pszValName, LPTSTR pszStrings[], DWORD dwCount)
{
  return RegLoadStringArr (hKey, pszSubKey, pszValName, pszStrings, dwCount);
}

/* load a new array of strings */
bool CReg::LoadNewStringArr (LPCTSTR pszValName, LPTSTR **pszStrings, DWORD *pdwCount)
{
  return RegLoadNewStringArr (hKey, NULL, pszValName, pszStrings, pdwCount);
}

/* load a new array of strings from subkey */
bool CReg::LoadNewStringArr (LPCTSTR pszSubKey, LPCTSTR pszValName, LPTSTR **pszStrings, DWORD *pdwCount)
{
  return RegLoadNewStringArr (hKey, pszSubKey, pszValName, pszStrings, pdwCount);
}

#ifdef REG_WITH_MFC

/* load a string */
bool CReg::LoadString (LPCTSTR pszValName, CString &sString)
{
  return RegLoadString (hKey, NULL, pszValName, sString);
}

/* load a string from subkey */
bool CReg::LoadString (LPCTSTR pszSubKey, LPCTSTR pszValName, CString &sString)
{
  return RegLoadString (hKey, pszSubKey, pszValName, sString);
}

/* load an array of strings */
bool CReg::LoadStringArr (LPCTSTR pszValName, CStringArray &arrString)
{
  return RegLoadStringArr (hKey, NULL, pszValName, arrString);
}

/* load an array of strings from subkey */
bool CReg::LoadStringArr (LPCTSTR pszSubKey, LPCTSTR pszValName, CStringArray &arrString)
{
  return RegLoadStringArr (hKey, pszSubKey, pszValName, arrString);
}

#endif /* REG_WITH_MFC */

/* store data of any type */
bool CReg::SaveVal (LPCTSTR pszValName, const RegVal *pValData)
{
  return RegSaveVal (hKey, NULL, pszValName, pValData);
}

/* store data of any type to subkey */
bool CReg::SaveVal (LPCTSTR pszSubKey, LPCTSTR pszValName, const RegVal *pValData)
{
  return RegSaveVal (hKey, pszSubKey, pszValName, pValData);
}

/* store a number */
bool CReg::SaveNumber (LPCTSTR pszValName, DWORD dwNumber)
{
  return RegSaveNumber (hKey, NULL, pszValName, dwNumber);
}

/* store a number to subkey */
bool CReg::SaveNumber (LPCTSTR pszSubKey, LPCTSTR pszValName, DWORD dwNumber)
{
  return RegSaveNumber (hKey, pszSubKey, pszValName, dwNumber);
}

/* store binary data */
bool CReg::SaveBinary (LPCTSTR pszValName, const LPBYTE pbyteData, DWORD dwSize)
{
  return RegSaveBinary (hKey, NULL, pszValName, pbyteData, dwSize);
}

/* store binary data to subkey */
bool CReg::SaveBinary (LPCTSTR pszSubKey, LPCTSTR pszValName, const LPBYTE pbyteData, DWORD dwSize)
{
  return RegSaveBinary (hKey, pszSubKey, pszValName, pbyteData, dwSize);
}

/* store a string */
bool CReg::SaveString (LPCTSTR pszValName, LPCTSTR pszString)
{
  return RegSaveString (hKey, NULL, pszValName, pszString);
}

/* store a string to subkey */
bool CReg::SaveString (LPCTSTR pszSubKey, LPCTSTR pszValName, LPCTSTR pszString)
{
  return RegSaveString (hKey, pszSubKey, pszValName, pszString);
}

/* store an array of strings */
bool CReg::SaveStringArr (LPCTSTR pszValName, const LPCTSTR pszStrings[], DWORD dwCount)
{
  return RegSaveStringArr (hKey, NULL, pszValName, pszStrings, dwCount);
}

/* store an array of strings to subkey */
bool CReg::SaveStringArr (LPCTSTR pszSubKey, LPCTSTR pszValName, const LPCTSTR pszStrings[], DWORD dwCount)
{
  return RegSaveStringArr (hKey, pszSubKey, pszValName, pszStrings, dwCount);
}

#ifdef REG_WITH_MFC

/* store an array of strings */
bool CReg::SaveStringArr (LPCTSTR pszValName, const CStringArray &arrString)
{
  return RegSaveStringArr (hKey, NULL, pszValName, arrString);
}

/* store an array of strings to subkey */
bool CReg::SaveStringArr (LPCTSTR pszSubKey, LPCTSTR pszValName, const CStringArray &arrString)
{
  return RegSaveStringArr (hKey, pszSubKey, pszValName, arrString);
}

#endif /* REG_WITH_MFC */

/* delete the given value or key in the registry with all of its subkeys */
bool CReg::DeleteKey (LPCTSTR pszValName)
{
  return RegDeleteKey (hKey, NULL, pszValName);
}

/* delete the given value or key in the registry with all of its subkeys in subkey */
bool CReg::DeleteKey (LPCTSTR pszSubKey, LPCTSTR pszValName)
{
  return RegDeleteKey (hKey, pszSubKey, pszValName);
}

/* delete all of subkeys in the key */
bool CReg::DeleteSubKeys ()
{
  return RegDeleteSubKeys (hKey);
}

/* check wether the given key has other subkeys and/or values */
bool CReg::HasEntries (DWORD *pdwSubKeyCount, DWORD *pdwValueCount)
{
  return RegHasEntries (hKey, NULL, pdwSubKeyCount, pdwValueCount);
}

/* check wether the given key has other subkeys and/or values in subkey */
bool CReg::HasEntries (LPCTSTR pszSubKey, DWORD *pdwSubKeyCount, DWORD *pdwValueCount)
{
  return RegHasEntries (hKey, pszSubKey, pdwSubKeyCount, pdwValueCount);
}

/* walks to the first value */
bool CReg::FindFirstValue (LPCTSTR &ppszValue, RegVal *pValData)
{
  return RegFindFirstValue (hKey, &ppszValue, pValData);
}

/* walks to the next value */
bool CReg::FindNextValue (LPCTSTR &ppszValue, RegVal *pValData)
{
  return RegFindNextValue (hKey, &ppszValue, pValData);
}

/* closes registry walking */
void CReg::FindClose ()
{
  RegFindClose ();
}

#endif /* cplusplus */

/*////////////////////////////////////////////////////////////////////////////*/
#pragma warning ( default : 4711 )

⌨️ 快捷键说明

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