Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7883987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:40:46+00:00 2026-06-03T04:40:46+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:40:47+00:00Added an answer on June 3, 2026 at 4:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Visual Studio 2003 to compile and run following program. There are
I'm using Visual Studio 2003 working on an ASP.NET application. Quite often, just in
This is a question about using Visual Studio 2003. Sorry it is not strictly
Using Visual Studio .NET 2003 C++ and the wininet.dll Am seeing many C4995 warnings
I am extending the Visual Studio 2003 debugger using autoexp.dat and a DLL to
Using Visual Studio 2008, I created a C++ Win32 project. To release the program,
While developing and ASP.NET application in C# or VB using Visual Studio 2005/2008/2010 (Not
Required to write a serial communication C++ program in Visual Studio C++ 2003 with
I have a web control developed using visual studio 2003. We are reusing this
How do I retrieve a Boolean value in dataset, I'm using visual studio 2003,I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.