Thanks, Microsoft

https://docs.microsoft.com/en-us/windows/desktop/api/winreg/nf-winreg-regenumkeyexa

Note to other documentation writers:
1. "TBD" does not constitute documentation.
2. Do not remove access to old versions of the documentation if you haven't finished the new version. Google cache does not count as "not removing access".
Is this page helpful?
YES NO


EDIT: Taken from code I wrote back in the 90s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool ntreg::Reg::Iterator::operator++(int)
{
    if (iteration_complete)
        return false;

    reinitialized        = false;
    DWORD   name_sz      = sizeof(name.size());
    DWORD   classname_sz = sizeof(classname.size());
    LONG    ret = ::RegEnumKeyEx(
                            reg.GetKey(),
                            cnt++,
                            name, &name_sz,
                            0,
                            classname, &classname_sz,
                           &last_write_time);
    if (ret != NO_ERROR)
    {
        iteration_complete = true;
        return false;
    }
    return true;
}
Last edited on
Oh, you can bet I left my feedback.

My version looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
std::vector<std::wstring> enum_subkeys(HKEY key){
	std::vector<std::wstring> ret;
	for (DWORD i = 0; ; i++){
		std::wstring name(256, 0);
		while (true){
			auto size = (DWORD)name.size();
			auto result = RegEnumKeyExW(key, i, &name[0], &size, nullptr, nullptr, nullptr, nullptr);
			if (result == ERROR_SUCCESS){
				name.resize(size);
				ret.emplace_back(std::move(name));
				break;
			}
			if (result == ERROR_NO_MORE_ITEMS)
				return ret;
			if (result == ERROR_MORE_DATA){
				name.resize(name.size() * 2);
				continue;
			}
			std::stringstream stream;
			stream << "RegEnumKeyW() failed with error ";
			error_to_stream(stream, result);
			throw std::runtime_error(stream.str());
		}
	}
	assert(false);
	return ret;
}
Last edited on
That'll do it :)

No one seems to use Class Name, but back in the day we weren't to know that.

Does the compiler warn about mixing DWORD and size_t?
Only if they're different types.
Topic archived. No new replies allowed.