RegEnumValue崩溃了应用程序

Ant*_*ton 1 c++ registry winapi

我目前正在学习如何使用c ++使用注册表.我已经创建了一个应用程序,可以查看某个键中是否存在某个值.但是,应用程序一到达RegEnumValue()就会崩溃.对问题可能是什么的任何想法?

码:

#include <iostream>
#include <windows.h>
#include <Psapi.h>

using namespace std;

bool registerKeyExists(char* key, char* subkey);

int main()
{
    while(true){
        if(registerKeyExists("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "SynTPEnh")){
            return 0;
        }
    }

    return 0;
}

bool registerKeyExists(char* key, char* subkey){
    HKEY keyEnum;
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &keyEnum) == ERROR_SUCCESS){
        char* valueName;
        DWORD valueSize = 100;
        DWORD cbName;
        FILETIME lastFiletime;
        DWORD i = 0;
        DWORD returnCode = ERROR_SUCCESS;
        while(returnCode == ERROR_SUCCESS){
            cout << "This show!" << endl;
            returnCode = RegEnumValue(keyEnum, i, valueName, &valueSize, NULL, NULL, NULL, NULL);
            cout << "This doesn't show!" << endl;
            if(valueName == subkey)
                return true;
            i++;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*ill 5

您没有为值的名称提供任何空间.你是传递一个未初始化的指针valueNameRegEnumValue(),它拒绝(显然,由崩溃您的应用程序的方式).请尝试以下方法:

char valueName[100];
DWORD valueSize = sizeof(valueName);
Run Code Online (Sandbox Code Playgroud)

这为返回的值名称保留了100个字符.

您还希望使用strcmp()而不是==测试字符串值:

if (strcmp(valueName, subkey) == 0) ...
Run Code Online (Sandbox Code Playgroud)