I am currently learning how to work with the registry using c++. I have made an application that is supposed to see if a certain value in a certain key exists. However, the application crashes as soon as it reaches RegEnumValue(). Any ideas to what the problem might be?
Code:
#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;
}
You have not provided any space for the name of the value. You are passing an uninitialised pointer
valueNametoRegEnumValue(), which it rejects (apparently, by way of crashing your app). Try the following instead:This reserves 100 characters for the returned value name.
You will also want to use
strcmp()instead of==for testing string values: