What im doing wrong this time? The following code always returns 4 bytes only, instead of the whole string:
HKEY hkey;
DWORD dwType, dwSize;
char keybuffer[512];
if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("software\\company name\\game name"), 0, KEY_READ, &hkey) == ERROR_SUCCESS){
dwType = REG_SZ;
dwSize = sizeof(keybuffer);
RegQueryValueEx(hkey, TEXT("setting"), NULL, &dwType, (PBYTE)&keybuffer, &dwSize);
RegCloseKey(hkey);
}
Even if i change dwSize to anything, it will still return 4 bytes.
Edit: Apparently there was no bug in above code, but somewhere else -_-
I see two more potential pitfalls here. First, as Francis mentioned, you should check the return value. Do the 4 bytes actually correspond to the string characters you expect? They might be anything. From the documentation:
The second potential pitfall is that you’re using a
chararray with a function that takesTCHARparameters. If you’re compiling for Unicode, the compiler will happily let you write a wide string to your narrow string buffer, due to the cast toPBYTE. It’s safer to either useTCHARconsistently or don’t use it at all (i.e. callRegQueryValueExAorRegQueryValueExW).