So, I made this function to return data from the registry. I'm going to be calling different things from different places to get hardware information. Unfortunately, it works the second time you call it. Here is the output:

Could not find value: Identifier

Returned: ??

Returned: x86 Family 6 Model 14 Stepping 8

Returned: x86 Family 6 Model 14 Stepping 8

Press any key to continue . . .

It's probably something simple, but why is it doing this?

#include <windows.h>
#include <iostream>
#include <sstream>
using namespace std;

std::string getreg(LPCTSTR regPath, LPCTSTR regReq) {
    HKEY keyHandle;
    char rgValue[1024];
    DWORD dwValue;
    DWORD size1;
    DWORD Type;
    std::ostringstream oss;
    
    oss << "";
     
    if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, regPath, 0, KEY_QUERY_VALUE, &keyHandle) == ERROR_SUCCESS) {
        if (RegQueryValueEx( keyHandle, regReq, NULL, &Type, (LPBYTE)rgValue,&size1) == ERROR_SUCCESS) { 
           RegQueryValueEx( keyHandle, regReq, NULL, &Type, (LPBYTE)rgValue,&size1);
           switch (Type) {
               case 1:
                  oss << rgValue;
                  break;
               case 4:
                  dwValue = *((DWORD*)rgValue);
                  oss << dwValue;
                  break;
               default:
                  std::cout << "Returned type is not supported.\n\n";
                  break;
            }              
        } else {
           std::cout << "Could not find value: " << regReq << "\n\n";
        }
    } else {
       std::cout << "Could not find key: " << regPath << "\n\n";
    }
    RegCloseKey(keyHandle);
    return oss.str();
}

int main () {
   
   LPCTSTR regPath;
   LPCTSTR regReq;
   std::string out;
   
   regPath = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
   regReq = "Identifier";
   out = getreg(regPath, regReq);
   
   if (out.length() > 0) {
      std::cout << "Returned: " << out << "\n\n";
   } else {
      std::cout << "Returned: ?? \n\n";
   }
   
   regPath = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
   regReq = "Identifier";
   out = getreg(regPath, regReq);
   
   if (out.length() > 0) {
      std::cout << "Returned: " << out << "\n\n";
   } else {
      std::cout << "Returned: ?? \n\n";
   }
   
   regPath = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
   regReq = "Identifier";
   out = getreg(regPath, regReq);
   
   if (out.length() > 0) {
      std::cout << "Returned: " << out << "\n\n";
   } else {
      std::cout << "Returned: ?? \n\n";
   }
   
   system("Pause"); 
}

That is weird behaviour, see what GetLastError() tells you.

I changed the code to the following:

std::cout << "Could not find value: " << regReq << "\nError: " << GetLastError() << "\n\n";

I'm getting Error: 0, which I interpret as being null ... so, I'm guessing it's not throwing an error.

Thinking of it, I'm not 100% sure whether the Reg* routines set the error state (returned by GetLastError() ). So, modify the code so that you get the return value from these Reg* calls. That would be along the lines of ..

...
LONG lResult = RegQueryValueEx(...);
if(lResult == ERROR_SUCCESS)
{
  lResult = RegQueryValueEx(...);
   ...

If the return value is non-zero, it will be a system error code.

It's throwing #234 - More data is available.
I have no idea what it means by this.

It's throwing #234 - More data is available.

Alright, this is progress.

See RegQueryValueEx documentation about its last argument ( LPDWORD lpcbData ).

Since you are passing in the buffer rgValue , the size1 needs to be initialized to the sizeof(rgValue) before calling RegQueryValueEx() .

Works now! I appreciate the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.