RegGetValue returns 1 character.

Hi Guys!!!

I have a bit of an issue where the RegGetValue is returning 1 character of the registry keys contents. Any ideas on what I am doing wrong?

1
2
3
4
5
6
7
8
9
10
#define BUFFER 8192

int main()
{
	char value[255];
	DWORD BufferSize = BUFFER;
	RegGetValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "SystemRoot", RRF_RT_ANY, NULL, (PVOID)&value, &BufferSize);
	std::cout << value << std::endl;
	system("pause");
}


Thanks for any and all assistance!!

Googie.
Strings in the Windows registry are encoded in UTF-16, thus you should be passing a pointer to a wchar_t array. Also, the value of BufferSize should match the actual size of the buffer in bytes.
1
2
3
4
wchar_t value[255];
DWORD BufferSize = sizeof(value);
//...
std::wcout << value << std::endl;
https://stackoverflow.com/questions/6308426/reggetvalue-question

You are passing a char array of 255 elements but telling the function call you are passing an array of 8192 elements.

Ooops! Buffer Overflow!
Thanks for your fast reply guys! It works now!!

I have one last question with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Reg() {
    wchar_t value[255];
    DWORD BufferSize = sizeof(value);
 
        RegGetValue(HKEY_LOCAL_MACHINE, L"registry path here", L"TEST", RRF_RT_ANY, NULL, (PVOID)&value, &BufferSize);
       std::wcout << value << std::endl;

 char* ascii = new char[wcslen(value) + 1];
wcstombs(ascii, value, wcslen(value));

printf(ascii);

        system("pause");
	}


Now when I get the results from "printf(ascii);" the results are correct but it is followed up with jumbled data after the desired result. The console looks like this:

DOUBLE
DOUBLE═²²²²▌▌▌▌▌≥Ao▌Press any key to continue . . .

Any ideas on what I'm doing to get this result?

Many Thanks,

Googie.
Yet another case of not nul-terminating the strings.
https://www.cplusplus.com/reference/cstdlib/wcstombs/

You need to pass wcslen(value)+1 (ie, the real size of your buffer).

> printf(ascii);
Don't do this.
Use https://www.cplusplus.com/reference/cstdio/puts/
puts(ascii);
or
printf("%s",ascii);

See https://www.geeksforgeeks.org/format-string-vulnerability-and-prevention-with-example/
Topic archived. No new replies allowed.