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位,又做了许多兼容的设计,才出现了这些过渡的配置。

相关推荐
原来是猿6 分钟前
关于【进程池阻塞 + 子进程未回收问题】
linux·服务器·c++
C澒10 分钟前
PC 桌面富应用:速分客户端
前端·c++·electron·web app
深邃-15 分钟前
数据结构-双向链表
c语言·开发语言·数据结构·c++·算法·链表·html5
2401_8785302116 分钟前
分布式任务调度系统
开发语言·c++·算法
Fortune7920 分钟前
用Pandas处理时间序列数据(Time Series)
jvm·数据库·python
艾莉丝努力练剑22 分钟前
【Linux:文件】文件基础IO进阶
linux·运维·服务器·c语言·网络·c++·centos
2401_8785302125 分钟前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
2401_8735449229 分钟前
使用Black自动格式化你的Python代码
jvm·数据库·python
艾莉丝努力练剑29 分钟前
【MYSQL】MYSQL学习的一大重点:表的约束
linux·运维·服务器·开发语言·数据库·学习·mysql
Fortune7931 分钟前
用Python破解简单的替换密码
jvm·数据库·python