Reading a BYTE from registry

I can open, close, read and write string values from the registry so I don't need any functions for that. But the problem I have is that I can't read in Binary values.

I'm using the "RegQueryValueEx" function call.

I simply want to read in the following registry value:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\RemoteAccess\Remote Connection


I want to know when the value is "0" and when it is something else.


Thanks for the help.

microboardAsked:
Who is Participating?
 
jhanceConnect With a Mentor Commented:
You want to read a REG_BINARY data type from the registry.  This is a two-step process:

1) Determine how large the data to be read is:

HKEY hKey;
LONG lResult;
DWORD dwSize = 0;
LPBYTE pBuffer = NULL;
DWORD dwType;

lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\RemoteAccess", 0, KEY_READ, &hKey);

if(lResult != ERROR_SUCCESS){
  // Error, do something useful
  return;
}

lResult = RegQueryValueEx(hKey, "Remote Connection", NULL, &dwType, pBuffer, &dwSize);

if(lResult != ERROR_MORE_DATA){
  // Unexpected error, do something useful
  RegCloseKey(hKey);
  return;
}

// Now, dwSize has the size of the buffer needed
pBuffer = new BYTE[dwSize];

lResult = RegQueryValueEx(hKey, "Remote Connection", NULL, &dwType, pBuffer, &dwSize);
RegCloseKey(hKey);

if(lResult != ERROR_SUCCESS){
  // Error
  return;
}

// OK, here, pBuffer has your REG_BINARY data
0
 
jkrCommented:
Hmm, can't find that one in my registry - what data type is it?
0
 
jhanceCommented:
Sorry, I forgot to put in the 2), but the example has it all there...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
microboardAuthor Commented:
jhance, the code executes this error trap:

if(lResult != ERROR_MORE_DATA){
 // Unexpected error, do something useful
 RegCloseKey(hKey);
 return;
}


The registry key and string are on my computer so I'm not sure why it hangs at this point.

0
 
jhanceCommented:
What error codes DOES it return?

I'm assuming that you provided the KEY and the VALUE names properly.  Double check them.


0
 
microboardAuthor Commented:
All the names are correct.

To put it more plainly, when execution reaches this part of the code:

if(lResult != ERROR_MORE_DATA){
// Unexpected error, do something useful
RegCloseKey(hKey);
return;
}

the variable lResult equals 0.

The variable ERROR_MORE_DATA equals 234 at this point. According to some text I have, that means the RegQuery failed because the Registry value was too large for the buffer.

I don't really know anything about what I just wrote, I'm just relaying information.



0
 
microboardAuthor Commented:
OK, after reading and reading some information, I realize that the iResult variable needs to equal ERROR_MORE_DATA. I now understand more of the code you wrote.

What I did is substituted the ERROR_MORE_DATA variable with the ERROR_SUCCESS variable so that execution would continue, just to see what I'd get.

The pBuffer variable is empty when not connected to the internet, but instead of expressing a number value when it is connected, it shows the terminating string character instead. At least it looks like the terminating string  character, like a thick pipe.

I tried to say something like:

if (pBuffer)
  {
    //Internet Connected
  }

But pBuffer seems to always execute TRUE no matter what.

I hope I've given some useful information to go on. Maybe I just need to be able to convert the pBuffer variable into an integer.

0
 
jhanceCommented:
Well, if it's returning ERROR_SUCCESS on the first attempt to read rather than ERROR_MORE_DATA, that means that the SIZE of the value is 0 bytes.  That is perfectly legal and could be what you have.

I'm not commenting on the MEANING of what is or is NOT in this particular value in the registry and that is not the topic of your question.  

You asked HOW to read a BINARY (i.e. REG_BINARY) value from the registry.  So I think the INTERPRETATION what WHAT is in there is up to you.
0
 
microboardAuthor Commented:
No, in the key I am reading, there is a value when I am connected to the internet. The value is 1. When I am NOT connected to the internet the value is 0. The code you've suggested is not reading in the value at all. It is simply failing. I can manipulate the code to allow the execution to continue, but the value I get is unreadable to me.

You made this comment: "I'm not commenting on the MEANING of what is or is NOT in this particular value in the registry and that is not the topic of your question. You asked HOW to read a BINARY (i.e. REG_BINARY) value from the registry."

You need to look at the question again. I can't put an entire question in the topic. I did ask about reading in a binary value, but as a programmer you should realize a value is no good to you unless you can do something with it. That's why I also asked this: "I want to know when the value is "0" and when it is something else."

I appreciate your help, but your code is no good to me as it is.
0
 
jhanceCommented:
Where is the code I posted failing?  You said it was returning ERROR_SUCCESS.  That is NOT a failure.  It is an indication that there are 0 bytes in the value you are reading.

Sorry, I tried to help you....
0
 
jhanceCommented:
Please post your code AND EXPORT the registry key in question.  Since I don't find this key mentioned anywhere, I can't test.
0
 
weiccoCommented:
"No, in the key I am reading, there is a value when I am connected to the internet. The value is 1. When
I am NOT connected to the internet the value is 0."

Do you want to check that you have internet connection?
If that's so, you don't need to read it from registy, use wininet api, like this...

DWORD dwFlags;
if (InternetGetConnectedState(dwFlags, 0)) {
    /* Yes, we have connection, dwFlags will tell you what kind of connection */
}

Include wininet.h and link with wininet.lib. You can find these on platfrom sdk.

Registry reading goes like this...

HKEY hKey;
DWORD dwSize = 0;
unsigned char *lpBuffer = NULL;

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\RemoteAccess\", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
    /* First get the size of data */
    RegQueryValueEx(hKey, "Remote Connection", 0, NULL, NULL, &dwSize);
    /* Allocate space for data */
    lpBuffer = (unsigned char *)malloc(dwSize);
    /* Now we can read the value */
    RegQueryValueEx(hKey, "Remote Connection", 0, NULL, lpBuffer, &dwSize);
    RegCloseKey(hKey);
}

There isn't much error checking there, you can add it yourself. Also that won't work if the value is type SZ, MULTI_SZ or REG_EXPAND_SZ.
0
 
microboardAuthor Commented:
As far as I know, InternetGetConnectedState only works if Internet Explorer is installed. I don't want to depend on that. That's why I'm going to use the registry.


jhance, by adjusting your code I was able to get it to work right. I changed up the way the Buffer variable was being used. The NULL statement wasn't working with the ERROR_MORE_DATA part of the code. The way it is now, not only does it retrieve the value from the registry but it gives me a decipherable value.


At least all the code is in here in case anyone comes looking.

//******************************************************
//Internet Connection Status Process
//
HKEY hKey = 0;
LONG lResult;
DWORD Buffer = 0;
DWORD dwSize = sizeof(DWORD);


lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "system\\currentcontrolset\\services\\remoteaccess", 0, KEY_READ, &hKey);


if(lResult != ERROR_SUCCESS){
   // Error, do something useful
   return 0;
}


lResult = ::RegQueryValueEx(hKey, "Remote Connection", NULL, NULL, (LPBYTE) &Buffer, &dwSize);


if(lResult != ERROR_SUCCESS){
   // Unexpected error, do something useful
   RegCloseKey(hKey);
   return 0;
}


RegCloseKey(hKey);



//Is there an internet connection?
if (Buffer)isInternet = 1;
//******************************************************
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.