MAA
MAA

Reputation: 93

RegQueryValueEx REG_SZ [C++]

which datatype should the variable that recives the data from the Registery have?

HKEY hKey;
HKEY hKey2;

DWORD dwMHz = MAX_PATH;
string pName;


long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
        0,
        KEY_READ,
        &hKey);
long lError2 = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
        0,
        KEY_READ,
        &hKey2);

// Working
RegQueryValueEx(hKey, "~MHz", NULL, NULL, (LPBYTE) &dwMHz, &BufSize);

//Not working                                            <-- THIS -->
RegQueryValueEx(hKey2, "ProcessorNameInfo", NULL, NULL, (LPBYTE) &pName, &BufSize2);


cout << "   Processor frequency: " << dwMHz / 1024 << " GHz" << endl;
cout << "   Processor Name: " << pName << endl;

RegCloseKey(hKey);
RegCloseKey(hKey2);

When I try to print the 'pName' I get nothing.

Upvotes: 0

Views: 5411

Answers (1)

Bukes
Bukes

Reputation: 3708

aYou'll want to read this data into an array of type TCHAR, which depending on how your application is built (UNICODE/MBCS) will be properly typed as char or wchar_t.

When reading REG_SZ strings using this API, you need to make sure of 2 things.

1) Your output buffer needs to have space for any trailing NULL terminator 2) On success, your output buffer MIGHT NOT BE NULL TERMINATED.

That last bit is important - if the string was not stored with a NULL terminator, then your output buffer won't be either. You need to watch for this and handle this properly, lest you end up with a security vulnerability in your application.

Upvotes: 3

Related Questions