win32-注册表-32位-64位-读写值-Qt-C++

文章目录

1.32访问64位和64位访问32位

32位的应用程序想访问64位的注册表视图的标志是KEY_WOW64_64KEY,该标志的值是0x0100。64位的应用程序想访问32位的注册表视图的标志是KEY_WOW64_32KEY。以上两个标志可以在RegCreateKeyEx()函数、RegDeleteKeyEx()函数和RegOpenKeyEx()函数中指定。

cpp 复制代码
LONG lReturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\***", 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hKey);

范例,32位读取64位注册表。

cpp 复制代码
//写注册表信息
static bool SetRegValue(const std::string& item, const std::string& value) {
	HKEY hkey = nullptr;
	LSTATUS res = ::RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Corel\\Setup\\CorelDRAW Graphics Suite 2019", 0, KEY_WRITE | KEY_WOW64_64KEY, &hkey);
	if (res != ERROR_SUCCESS) {
		res = ::RegCreateKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Corel\\Setup\\CorelDRAW Graphics Suite 2019", &hkey);
	}
	if (res != ERROR_SUCCESS) {
		if (hkey != nullptr) {
			::RegCloseKey(hkey);
			hkey = nullptr;
		}
		return false;
	}
	res = ::RegSetValueExA(hkey, item.c_str(), 0, REG_SZ, (BYTE*)value.c_str(), value.length());
	if (res != ERROR_SUCCESS) {
		return false;
	}
	return true;
}

//读取注册表信息
static std::string GetRegValue(const std::string& item) {
	HKEY hkey = nullptr;
	LSTATUS res = ::RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Corel\\Setup\\CorelDRAW Graphics Suite 2019", 0, KEY_READ | KEY_WOW64_64KEY, &hkey);
	if (res != ERROR_SUCCESS) {
		if (hkey != nullptr) {
			::RegCloseKey(hkey);
			hkey = nullptr;
		}
		return "";
	}
	DWORD type = REG_SZ;
	DWORD size = 0;
	res = ::RegQueryValueExA(hkey, item.c_str(), 0, &type, nullptr, &size);
	if (res != ERROR_SUCCESS || size <= 0) {
		return "";
	}
	std::vector<BYTE> value_data(size);
	res = ::RegQueryValueExA(hkey, item.c_str(), 0, &type, value_data.data(), &size);
	if (res != ERROR_SUCCESS) {
		return "";
	}
	return std::string(value_data.begin(), value_data.end());
}

2.在Qt中qsetting的使用

32程序访问64位注册表,必须使用QSettings::Registry64Format标志,而不能简单使用本地标志QSettings::NativeFormat。

cpp 复制代码
QString regPos = QString::fromLocal8Bit("HKEY_LOCAL_MACHINE/SOFTWARE/Corel/Setup/CorelDRAW Graphics Suite 2019");
regPos.replace("/", "\\");
QSettings tQSettings(regPos, QSettings::Registry64Format);//32程序访问64位注册表-必须采用这种方式
QString destination = tQSettings.value("Destination").toString();

3.总结

注册表,是windows系统保存配置的地方,64位系统为了兼容32位,又做了许多兼容的设计,才出现了这些过渡的配置。

相关推荐
ma_king1 小时前
入门 java 和 数据库
java·数据库·后端
jiayou644 小时前
KingbaseES 实战:审计追踪配置与运维实践
数据库
NineData16 小时前
NineData 迁移评估功能正式上线
数据库·dba
NineData21 小时前
数据库迁移总踩坑?用 NineData 迁移评估,提前识别所有兼容性风险
数据库·程序员·云计算
赵渝强老师1 天前
【赵渝强老师】PostgreSQL中表的碎片
数据库·postgresql
blasit1 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
全栈老石1 天前
拆解低代码引擎核心:元数据驱动的"万能表"架构
数据库·低代码
倔强的石头_2 天前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
肆忆_2 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星2 天前
虚函数表:C++ 多态背后的那个男人
c++