我的 winreg 函数出现错误 2 [英] I am getting error 2 in my winreg function

查看:55
本文介绍了我的 winreg 函数出现错误 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

HKEY hKey;
char *path = "SYSTEM\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles\\0001\\HwProfileGuid";
LONG result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS, &hKey);
QString q = QString::number(result);

if (result == ERROR_SUCCESS) {
    QMessageBox messageBox1;
    messageBox1.critical(0,"Error", "Success");
    messageBox1.setFixedSize(500,200);
} else {
    QMessageBox messageBox2;
    messageBox2.critical(0,"Error", q);
    messageBox2.setFixedSize(500,200);
}

我得到的错误:

密钥在我的注册表中的位置:

Where the key is in my Registry:

我认为问题与我将信息放入 path 变量的方式有关,但我不确定.

I think the problem is related to the way I put the info in the path variable, but I am not sure.

推荐答案

HwProfileGuid 放错地方了.

HwProfileGuid0001 键内的 value,但您试图打开 HwProfileGuid 作为而是 0001sub-key,这就是为什么您会收到错误 2 (ERROR_FILE_NOT_FOUND),因为没有 sub-key 名为 HwProfileGuid.

You have HwProfileGuid in the wrong place.

HwProfileGuid is a value inside of the 0001 key, but you are trying to open HwProfileGuid as a sub-key of 0001 instead, which is why you are getting error 2 (ERROR_FILE_NOT_FOUND), because there is no sub-key named HwProfileGuid.

此外,KEY_ALL_ACCESS 请求的权限太多,无法仅从键中读取值.请改用 KEY_QUERY_VALUE.不要要求比实际需要更多的权利.

Also, KEY_ALL_ACCESS is too many rights to request just to read a value from a key. Use KEY_QUERY_VALUE instead. Don't request more rights than you actually need.

试试这个:

const char *path = "SYSTEM\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles\\0001";
const char *valueName = "HwProfileGuid";
char guid[40] = {0};

HKEY hKey;
LONG result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, path, 0, KEY_QUERY_VALUE, &hKey);
if (result == ERROR_SUCCESS) {
    DWORD size = sizeof(guid);
    result = RegQueryValueExA(hKey, valueName, NULL, NULL, reinterpret_cast<LPBYTE>(guid), &size);
    RegCloseKey(hKey);
}

QMessageBox messageBox;
if (result == ERROR_SUCCESS) {
    messageBox.critical(0, "Success", guid);
} else {
    messageBox.critical(0, "Error", QString::number(result));
}
messageBox.setFixedSize(500, 200);

或者,您可以使用 RegGetValueA() 而不是使用RegOpenKeyExA()+RegQueryValueExA():

Alternatively, you can use RegGetValueA() instead of using RegOpenKeyExA()+RegQueryValueExA():

const char *path = "SYSTEM\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles\\0001";
const char *valueName = "HwProfileGuid";
char guid[40] = {0};
DWORD size = sizeof(guid);
QMessageBox messageBox;

LSTATUS result = RegGetValueA(HKEY_LOCAL_MACHINE, path, valueName, RRF_RT_REG_SZ, NULL, guid, &size);
if (result == ERROR_SUCCESS) {
    messageBox.critical(0, "Success", guid);
} else {
    messageBox.critical(0, "Error", QString::number(result));
}
messageBox.setFixedSize(500, 200);

这篇关于我的 winreg 函数出现错误 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