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");
}