繁体   English   中英

如何使用C ++正确检查注册表项是否存在?

[英]How to properly check if a registry key exists with c++?

我的目标非常简单,我只想检查C ++是否存在注册表项。 我找不到任何有用的在线信息。 下面的代码编译没有错误,但是我得到输出“ Not open”,并且我知道我有该注册表项。

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
using namespace std;
int main(){
    HKEY hKey;
    if(RegOpenKey(HKEY_LOCAL_MACHINE,TEXT("Software\\Oracle\\VirtualBox"),&hKey) == ERROR_SUCCESS)    {

        cout << "Opened";
    }
    else
    {
        cout << "not opened";
    }
    return 0;
}

你的代码是好的(虽然你应该使用RegOpenKeyEx()来代替,如RegOpenKey()已被废弃,仅适用于16位代码的向后兼容性提供)。 您只需要注意返回值。 如果RegOpenKey/Ex()返回ERROR_PATH_NOT_FOUNDERROR_FILE_NOT_FOUND ,则该键不存在。 其他任何返回值都表示该密钥存在,并且如果返回值为ERROR_SUCCESS则需要关闭打开的密钥,否则即使该密钥存在,也无权打开该密钥。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM