-1

Hey I'm new to C++ and I am trying to find out if a specified registry index exists. I have to check multiple locations due to the possibility of the software being run on a 64bit machine and being under the WOW6432Node key instead of the usual position. When RegQueryValueExA (using visual c++ 6.0 on xp so I can't use a newer function) is run it should return a Boolean of true if the key exists, (I'll deal with getting the value of the key later). However on run it generates access violation 0xc00005. Any ideas whats gone wrong?

bool FindAndRemoveUninstall(string path){
    bool result;
    result = RegQueryValueExA(HKEY_LOCAL_MACHINE, 
    TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\ABC"), NULL, NULL, NULL, (unsigned long *)MAX_PATH);
    if (result= ERROR_SUCCESS){
        cout <<" is a 32 bit program\n";
        //path= Value in key
    }
    result = RegQueryValueEx(HKEY_LOCAL_MACHINE, 
    TEXT("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\ABC"), NULL, NULL, NULL, (unsigned long *)MAX_PATH);
    if (result= ERROR_SUCCESS){
        cout << " is 64 bit program\n";
        //path= Value in key
    }
    return true;
}
en93
  • 19
  • 2

1 Answers1

4

You have multiple problems.

The last parameter to RegQueryValueExA is documented as

lpcbData [in, out, optional]

A pointer to a variable that specifies the size of the buffer pointed to by the lpData parameter,

But you are not passing a pointer to a variable. You are passing (unsigned long *)MAX_PATH, which is a garbage pointer. When the operating system tries to store the result into the pointer, it takes an access violation. You need to pass a pointer to a variable, like the documentation says.

The next problem is that you are calling the A function (explicit ANSI) but using the TEXT macro (adaptive character set). Make up your mind which model you are using (ANSI or adaptive) and choose one model or the other. Let's assume you explicit ANSI.

The next problem is that you didn't specify an output buffer, so you don't actually retrieve the path.

Another problem is that the RegQueryValueExA function does not return a bool; it returns an error code.

Yet another problem is that your if test contains an assignment, so it does not actually test anything.

Another problem is that you didn't specify a way for the function to return the path to the caller. Let's assume you want the result to be returned in the path parameter.

Yet another problem is that you have the 32-bit and 64-bit cases reversed.

Also, you are using '\n' instead of std::endl.

The eight problem is that your function returns true even if it didn't do anything.

And the ninth problem is that the function says FindAndRemove, and it finds, but doesn't remove.

bool FindUninstall(string& path){ // parameter passed by reference, fix function name
    LONG result; // change variable type
    char buffer[MAX_PATH]; // provide an output buffer
    DWORD bufferSize = MAX_PATH; // and a variable to specify the buffer size / receive the data size
    result = RegQueryValueExA(HKEY_LOCAL_MACHINE, 
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\ABC", NULL, NULL, (LPBYTE)buffer, &bufferSize); // remove TEXT macro, pass the buffer and buffer size
    if (result== ERROR_SUCCESS){ // fix comparison
        cout <<" is a 64 bit program" << std::endl; // fix message
        path = buffer;
                return true; // stop once we have an answer
    }
        buffersize = MAX_PATH; // reset for next query
    result = RegQueryValueEx(HKEY_LOCAL_MACHINE, 
    "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\ABC", NULL, NULL, (LPBYTE)buffer, &bufferSize); // remove TEXT macro, pass the buffer and buffer size
    if (result== ERROR_SUCCESS){ // fix comparison
        cout << " is 32 bit program" << std::endl; // fix message
        path = buffer;
                return true; // stop once we have an answer
    }
    return false; // nothing found
}

Since you are new to C++, I would recommend that you get some experience with C++ doing simpler projects before diving into more complicated things like this.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135