VC注册表操作

注册表是什么

注册表是WINDOWS的大管家,系统的设置、记录、程序关联都在里面。他其实就是一个数据库,在系统中处于核心地位,在后台管理WINDOWS。


注册表怎样打开

开始--运行中输入regedit就可以了





注册表操作

CRegConfig

头文件
// RegConfig.h: interface for the CRegConfig class.
// Written by Alexander Hritonenkov on march 19, 2003
//

#if !defined(AFX_REGCONFIG_H__7E051819_5CAA_4E49_99F2_0236B42086A4__INCLUDED_)
#define AFX_REGCONFIG_H__7E051819_5CAA_4E49_99F2_0236B42086A4__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "atlbase.h"

class CRegConfig  
{
public:
	void SetRegKey(HKEY hKey, LPCTSTR strKey);	// Sets registry key to keep settings in
	void Load();	// Loads settings from registry
	void Save();	// Saves settings to registry
	CRegConfig(HKEY hKey, LPCTSTR strKey);	// Constructor that sets registry key to keep settings in
	CRegConfig();	// Default constructor
	virtual ~CRegConfig();	// Destructor (empty)

protected:
	HKEY m_hKey;	// Parent key descriptor
	CString m_strKey;	// Our key full name
	CRegKey m_rKey;
	virtual void TransferAllData(BOOL bSave)=0;	// Data transfering function
	void Transfer(BOOL bSave, LPCTSTR strValueName, CString &strValue);	// Function to load/save string data
	void Transfer(BOOL bSave, LPCTSTR strValueName, DWORD &dwValue);	// Function to load/save DWORD data
	void Transfer(BOOL bSave, LPCTSTR strValueName, CByteArray &bValue);	// Function to load/save binary data
};

#endif // !defined(AFX_REGCONFIG_H__7E051819_5CAA_4E49_99F2_0236B42086A4__INCLUDED_)

源文件
// RegConfig.cpp: implementation of the CRegConfig class.
// Written by Alexander Hritonenkov on march 19, 2003
//

#include "stdafx.h"
#include "config.h"
#include "RegConfig.h"

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

//
// Construction/Destruction
//

CRegConfig::CRegConfig(HKEY hKey, LPCTSTR strKey)
{
	SetRegKey(hKey, strKey);
}

CRegConfig::CRegConfig()
{
	m_hKey=NULL;
	m_strKey="";
}

CRegConfig::~CRegConfig()
{
}

void CRegConfig::SetRegKey(HKEY hKey, LPCTSTR strKey)
{
	m_hKey=hKey;
	m_strKey=strKey;
}

void CRegConfig::Save()
{
	if(m_rKey.Create(m_hKey, m_strKey)==ERROR_SUCCESS)
	{
		TransferAllData(TRUE);
		m_rKey.Close();
	}
}

void CRegConfig::Load()
{
	if(m_rKey.Create(m_hKey, m_strKey)==ERROR_SUCCESS)
	{
		TransferAllData(FALSE);
		m_rKey.Close();
	}
}

void CRegConfig::Transfer(BOOL bSave, LPCTSTR strValueName, DWORD &dwValue)
{
	if(bSave)
		m_rKey.SetValue(dwValue, strValueName);
	else
		m_rKey.QueryValue(dwValue, strValueName);
}

void CRegConfig::Transfer(BOOL bSave, LPCTSTR strValueName, CString &strValue)
{
	if(bSave)
		m_rKey.SetValue(strValue, strValueName);
	else
	{
		DWORD dwLen;
		LPTSTR lpStr;
		if(m_rKey.QueryValue(NULL, strValueName, &dwLen)==ERROR_SUCCESS)
		{
			lpStr=strValue.GetBuffer(dwLen+1);
			m_rKey.QueryValue(lpStr, strValueName, &dwLen);
			strValue.ReleaseBuffer(dwLen);
		}
	}
}

void CRegConfig::Transfer(BOOL bSave, LPCTSTR strValueName, CByteArray &bValue)
{
	DWORD dwLen, dwType;
	LPBYTE lpData;
	if(bSave)
	{
		dwLen=bValue.GetSize();
		lpData=bValue.GetData();
		RegSetValueEx(m_rKey.m_hKey, strValueName, 0, REG_BINARY, lpData, dwLen);
	}
	else
	{
		dwType=REG_BINARY;
		if(RegQueryValueEx(m_rKey.m_hKey, strValueName, NULL, &dwType, NULL, &dwLen)==ERROR_SUCCESS)
		{
			bValue.RemoveAll();
			bValue.SetSize(dwLen);
			lpData=bValue.GetData();
			RegQueryValueEx(m_rKey.m_hKey, strValueName, NULL, &dwType, lpData, &dwLen);
		}
	}
}

CMyRegConfig

头文件
// MyRegConfig.h: interface for the CMyRegConfig class.
// Written by Alexander Hritonenkov on march 19, 2003
//

#if !defined(AFX_MYREGCONFIG_H__9F26EC5C_AE0B_4259_A4B4_A09A40FB89D7__INCLUDED_)
#define AFX_MYREGCONFIG_H__9F26EC5C_AE0B_4259_A4B4_A09A40FB89D7__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "RegConfig.h"

class CMyRegConfig : public CRegConfig  
{
public:
	CMyRegConfig();
	virtual ~CMyRegConfig();

	// data members
	DWORD m_dwValue1;
	DWORD m_dwValue2;
	CString m_strValue;
	CByteArray m_bData;

protected:
	virtual void TransferAllData(BOOL bSave);	// overrided pure virtual function for perform saving and loading data
};

#endif // !defined(AFX_MYREGCONFIG_H__9F26EC5C_AE0B_4259_A4B4_A09A40FB89D7__INCLUDED_)

源文件
// MyRegConfig.cpp: implementation of the CMyRegConfig class.
// Written by Alexander Hritonenkov on march 19, 2003
//

#include "stdafx.h"
#include "config.h"
#include "MyRegConfig.h"

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

//
// Construction/Destruction
//

CMyRegConfig::CMyRegConfig() : CRegConfig(HKEY_LOCAL_MACHINE, "Software\\test")
{
	// set default values for our settings
	// they are used on first start (if data is not present in registry)
	m_dwValue1=100;
	m_dwValue2=200;
	m_strValue="Sample string";

	Load();	// you can add automatic load of config on construction
}

CMyRegConfig::~CMyRegConfig()
{
	Save();	// you can add automatic save of config on destruction
}

void CMyRegConfig::TransferAllData(BOOL bSave)
{
	// add Transfer() function for each data member in your class
	Transfer(bSave, "Value1", m_dwValue1);
	Transfer(bSave, "Value2", m_dwValue2);
	Transfer(bSave, "String", m_strValue);
	Transfer(bSave, "Data", m_bData);
}

操作界面



相关操作
void CConfigDlg::OnBtnOk() 
{
	SaveData();
	OnOK();
}

void CConfigDlg::LoadData()
{
	SetDlgItemInt(ID_VAL1, m_Config.m_dwValue1, 0);
	SetDlgItemInt(ID_VAL2, m_Config.m_dwValue2, 0);
	SetDlgItemText(ID_STRING, m_Config.m_strValue);
	LPCTSTR lpStr=(LPCTSTR)m_Config.m_bData.GetData();
	if(lpStr!=NULL)
	{
		SetDlgItemText(ID_BINARY, lpStr);
	}
}

void CConfigDlg::SaveData()
{
	m_Config.m_dwValue1=GetDlgItemInt(ID_VAL1, NULL, 0);
	m_Config.m_dwValue2=GetDlgItemInt(ID_VAL2, NULL, 0);
	GetDlgItemText(ID_STRING, m_Config.m_strValue);
	CString str;
	GetDlgItemText(ID_BINARY, str);
	m_Config.m_bData.RemoveAll();
	m_Config.m_bData.SetSize(str.GetLength()+1);
	memcpy(m_Config.m_bData.GetData(), (LPCTSTR)str, str.GetLength());
}

运行效果

根据如下所知,是要在HKEY_LOCAL_MACHINE项的Software下添加一项test



运行前,注册表中没有这一项



运行后,我们发现多了一个我们生成的这个项
 

注册表中多了一项test
 

修改值
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值