forked from zodiacon/TotalRegistry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumStrings.cpp
89 lines (76 loc) · 2.08 KB
/
EnumStrings.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "pch.h"
#include "EnumStrings.h"
#include "Registry.h"
void CEnumStrings::SetRegistryPath(const CString& path) {
_path = path;
}
bool CEnumStrings::GenerateStrings(const CString& path) {
_strings.clear();
_current = 0;
if (path.IsEmpty()) {
for (auto& key : Registry::Keys) {
_strings.push_back(key.text);
}
return true;
}
if (path == L"\\") {
_strings.push_back(L"\\REGISTRY");
return true;
}
auto key = Registry::OpenKey(path, KEY_READ);
if (!key)
return false;
Registry::EnumSubKeys(key.Get(), [&](auto name, const auto&) {
_strings.push_back(path + name);
return TRUE;
});
return true;
}
HRESULT __stdcall CEnumStrings::Next(ULONG celt, LPOLESTR* rgelt, ULONG* pceltFetched) {
if (_strings.empty()) {
if(pceltFetched)
*pceltFetched = 0;
// generate strings
if (!GenerateStrings(_path))
return E_FAIL;
}
if (celt == 0)
return S_OK;
ULONG i = 0;
for (; i < celt; i++) {
auto index = _current + i;
if (index >= _strings.size())
break;
CString& str(_strings[index]);
rgelt[i] = (PWSTR)::CoTaskMemAlloc((str.GetLength() + 1) * sizeof(WCHAR));
wcscpy_s(rgelt[i], str.GetLength() + 1, str);
ATLTRACE(L"Added string: %s\n", rgelt[i]);
}
if (pceltFetched)
*pceltFetched = i;
_current += i;
return i < celt ? S_FALSE : S_OK;
}
HRESULT __stdcall CEnumStrings::Skip(ULONG celt) {
_current += celt;
return S_OK;
}
HRESULT __stdcall CEnumStrings::Reset(void) {
_current = 0;
return S_OK;
}
HRESULT __stdcall CEnumStrings::Clone(IEnumString** ppenum) {
CComObject<CEnumStrings>* p;
auto hr = p->CreateInstance(&p);
if (FAILED(hr))
return hr;
p->SetRegistryPath(_path);
p->_current = _current;
p->_strings = _strings;
return p->QueryInterface(ppenum);
}
HRESULT __stdcall CEnumStrings::Expand(PCWSTR pszExpand) {
_path = pszExpand;
_strings.clear();
return S_OK;
}