C++如何读写注册表

1.如何向注册表中写值

//写入注册表为DWORD类型
bool SetRegValue(LPCTSTR lpSubKey, LPCTSTR lpValueName, DWORD dwValueKey)
{
	bool bRet = false;
	HKEY hKey;
	DWORD dwDispoistion = REG_OPENED_EXISTING_KEY;

	if (ERROR_SUCCESS == ::RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &hKey, &dwDispoistion))
	{
		if (ERROR_SUCCESS == ::RegSetValueEx(hKey, lpValueName, 0, REG_DWORD, (BYTE*)&dwValueKey, sizeof(DWORD)))
		{
			bRet = true;
		}
	}
	::RegCloseKey(hKey);
	return bRet;
}


// 写入注册表为LPCTSTR 类型
bool SetRegValue(LPCTSTR lpSubKey, LPCTSTR lpValueName, LPCTSTR lpValueKey)
{
	bool bRet = false;
	HKEY hKey;
	DWORD dwDisposition = REG_OPENED_EXISTING_KEY;

	if (ERROR_SUCCESS == ::RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &hKey, &dwDisposition))
	{
		if (ERROR_SUCCESS == ::RegSetValueEx(hKey, lpValueName, 0, REG_SZ, (BYTE*)lpValueKey,
			_tcslen(lpValueKey) * sizeof(TCHAR)))
		{
			bRet = true;
		}
	}
	::RegCloseKey(hKey);
	return bRet;
}
注:其中上面HKEY_LOCAL_MACHINE代表注册表中 众多主目录中的一个目录,也可以是其他主目录



写注册表示例如下:

//写入DWORD类型
SetRegValue(L"SYSTEM\\CurrentControlSet\\Services\\USBSTOR",	L"Start", 8);
//写入LPCTSTR类型
LPCTSTR lpSubKey = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\RoomManagement";
LPCTSTR lpValue = L"hello world";
bool bRet = SetRegValue(lpSubKey, L"YourKey", lpValue); //参数二三分别代表要写入的 键和值



2.如何读取注册表的值

// 读注册表的值(output --- wstring)
bool GetRegValue(HKEY hMainKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, std::wstring& wstrReturn/*out*/)
{
	bool bRet = false;
	HKEY hKey;
	if (ERROR_SUCCESS == ::RegOpenKeyEx(hMainKey, lpSubKey, 0, KEY_READ, &hKey))
	{
		DWORD dwType = REG_SZ;
		TCHAR szReturnValue[MAX_PATH + 1] = { 0 };
		DWORD dwSize;
		if (ERROR_SUCCESS == ::RegQueryValueEx(hKey, lpValueName, NULL, &dwType, (LPBYTE)szReturnValue, &dwSize))
		{
			wstrReturn = szReturnValue;
			bRet = true;
		}
	}
	::RegCloseKey(hKey);
	return bRet;
}

// 读出册表(output --- string)
bool GetRegValue(LPCTSTR lpSubKey, LPCTSTR lpValueName, std::string& strReturn/*out*/)
{
	bool bRet = false;
	HKEY hKey;
	if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_READ, &hKey))
	{
		DWORD dwType = REG_SZ;
		TCHAR szReturnValue[MAX_PATH + 1] = { 0 };
		DWORD dwSize;
		if (ERROR_SUCCESS == ::RegQueryValueEx(hKey, lpValueName, NULL, &dwType, (LPBYTE)szReturnValue, &dwSize))
		{
			// 将宽字节转换成多字节
			int dwBufSize = ::WideCharToMultiByte(CP_OEMCP, 0, szReturnValue, -1, NULL, 0, NULL, FALSE);
			char *buff = new char[dwBufSize];
			memset(buff, 0, dwBufSize);
			if (::WideCharToMultiByte(CP_OEMCP, 0, szReturnValue, -1, buff, dwBufSize, NULL, FALSE))
			{
				strReturn = buff;
				delete[] buff;
				buff = NULL;
			}
			bRet = true;
		}
	}
	::RegCloseKey(hKey);
	return bRet;
}


读注册表示例如下:

//读取wstring类型
LPCTSTR lpSubkey = _T("Software\\Microsoft\\Windows\\CurrentVersion\\YourFileName");
wstring	wsValue;
GetRegValue(HKEY_CURRENT_USER, lpSubkey, _T("YourKey"), wsValue);
//读取string类型
string strValue;
LPCTSTR lpSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\YourFileName");
GetRegValue(lpSubKey, _T("YourKey"), strValue);



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值