Knox
Knox

Reputation: 1361

RegQueryValueEx giving strange data

DWORD dwType = REG_SZ;
TCHAR keyData[1024];
DWORD keyDataLength = 1024;
cchValue = MAX_VALUE_NAME; 
achValue[0] = '\0';     
HKEY currentKey;
long err = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), NULL, KEY_READ, &currentKey);
_tprintf(TEXT("OpenKey Error Code: %d\n"), err);
err = RegQueryValueEx(currentKey, TEXT("STEAM"), NULL, NULL, (LPBYTE)&keyData, &keyDataLength);
_tprintf(TEXT("QueryKey Error Code: %d\n"), err);
_tprintf(TEXT("Data: %d\n"), keyData);

The code above produces the following result:

OpenKey Error Code: 0
QueryKey Error Code: 0
Data: 15332432

How can I get the keyData to display the proper result? I've looked online for quite a while and this is almost a duplicate of working examples I found online... It is set for UNICODE and I do not want to switch the charset.

Upvotes: 0

Views: 1032

Answers (1)

taocp
taocp

Reputation: 23654

keyData is an array of TCHAR. try to print the char array with %s.

 _tprintf(TEXT("Data: %s\n"), keyData);
                    //^^^ not %d

EDIT Thanks @Windows programmer

In addition, keyDataLength is supposed to count bytes, so it should be either 1024 * sizeof(TCHAR) or more simply sizeof(keyData).

Upvotes: 3

Related Questions