Question

Im using Visual Studio 2003. The program in question is an MFC application.

My program deals with opening, reading from, and then closing registry keys. I assume that I am opening and reading the key fine (because there are no invalids, message boxes, or any other error finding methods I've implemented showing up).

However, when I debug my program, I get "...Invalid HANDLE was specified..." (including a memory address). I've tried stepping through the code using the debugger but I've been so fun unable to follow things.

The portion of the code where the error occurs is:

HKEY hKey;
char *subKey = "\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port ";
strcat(subKey, scsiPortNum);
LONG openResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey, 0, KEY_ALL_ACCESS, &hKey);
if(openResult != ERROR_SUCCESS){
MessageBox(0, "There was an error closing the registry key", "Error", MB_OK);
}
//...do a few RegQueries...
LONG closeResult = RegCloseKey(hKey); //******Invalid HANDLE occurs here*******
if(closeResult != ERROR_SUCCESS){
MessageBox(0, "There was an error closing the registry key", "Error", MB_OK);
}

(scsiPortNum is char[2] and is just the port number)

Like I said, I'm not getting any error/msg boxes with open, but I am with closing...If anyone can point out where I went wrong I would be greatly appreciative! :)

UPDATE: Based on luskan's answer below, I altered my code and thus got closer to the issue. The code structer now looks like this:

HKEY hKey;
try{
char subKey[MAX_PATH];
strcpy(subKey, "\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port ");
strcat(subKey, scsiPortNum);
auto openResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey, 0, KEY_ALL_ACCESS, &hKey);
if(openResult == ERROR_SUCCESS){
//do query...
//close key
auto closeResult = RegCloseKey(hKey);
if(closeResult != ERROR_SUCCESS){
MessageBox(0, "Error closing the key", "Registry Error", MB_OK);
}
}else{
MessageBox(0, "Error opening the key", "Registry Error", MB_OK);
}
}catch(...){
MessageBox(0, "You caught an exception!", "Try/Catch", MB_OK);
}

And I get the message about opening the key now... perhaps I'm misunderstanding the arguments that RegOpenKeyEx takes? or am I not initalizing something correctly?

Was it helpful?

Solution

Well the answer is very, Very, VERY simple folks...

I was using "\HARDWARE\DEVICEMAP\Scsi\Scsi Port ". What ended up being correct was "HARDWARE\DEVICEMAP\Scsi\Scsi Port "...I had an extra \.

I figured this out by finding which error code RegOpenKeyEx returned. (See Below).


For anyone debugging returns from RegOpenEx, or RegQueryValue, or any of the other Reg functions, you can see which Error the function returns by using the following (and you could probably improve it too!):

char buffer[250]; //size of buffer is not that important
for(long a = 0; a < 1500; a++) {
if(result == a) {
_itoa((int)a, buffer, 10);
MessageBox(0, buffer, "Error from Reg Function", MB_ICONERROR | MB_OK);
}
}

The result of this little snippet give you a number, in decimal, for you to check against winerror.h. If you do a search for the number then L - e.g. "161L" you will find what the error means.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top