C++ 注册表

百科:https://baike.baidu.com/item/%E6%B3%A8%E5%86%8C%E8%A1%A8/101856?fr=aladdin

CRegKey:https://blog.csdn.net/pbymw8iwm/article/details/6931946

C++ 注册表_第1张图片

HKEY_CURRENT_USER:管理系统当前的用户信息。在这个根键中保存了本地计算机中存放的当前登录的用户信息,包括用户登录用户名和暂存的密码。在用户登录Windows时,其信息从HKEY_USERS中相应的项拷贝到HKEY_CURRENT_USER中。

HKEY_CLASSES_ROOT是应用程序运行时必需的信息,和HKEY_LOCAL_MACHINE\SOFTWARE\Classes是一样的,但是在HKEY_CLASSES_ROOT窗编辑相对来说显得更容易和有条理。

在微软的Windows操作系统中,HKEY_LOCAL_MACHINE保存了注册表里的所有与这台计算机有关的配置信息,只是一个公共配置信息单元,对于读者来说,只需做一个大致的了解即可。

HKEY_USERS仅包含了缺省用户设置和登录用户的信息。虽然它包含了所有独立用户的设置,但在用户未登录时用户的设置是不可用的。这些设置告诉系统哪些图标会被使用,什么组可用,哪个开始菜单可用,哪些颜色和字体可用,和控制面板上什么选项和设置可用。

#define HKEY_CLASSES_ROOT                   (( HKEY ) (ULONG_PTR)((LONG)0x80000000) )
#define HKEY_CURRENT_USER                   (( HKEY ) (ULONG_PTR)((LONG)0x80000001) )
#define HKEY_LOCAL_MACHINE                  (( HKEY ) (ULONG_PTR)((LONG)0x80000002) )
#define HKEY_USERS                          (( HKEY ) (ULONG_PTR)((LONG)0x80000003) )
#define HKEY_PERFORMANCE_DATA               (( HKEY ) (ULONG_PTR)((LONG)0x80000004) )
#define HKEY_PERFORMANCE_TEXT               (( HKEY ) (ULONG_PTR)((LONG)0x80000050) )
#define HKEY_PERFORMANCE_NLSTEXT            (( HKEY ) (ULONG_PTR)((LONG)0x80000060) )
#if(WINVER >= 0x0400)
#define HKEY_CURRENT_CONFIG                 (( HKEY ) (ULONG_PTR)((LONG)0x80000005) )
#define HKEY_DYN_DATA                       (( HKEY ) (ULONG_PTR)((LONG)0x80000006) )
#define HKEY_CURRENT_USER_LOCAL_SETTINGS    (( HKEY ) (ULONG_PTR)((LONG)0x80000007) )

RegKeyEx.h

#pragma once
#include 
#include 
#include 
class CRegKeyEx :public CRegKey
{
public:
	CRegKeyEx();
	~CRegKeyEx();
public:
	bool QueryStringValue(HKEY hKey,const std::wstring& strRegPath,const std::wstring& strKey,std::wstring& strValue,REGSAM samDesired = KEY_WOW64_64KEY);
	bool SetStringValue(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, std::wstring& strValue, REGSAM samDesired = KEY_WOW64_64KEY);
	bool QueryDWORDValue(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, DWORD& dwValue, REGSAM samDesired = KEY_WOW64_64KEY);
	bool SetDWORDValue(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, DWORD dwValue,  REGSAM samDesired = KEY_WOW64_64KEY);
	bool SetBinaryValue(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, unsigned char* const bzValue, ULONG nBytes, REGSAM samDesired = KEY_WOW64_64KEY);
	bool RecurseDeleteKey(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, REGSAM samDesired = KEY_WOW64_64KEY);

};

RegKeyEx.cpp

#include "stdafx.h"
#include "RegKeyEx.h"


CRegKeyEx::CRegKeyEx()
{
}


CRegKeyEx::~CRegKeyEx()
{
}

bool CRegKeyEx::QueryStringValue(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, std::wstring& strValue, REGSAM samDesired/* = KEY_WOW64_64KEY*/)
{
	bool bSuccess = false;
	if (!strRegPath.empty())
	{
		ULONG nChars = MAX_PATH * 10;
		WCHAR szValue[MAX_PATH * 10] = { 0 };
		if (ERROR_SUCCESS == CRegKey::Open(hKey, strRegPath.c_str(), KEY_READ | samDesired)
			&& ERROR_SUCCESS == CRegKey::QueryStringValue(strKey.c_str(), szValue, &nChars))
		{
			strValue = szValue;
			bSuccess = true;
		}
	}
	return bSuccess;
}

