📄 registry.cpp
字号:
#include "stdafx.h"
#include "Registry.h"
/*================================================================
* 函数名: CRegistry
* 参数: (HKEY hRootKey)
* 说明: 如果构造函数不带参数,则使用默认的参数,m_hRootKey被初始化
为HKEY_LOCAL_MACHINE, 如果带有参数则 m_hRootKey为指定的值
================================================================*/
CRegistry::CRegistry(HKEY hRootKey)
{
m_hRootKey = hRootKey;
}
CRegistry::~CRegistry() //在析构函数中关闭打开注册表句柄
{
Close();
}
/*================================================================
* 函数名: VerifyKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 判断给定的路径是否存在 (兼有打开的功能)
如果第一个参数为NULL,则使用默认的根键。
* 返回值: BOOL
================================================================*/
BOOL CRegistry::VerifyKey (LPCTSTR pszPath)
{
LONG ReturnValue = ::RegOpenKeyEx (m_hRootKey, pszPath, 0L,
KEY_ALL_ACCESS, &m_hSubKey);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: VerifyValue
* 参数: (LPCTSTR pszValue)
* 功能描述: 判断给定的值是否存在 (请先调用VerifyKey,然后在使用该函数)
* 返回值: BOOL
================================================================*/
BOOL CRegistry::VerifyValue (LPCTSTR pszValue)
{
LONG lReturn = ::RegQueryValueEx(m_hSubKey, pszValue, NULL,
NULL, NULL, NULL);
if(lReturn == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: VerifyValue
* 参数: (LPCTSTR pszValue)
* 功能描述: 判断指定的键名是否等于某个值
* 返回值: BOOL
================================================================*/
BOOL CRegistry::IsEqual(LPCTSTR pszValue,int nn)
{
int nTemp;
this->Read(pszValue,nTemp);
if(nTemp==nn)
return TRUE;
return FALSE;
}
BOOL CRegistry::IsEqual(LPCTSTR pszValue,DWORD dw)
{
DWORD dwTemp;
this->Read(pszValue,dwTemp);
if(dwTemp==dw)
return TRUE;
return FALSE;
}
BOOL CRegistry::IsEqual(LPCTSTR pszValue,LPCTSTR lpsz)
{
CString str;
this->Read(pszValue,str);
if(str.CompareNoCase(lpsz)==0)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: CreateKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 创建路径
* 返回值: BOOL
================================================================*/
BOOL CRegistry::CreateKey (LPCTSTR pszPath)
{
DWORD dw;
LONG ReturnValue = ::RegCreateKeyEx (m_hRootKey, pszPath, 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL,
&m_hSubKey, &dw);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: Write
* 参数: (LPCTSTR lpszKeyName, int iVal)
* 功能描述: 写入整型值
* 返回值: BOOL
================================================================*/
BOOL CRegistry::Write (LPCTSTR lpszKeyName, int iVal)
{
DWORD dwValue;
dwValue = (DWORD)iVal;
LONG ReturnValue = ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_DWORD,
(CONST BYTE*) &dwValue, sizeof(DWORD));
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: Write
* 参数: (LPCTSTR lpszKeyName, DWORD dwVal)
* 功能描述: 写入DWORD值
* 返回值: BOOL
================================================================*/
BOOL CRegistry::Write (LPCTSTR lpszKeyName, DWORD dwVal)
{
return ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_DWORD,
(CONST BYTE*) &dwVal, sizeof(DWORD));
}
/*================================================================
* 函数名: Write
* 参数: (LPCTSTR lpszKeyName, LPCTSTR pszData)
* 功能描述: 写入字符串值
* 返回值: BOOL
================================================================*/
BOOL CRegistry::Write (LPCTSTR lpszKeyName, LPCTSTR pszData)
{
LONG ReturnValue = ::RegSetValueEx (m_hSubKey, lpszKeyName, 0L, REG_SZ,
(CONST BYTE*) pszData, strlen(pszData) + 1);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
/*================================================================
* 函数名: Read
* 参数: (LPCTSTR lpszKeyName, int& iVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述: 读取整数
* 返回值: BOOL
================================================================*/
BOOL CRegistry::Read(LPCTSTR lpszKeyName, int& iVal)
{
DWORD dwType;
DWORD dwSize = sizeof (DWORD);
DWORD dwDest;
LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
&dwType, (BYTE *) &dwDest, &dwSize);
if(lReturn == ERROR_SUCCESS)
{
iVal = (int)dwDest;
return TRUE;
}
return FALSE;
}
/*================================================================
* 函数名: Read
* 参数: (LPCTSTR lpszKeyName, DWORD& dwVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述: 读取DWORD值
* 返回值: BOOL
================================================================*/
BOOL CRegistry::Read (LPCTSTR lpszKeyName, DWORD& dwVal)
{
DWORD dwType;
DWORD dwSize = sizeof (DWORD);
DWORD dwDest;
LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
&dwType, (BYTE *) &dwDest, &dwSize);
if(lReturn == ERROR_SUCCESS)
{
dwVal = dwDest;
return TRUE;
}
return FALSE;
}
/*================================================================
* 函数名: Read
* 参数: (LPCTSTR lpszKeyName, CString& sVal) 第2个参数通过引用传递,可以在函数中修改实参
* 功能描述: 读取字符串值
* 返回值: BOOL
================================================================*/
BOOL CRegistry::Read (LPCTSTR lpszKeyName, CString& sVal)
{
DWORD dwType;
DWORD dwSize = 200;
char szString[255];
LONG lReturn = ::RegQueryValueEx (m_hSubKey, (LPSTR) lpszKeyName, NULL,
&dwType, (BYTE *) szString, &dwSize);
if(lReturn == ERROR_SUCCESS)
{
sVal = szString;
return TRUE;
}
return FALSE;
}
/*================================================================
* 函数名: DeleteValue
* 参数: (LPCTSTR pszValue)
* 功能描述: 删除值
* 返回值: BOOL
================================================================*/
BOOL CRegistry::DeleteValue (LPCTSTR pszValue)
{
if(::RegDeleteValue(m_hSubKey, pszValue)== ERROR_SUCCESS)
return TRUE;
else
return FALSE;
}
/*================================================================
* 函数名: DeleteKey
* 参数: (HKEY hRootKey, LPCTSTR pszPath)
* 功能描述: 删除路径
* 返回值: BOOL
================================================================*/
BOOL CRegistry::DeleteKey (LPCTSTR pszPath)
{
if(::RegDeleteKey(m_hRootKey, pszPath) == ERROR_SUCCESS)
return TRUE;
else
return FALSE;
}
/*================================================================
* 函数名: Close
* 参数:
* 功能描述: 关闭注册表
* 返回值: void
================================================================*/
void CRegistry::Close()
{
if (m_hSubKey)
{
::RegCloseKey (m_hSubKey);
m_hSubKey = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -