1,105,556 Community Members

C++, Registry, and the Garbage Within

Member Avatar
freemanirl
Newbie Poster
10 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Okay, so I've been looking at this for quite some time. I think I know what the problem is, but my C++ skills are sad a best due to stuff like Java, C# and VB. So, I'm trying to pull information from the registry, and I have no problem getting it to work. I can pull REG_SZ values just fine. I can output them and everything. Now, I'm trying to get REG_DWORDs to work, but am having issues. When I have it look at something that has a DWORD value of like 546258943, the program spits out something like " ?Å ╪·"☺t·"" Unfortunately, this is not my intended output. So, the way I see it, I've got this char buffer that I'm filling with this numerical data, which it's interpreting as a character array what it outputs it. So, my dilemma is to get it not to do that.

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

int main () {
    HKEY keyHandle;
    char rgValue [1024];

	LPCTSTR regPath = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
    LPCTSTR regReq = "FeatureSet";
 
    DWORD size1 = sizeof(rgValue);
    DWORD Type;
 
    if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, regPath,0, KEY_QUERY_VALUE, &keyHandle) == ERROR_SUCCESS) {
        RegQueryValueEx( keyHandle, regReq, NULL, &Type, (LPBYTE)rgValue,&size1); 
    } 
    RegCloseKey(keyHandle);

    std::cout << "Returned: -->" << rgValue << "<-- (at least I think so) \n\n";
    system("Pause");
}
Member Avatar
Banfa
Practically a Posting Shark
801 posts since Mar 2010
Reputation Points: 582 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 12 [?]
Featured
 
1
 

You shouldn't just output rgValue, it is binary data unless Type has a value of REG_EXPAND_SZ, REG_MULTI_SZ, REG_SZ.

If the value in the registry is really a DWORD then Type should be REG_DWORD and size1 should be 4 on exit. In that case something (non-portable) like DWORD dwValue = *((DWORD*)rgValue); should do the trick. Alternatively just pass a pointer to a DWORD into RegQueryValueEx in the first place if you are sure of the type the key holds.

RTFM: RegQueryValueEx

Member Avatar
freemanirl
Newbie Poster
10 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

“He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever”

Okay, that made a lot more sense. I appreciate your help!

Question Answered as of 3 Years Ago by Banfa
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: