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

📄 registry.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 3 页
字号:
///////////////////////////////////////////////////////////////////////////
//  File:    registry.cpp
//  Version: 1.1.0.4
//  Updated: 19-Jul-1998
//
//  Copyright:  Ferdinand Prantl
//  E-mail:     prantl@ff.cuni.cz
//
//  Some handy stuff to deal with Windows registry
//
//  You are free to use or modify this code to the following restrictions:
//  - Acknowledge me somewhere in your about box, simple "Parts of code by.."
//  will be enough. If you can't (or don't want to), contact me personally.
//  - LEAVE THIS HEADER INTACT
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
//  14-Sep-99
//  + FIX: closing null handle (Michael A. Barnhart)
////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "registry.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

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

/* initializes registry value data */
void
RegValInit (RegVal *pValData)
{
  ASSERT (pValData);
  pValData->dwType = REG_NONE;
}

/* frees registry value data */
void
RegValFree (RegVal *pValData)
{
  ASSERT (pValData);
  if (pValData->dwType == REG_SZ || pValData->dwType == REG_EXPAND_SZ
        || pValData->dwType == REG_LINK || pValData->dwType == REG_MULTI_SZ
        || pValData->dwType == REG_BINARY)
    {
      free (pValData->pbyteData);
      pValData->dwType = REG_NONE;
    }
}

/* get a number */
bool
RegValGetNumber (const RegVal *pValData, DWORD *pdwNumber)
{
  ASSERT (pValData &&pdwNumber);
  if (pValData->dwType == REG_DWORD)
    {
      *pdwNumber = pValData->dwNumber;
      return true;
    }
  return false;
}

/* get binary data */
bool
RegValGetBinary (const RegVal *pValData, LPBYTE pbyteData, DWORD dwSize)
{
  ASSERT (pValData &&pbyteData);
  if (pValData->dwType == REG_BINARY &&dwSize >= pValData->dwSize)
    {
      memcpy (pbyteData, pValData->pbyteData, pValData->dwSize);
      return true;
    }
  return false;
}

/* get new binary data */
bool
RegValGetNewBinary (const RegVal *pValData, LPBYTE *pbyteData, DWORD *pdwSize)
{
  ASSERT (pValData &&pbyteData);
  if (pValData->dwType == REG_BINARY)
    {
      LPBYTE pbyteNewData = (LPBYTE) malloc (pValData->dwSize);
      if (pbyteNewData)
        {
          *pbyteData = pbyteNewData;
          *pdwSize = pValData->dwSize;
          memcpy (pbyteNewData, pValData->pbyteData, pValData->dwSize);
          return true;
        }
    }
  return false;
}

/* get a new string */
bool
RegValGetNewString (const RegVal *pValData, LPTSTR *pszString, DWORD *pdwLength)
{
  ASSERT (pValData &&pszString);
  if (pValData->dwType == REG_SZ || pValData->dwType == REG_EXPAND_SZ
        || pValData->dwType == REG_LINK || pValData->dwType == REG_MULTI_SZ)
    {
      LPTSTR pszNewString = (LPTSTR) malloc (pValData->dwLength + 1);
      if (pszNewString)
        {
          *pszString = pszNewString;
          if (pdwLength)
            {
              *pdwLength = pValData->dwLength;
            }
          memcpy (pszNewString, pValData->pszString, pValData->dwLength);
          pszNewString [pValData->dwLength] = _T ('\0');
          return true;
        }
    }
  return false;
}

/* get a string */
bool
RegValGetString (const RegVal *pValData, LPTSTR pszString, DWORD dwLength)
{
  ASSERT (pValData &&pszString);
  if ((pValData->dwType == REG_SZ || pValData->dwType == REG_EXPAND_SZ
         || pValData->dwType == REG_LINK || pValData->dwType == REG_MULTI_SZ)
        && dwLength >= pValData->dwLength)
    {
      memcpy (pszString, pValData->pszString, pValData->dwLength);
      pszString [pValData->dwLength] = _T ('\0');
      return true;
    }
  return false;
}

/* get an array of strings */
bool
RegValGetStringArr (const RegVal *pValData, LPTSTR pszStrings[], DWORD dwCount)
{
  ASSERT (pValData);
  if (pValData->dwType == REG_MULTI_SZ)
    {
      LPCTSTR pszString;
      DWORD dwRealCount = 0, dwLength;
      for (pszString = pValData->pszString; *pszString; pszString += dwLength)
        {
          dwLength = _tcslen (pszString) + 1;
          dwRealCount++;
        }
      if (dwCount >= dwRealCount)
        {
          LPTSTR *pszDstString = pszStrings;
          for (LPCTSTR pszString = pValData->pszString; *pszString; pszString += dwLength, pszDstString++)
            {
              dwLength = _tcslen (pszString) + 1;
              LPTSTR pszNewString = (LPTSTR) malloc (dwLength);
              *pszDstString = pszNewString;
              if (pszNewString)
                {
                  while ((*pszNewString = (BYTE) *pszString) != _T ('\0'))
                    {
                      pszNewString++;
                      pszString++;
                    }
                }
              else
                {
                  while (*pszString)
                    {
                      pszString++;
                    }
                }
            }
          return true;
        }
    }
  return false;
}

