First off, you are passing in the wrong BufferSize
value to RegGetValueA()
. You are claiming that you have allocated 8KB of memory, but you have actually allocated only 2KB. If the Registry data exceeds 2KB, you will overflow your buffer and corrupt memory. NEVER lie about your buffer sizes.
Also, you are trying to print out a narrow ANSI string as-if it were a wide Unicode string, which it is not, so that is also wrong.
That being said, a REG_MULTI_SZ
value is a null-terminated list of multiple null-terminated strings. You are printing out only the 1st string in the list. You need to loop through the whole list until you encounter the final null terminator, eg:
void get_reg_value(HKEY handle, std::string value_to_read)
{
char Buffer[2048];
DWORD BufferSize = sizeof(Buffer);
LONG errorcode = RegGetValueA(handle, NULL, value_to_read.c_str(), RRF_RT_REG_MULTI_SZ, NULL, Buffer, &BufferSize);
if (errorcode == ERROR_SUCCESS) {
char *value = Buffer;
while (*value != '\0') {
printf("Value data: %s\n", value);
value += (strlen(value) + 1);
}
}
else {
printf("Error: %ld\n", errorcode);
}
}
Alternatively, if needed, consider using a dynamically allocated buffer, so can handle data values greater than 2KB:
void get_reg_value(HKEY handle, std::string value_to_read)
{
std::vector<char> Buffer(2048);
DWORD BufferSize = Buffer.size();
LONG errorcode;
do {
errorcode = RegGetValueA(handle, NULL, value_to_read.c_str(), RRF_RT_REG_MULTI_SZ, NULL, Buffer, &BufferSize);
if (errorcode != ERROR_MORE_DATA) break;
Buffer.resize(BufferSize);
}
while (true);
if (errorcode == ERROR_SUCCESS) {
char *value = Buffer.data();
while (*value != '\0') {
printf("Value data: %s\n", value);
value += (strlen(value) + 1);
}
}
else {
printf("Error: %ld\n", errorcode);
}
}