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

相关推荐
xcyxiner1 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner2 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner2 天前
DicomViewer (添加模型类)3
qt
xcyxiner3 天前
DicomViewer (目录调整) 2
qt
xcyxiner3 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
倔强的石头_3 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab3 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
clint4564 天前
C++进阶(1)——前景提要
c++
ClouGence4 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
夜悊4 天前
C++代码示例:进制数简单生成工具
c++