Win32 对注册表添加和删除的操作

转载请注明出处: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;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值