转载请注明出处:http://blog.csdn.net/drecik__/article/details/8059631
最近在写一个软件,需要存放配置,由于很小所以不打算存放在其他文件中
想到存放在注册表中,参考网上资料,自己写了个操作注册表的类
//
// 文件:MyRegKey.h
// 功能:对CMyRegKey类进行声明;
// Made by: Drecik;
#ifndef _DRE_MYREGKEY_H_
#define _DRE_MYREGKEY_H_
#include <Windows.h>
#include <tchar.h>
#include <WinReg.h>
class CMyRegKey
{
public:
CMyRegKey();
~CMyRegKey();
// 打开关闭注册表;
bool RegOpen( HKEY hRootKey, LPCTSTR szPath );
bool RegClose();
// 利用重载函数实现对注册表的读写(字符串,DWORD,二进制);
bool RegRead( LPCTSTR szKey, LPTSTR szValue, DWORD& dwLen );
bool RegRead( LPCTSTR szKey, DWORD& dwValue );
bool RegRead( LPCTSTR szKey, BYTE* pData, DWORD& dwLen );
bool RegWrite( LPCTSTR szKey, LPCTSTR szValue );
bool RegWrite( LPCTSTR szKey, const DWORD dwValue );
bool RegWrite( LPCTSTR szKey, const BYTE* pData, DWORD dwLen );
// 删除注册表项;
bool RegDelete( LPCTSTR szKey );
protected:
HKEY m_hKey; // 注册表项;
TCHAR m_szPath[MAX_PATH]; // 注册表目录;
};
#endif
#include "MyRegKey.h"
CMyRegKey::CMyRegKey()
{
m_hKey = NULL;
ZeroMemory( m_szPath, sizeof(m_szPath) );
}
CMyRegKey::~CMyRegKey()
{
RegClose();
}
bool CMyRegKey::RegOpen( HKEY hRootKey, LPCTSTR szPath )
{
// 关闭上一次打开的注册表,如果有的话;
RegClose();
DWORD dw;
int iRet = RegCreateKeyEx( hRootKey,szPath, 0L, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &m_hKey,&dw);
if ( iRet != ERROR_SUCCESS )
return false;
return true;
}
bool CMyRegKey::RegClose()
{
if ( m_hKey )
{
RegCloseKey(m_hKey);
m_hKey = NULL;
}
ZeroMemory( m_szPath, sizeof(m_szPath) );
return true;
}
bool CMyRegKey::RegRead( LPCTSTR szKey, LPTSTR szValue, DWORD& dwLen )
{
DWORD dwType = 0;
int iRet = RegQueryValueEx( m_hKey, szKey, NULL, &dwType, (BYTE*)szValue, &dwLen );
if ( iRet != ERROR_SUCCESS )
return false;
return true;
}
bool CMyRegKey::RegRead( LPCTSTR szKey, DWORD& dwValue )
{
DWORD dwType = 0;
DWORD dwSize = sizeof(DWORD);
int iRet = RegQueryValueEx( m_hKey, szKey, NULL, &dwType, (BYTE*)&dwValue, &dwSize );
if ( iRet != ERROR_SUCCESS )
return false;
return true;
}
bool CMyRegKey::RegRead( LPCTSTR szKey, BYTE* pData, DWORD& dwLen )
{
DWORD dwType = 0;
int iRet = RegQueryValueEx( m_hKey, szKey, NULL, &dwType, pData, &dwLen );
if ( iRet != ERROR_SUCCESS )
return false;
return true;
}
bool CMyRegKey::RegWrite( LPCTSTR szKey, LPCTSTR szValue )
{
int iLen = _tcslen(szValue);
// 这里测试的时候发现,如果是宽字符,得到的长度是实际数据长度的一半,所以乘以2;
#ifdef _UNICODE
iLen *= 2;
#endif
int iRet = RegSetValueEx( m_hKey, szKey, 0, REG_SZ, (const BYTE*)szValue, iLen );
if ( iRet != ERROR_SUCCESS )
return false;
return true;
}
bool CMyRegKey::RegWrite( LPCTSTR szKey, const DWORD dwValue )
{
int iRet = RegSetValueEx( m_hKey, szKey, 0, REG_DWORD, (const BYTE*)&dwValue, sizeof(DWORD) );
if ( iRet != ERROR_SUCCESS )
return false;
return true;
}
bool CMyRegKey::RegWrite( LPCTSTR szKey, const BYTE* pData, DWORD dwLen )
{
int iRet = RegSetValueEx( m_hKey, szKey, 0, REG_BINARY, (const BYTE*)pData, dwLen );
if ( iRet != ERROR_SUCCESS )
return false;
return true;
}
bool CMyRegKey::RegDelete( LPCTSTR szKey )
{
// 这里测试的时候发现删除某一个Key时,即使存在;
// iRet 也返回2,即没有找到到文件,不知道为什么;
// 希望有哪位大神知道告诉我下;
int iRet = RegDeleteKey( m_hKey, szKey );
if ( iRet != ERROR_SUCCESS )
return false;
return true;
}