/* get a new array of strings */
bool
RegValGetNewStringArr (const RegVal *pValData, LPTSTR **pszStrings, DWORD *pdwCount)
{
  ASSERT (pValData);
  if (pValData->dwType == REG_MULTI_SZ)
    {
      LPTSTR pszString;
      DWORD dwRealCount = 0, dwLength;
      for (pszString = pValData->pszString; *pszString; pszString += dwLength)
        {
          dwLength = _tcslen (pszString) + 1;
          dwRealCount++;
        }
      LPTSTR *pszNewStrings = (LPTSTR *) malloc (dwRealCount *sizeof (LPTSTR));
      if (pszNewStrings)
        {
          *pszStrings = pszNewStrings;
          *pdwCount = dwRealCount;
          for (pszString = pValData->pszString; *pszString; pszString += dwLength, pszNewStrings++)
            {
              dwLength = _tcslen (pszString) + 1;
              LPTSTR pszNewString = (LPTSTR) malloc (dwLength);
              *pszNewStrings = pszNewString;
              if (pszNewString)
                {
                  while ((*pszNewString = (BYTE) *pszString) != _T ('\0'))
                    {
                      pszNewString++;
                      pszString++;
                    }
                }
              else
                {
                  while (*pszString)
                    {
                      pszString++;
                    }
                }
            }
          return true;
        }
    }
  return false;
}

#ifdef REG_WITH_MFC

/* get a string */
bool
RegValGetString (const RegVal *pValData, CString &sString)
{
  ASSERT (pValData);
  if (pValData->dwType == REG_SZ || pValData->dwType == REG_EXPAND_SZ
        || pValData->dwType == REG_LINK || pValData->dwType == REG_MULTI_SZ)
    {
      LPTSTR pszString = sString.GetBuffer (pValData->dwLength + 1);
      memcpy (pszString, pValData->pszString, pValData->dwLength);
      pszString [pValData->dwLength] = _T ('\0');
      sString.ReleaseBuffer ();
      return true;
    }
  return false;
}

/* get an array of strings */
bool
RegValGetStringArr (const RegVal *pValData, CStringArray &arrString)
{
  ASSERT (pValData);
  if (pValData->dwType == REG_MULTI_SZ)
    {
      arrString.RemoveAll ();
      for (LPCTSTR pszString = pValData->pszString; *pszString; pszString += _tcslen (pszString) + 1)
        {
          arrString.Add (pszString);
        }
      return true;
    }
  return false;
}

#endif /* REG_WITH_MFC */

/* set a number */
void
RegValSetNumber (RegVal *pValData, DWORD dwNumber)
{
  ASSERT (pValData);
  pValData->dwType = REG_DWORD;
  pValData->dwNumber = dwNumber;
}

/* set binary data */
bool
RegValSetBinary (RegVal *pValData, const LPBYTE pbyteData, DWORD dwSize)
{
  ASSERT (pValData &&pbyteData);
  pValData->pbyteData = (LPBYTE) malloc (dwSize);
  if (pValData->pbyteData)
    {
      pValData->dwSize = dwSize;
      pValData->dwType = REG_BINARY;
      memcpy (pValData->pbyteData, pbyteData, dwSize);
      return true;
    }
  pValData->dwType = REG_NONE;
  return false;
}

/* set a string */
bool
RegValSetString (RegVal *pValData, LPCTSTR pszString)
{
  ASSERT (pValData &&pszString);
  DWORD dwLength = _tcslen (pszString) + 1;
  pValData->pszString = (LPTSTR) malloc (dwLength);
  if (pValData->pszString)
    {
      pValData->dwLength = dwLength;
      pValData->dwType = REG_SZ;
      memcpy (pValData->pbyteData, pszString, dwLength);
      return true;
    }
  pValData->dwType = REG_NONE;
  return false;
}