bool CRegKeyEx::SetStringValue(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, std::wstring& strValue, REGSAM samDesired/* = KEY_WOW64_64KEY*/)
{
	bool bSuccess = false;
	if (false == strRegPath.empty() && (false == strKey.empty() || false == strValue.empty()))
	{
		if (ERROR_SUCCESS == CRegKey::Create(hKey, strRegPath.c_str(), nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE | samDesired)
			&& ERROR_SUCCESS == CRegKey::SetStringValue(strKey.c_str(), strValue.c_str()))
		{
			bSuccess = true;
		}
	}
	return bSuccess;
}

bool CRegKeyEx::QueryDWORDValue(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, DWORD& dwValue, REGSAM samDesired/* = KEY_WOW64_64KEY*/)
{
	bool bSuccess = false;
	if (false == strRegPath.empty())
	{
		ULONG nChars = MAX_PATH * 10;
		WCHAR szValue[MAX_PATH * 10] = { 0 };

		if (ERROR_SUCCESS == CRegKey::Open(hKey, strRegPath.c_str(), KEY_READ | KEY_QUERY_VALUE | samDesired)
			&& ERROR_SUCCESS == CRegKey::QueryDWORDValue(strKey.c_str(), dwValue))
		{
			bSuccess = true;
		}
	}
	return bSuccess;
}

bool CRegKeyEx::SetDWORDValue(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, DWORD dwValue, REGSAM samDesired/* = KEY_WOW64_64KEY*/)
{
	bool bSuccess = false;
	if (false == strRegPath.empty() && (false == strKey.empty()))
	{
		if (ERROR_SUCCESS == CRegKey::Create(hKey, strRegPath.c_str(), nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE | samDesired)
			&& ERROR_SUCCESS == CRegKey::SetDWORDValue(strKey.c_str(), dwValue))
		{
			bSuccess = true;
		}
	}
	return bSuccess;
}

bool CRegKeyEx::SetBinaryValue(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, unsigned char* const bzValue, ULONG nBytes, REGSAM samDesired/* = KEY_WOW64_64KEY*/)
{
	bool bSuccess = false;
	if (false == strRegPath.empty() && (false == strKey.empty()))
	{
		if (ERROR_SUCCESS == Create(hKey, strRegPath.c_str(), nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE | samDesired)
			&& ERROR_SUCCESS == CRegKey::SetBinaryValue(strKey.c_str(), bzValue, nBytes))
		{
			bSuccess = true;
		}
	}
	return bSuccess;
}

bool CRegKeyEx::RecurseDeleteKey(HKEY hKey, const std::wstring& strRegPath, const std::wstring& strKey, REGSAM samDesired/* = KEY_WOW64_64KEY*/)
{
	bool bSuccess = false;
	if (ERROR_SUCCESS == Open(hKey, strRegPath.c_str(), KEY_WRITE | samDesired))
	{
		LONG nResult = CRegKey::RecurseDeleteKey(strKey.c_str());
		if (ERROR_SUCCESS == nResult || ERROR_FILE_NOT_FOUND == nResult)
		{
			bSuccess = true;
		}
		else
		{
			nResult = CRegKey::DeleteSubKey(strKey.c_str());
			if (ERROR_SUCCESS == nResult || ERROR_FILE_NOT_FOUND == nResult)
			{
				bSuccess = true;
			}
		}
	}
	return bSuccess;
}

main.cpp

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "RegKeyEx.h"

#define REG_PATH L"Software\\chaoge"
#define REG_SCORE	L"Score"
#define REG_AGE		L"Age"
#define REG_BINARY	L"Binary"
int main()
{
	CRegKeyEx objRegKeyEx;
	std::wstring strScore = L"cet 4 100";
	objRegKeyEx.SetStringValue(HKEY_CURRENT_USER, REG_PATH, REG_SCORE,strScore);
	std::wstring strRet;
	objRegKeyEx.QueryStringValue(HKEY_CURRENT_USER, REG_PATH, REG_SCORE, strRet);
	std::wcout << strRet << std::endl;

	objRegKeyEx.SetDWORDValue(HKEY_CURRENT_USER, REG_PATH, REG_AGE, 18);
	DWORD dwRet;
	objRegKeyEx.QueryDWORDValue(HKEY_CURRENT_USER, REG_PATH, REG_AGE, dwRet);
	std::wcout << dwRet << std::endl;

	unsigned char buf[2] = { 0x02,0x03 };
	objRegKeyEx.SetBinaryValue(HKEY_CURRENT_USER, REG_PATH, REG_BINARY, buf, 2);

	objRegKeyEx.RecurseDeleteKey(HKEY_CURRENT_USER, REG_PATH, REG_BINARY);

    return 0;
}

 

你可能感兴趣的:(C++,VC)