Solved

Reading a BYTE from registry

Posted on 2002-06-14
13
261 Views
Last Modified: 2010-04-01
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.

0
Comment
Question by:microboard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7078881
Hmm, can't find that one in my registry - what data type is it?
0
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 7079064
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
 
LVL 32

Expert Comment

by:jhance
ID: 7079068
Sorry, I forgot to put in the 2), but the example has it all there...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:microboard
ID: 7079210
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
 
LVL 32

Expert Comment

by:jhance
ID: 7079554
What error codes DOES it return?

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


0
 

Author Comment

by:microboard
ID: 7079689
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
 

Author Comment

by:microboard
ID: 7080059
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
 
LVL 32

Expert Comment

by:jhance
ID: 7080341
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
 

Author Comment

by:microboard
ID: 7080502
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
 
LVL 32

Expert Comment

by:jhance
ID: 7080514
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
 
LVL 32

Expert Comment

by:jhance
ID: 7080820
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
 

Expert Comment

by:weicco
ID: 7083623
"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
 

Author Comment

by:microboard
ID: 7084644
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question