bool
RegValSetStringArr (RegVal *pValData, const LPCTSTR pszStrings[], DWORD dwCount)
{
  ASSERT (pValData &&pszStrings);
  DWORD i, dwSize = 1;
  if (dwCount)
    {
      for (i = 0; i < dwCount; i++)
        {
          dwSize += _tcslen (pszStrings[i]) + 1;
        }
    }
  else
    {
      dwSize++;
    }
  pValData->pbyteData = (LPBYTE) malloc (dwSize);
  if (pValData->pbyteData)
    {
      pValData->dwSize = dwSize;
      pValData->dwType = REG_MULTI_SZ;
      LPBYTE pbyteData = pValData->pbyteData;
      if (dwCount)
        {
          for (i = 0; i < dwCount; i++)
            {
              LPCTSTR pszString = pszStrings[i];
              while ((*pbyteData++ = (BYTE) *pszString) != _T ('\0'))
                {
                  pszString++;
                }
            }
        }
      else
        {
          *pbyteData++ = _T ('\0');
        }
      *pbyteData = _T ('\0');
      return true;
    }
  pValData->dwType = REG_NONE;
  return false;
}

#ifdef REG_WITH_MFC

/* set an array of strings */
bool
RegValSetStringArr (RegVal *pValData, const CStringArray &arrString)
{
  ASSERT (pValData);
  DWORD i, dwSize = 1, dwCount = arrString.GetSize ();
  if (dwCount)
    {
      for (i = 0; i < dwCount; i++)
        {
          dwSize += arrString[i].GetLength () + 1;
        }
    }
  else
    {
      dwSize++;
    }
  pValData->pbyteData = (LPBYTE) malloc (dwSize);
  if (pValData->pbyteData)
    {
      pValData->dwSize = dwSize;
      pValData->dwType = REG_MULTI_SZ;
      LPBYTE pbyteData = pValData->pbyteData;
      if (dwCount)
        {
          for (i = 0; i < dwCount; i++)
            {
              LPCTSTR pszString = arrString[i];
              while ((*pbyteData++ = (BYTE) *pszString) != _T ('\0'))
                {
                  pszString++;
                }
            }
        }
      else
        {
          *pbyteData++ = _T ('\0');
        }
      *pbyteData = _T ('\0');
      return true;
    }
  pValData->dwType = REG_NONE;
  return false;
}

#endif /* REG_WITH_MFC */

/* connect to remote computer registry */
HKEY
RegConnect (HKEY hKey, LPCTSTR pszRemote)
{
  HKEY hSubKey;
  if (RegConnectRegistry (pszRemote, hKey, &hSubKey) == ERROR_SUCCESS)
    {
      return hSubKey;
    }
  return NULL;
}

/* open computer registry */
HKEY
RegOpen (HKEY hKey, LPCTSTR pszSubKey, DWORD dwRights)
{
  HKEY hSubKey;
  if (RegOpenKeyEx (hKey, pszSubKey, 0, dwRights, &hSubKey) == ERROR_SUCCESS)
    {
      return hSubKey;
    }
  return NULL;
}

/* create computer registry */
HKEY
RegCreate (HKEY hKey, LPCTSTR pszSubKey, DWORD dwRights)
{
  HKEY hSubKey;
  DWORD dwDisposition;
  if (RegCreateKeyEx (hKey, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, dwRights,
                        NULL, &hSubKey, &dwDisposition) == ERROR_SUCCESS)
    {
      return hSubKey;
    }
  return NULL;
}

/* close computer registry */
void
RegClose (HKEY hKey)
{
  if (hKey)
    RegCloseKey (hKey);
}

/* load data of any type */
bool
RegLoadVal (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, RegVal *pValData)
{
  ASSERT (pValData);
  HKEY hSubKey = pszSubKey ? RegOpen (hKey, pszSubKey, KEY_READ) : hKey;
  if (hSubKey)
    {
      DWORD dwType, dwSize;
      if (RegQueryValueEx (hSubKey, pszValName, 0, &dwType, NULL, &dwSize) == ERROR_SUCCESS)
        {
          if (dwType == REG_DWORD)
            {
              ASSERT (dwSize == sizeof (DWORD));
              DWORD dwNumber;
              if (RegQueryValueEx (hSubKey, pszValName, 0, NULL, (LPBYTE) &dwNumber, &dwSize) == ERROR_SUCCESS)
                {
                  ASSERT (dwSize == sizeof (DWORD));
                  RegValFree (pValData);
                  pValData->dwType = dwType;
                  pValData->dwNumber = dwNumber;
                  if (hSubKey != hKey)
                    RegClose (hSubKey);
                  return true;
                }
            }
          else if (dwType == REG_SZ || dwType == REG_EXPAND_SZ || dwType == REG_LINK
                || dwType == REG_MULTI_SZ || dwType == REG_BINARY)
            {
              LPBYTE pbyteData = (LPBYTE) malloc (dwSize);
              if (pbyteData)
                {
                  if (RegQueryValueEx (hSubKey, pszValName, 0, NULL, pbyteData, &dwSize) == ERROR_SUCCESS)
                    {
                      RegValFree (pValData);
                      pValData->dwType = dwType;
                      pValData->pbyteData = pbyteData;

⌨️ 快捷键说明

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