4#include "Stroika/Foundation/StroikaPreComp.h"
6#if qStroika_Foundation_Common_Platform_Windows
9#error "WINDOWS REQUIRED FOR THIS MODULE"
12#include "Stroika/Foundation/Execution/DLLSupport.h"
13#include "Stroika/Foundation/Execution/Platform/Windows/Exception.h"
21using namespace Stroika::Foundation::Common::Platform::Windows;
23using namespace Stroika::Foundation::Debug;
34RegistryKey::RegistryKey (HKEY parentKey,
const String& path, REGSAM samDesired)
35 : fKey_{OpenPath_ (parentKey, path, samDesired)}
44HKEY RegistryKey::OpenPath_ (HKEY parentKey,
const String& path, REGSAM samDesired)
46#if USE_NOISY_TRACE_IN_THIS_MODULE_
50 Require (parentKey !=
nullptr);
51 Require (parentKey != INVALID_HANDLE_VALUE);
52 Require (samDesired == KEY_READ);
55 Ensure (result !=
nullptr and result != INVALID_HANDLE_VALUE);
62 VariantValue ExtractValue_ (HKEY key,
const TCHAR* path,
bool missingReturnsEmpty)
65 DWORD dwCountInBytes = 0;
66 LONG lResult = ::RegQueryValueExW (key, path,
nullptr, &dwType,
nullptr, &dwCountInBytes);
67 if (lResult == ERROR_SUCCESS) {
72 if (dwCountInBytes != 0) {
73 Assert (dwCountInBytes %
sizeof (
wchar_t) == 0);
74 size_t nChars = dwCountInBytes / 2 - 1;
75 strValue.resize (nChars);
76 ThrowIfNotERROR_SUCCESS (::RegQueryValueExW (key, path,
nullptr, &dwType, (LPBYTE) & (*strValue.begin ()), &dwCountInBytes));
77 Assert (strValue[nChars] ==
'\0');
83 Assert (dwCountInBytes ==
sizeof (DWORD));
85 Assert (dwCountInBytes ==
sizeof (DWORD));
95 else if (lResult == ERROR_FILE_NOT_FOUND and missingReturnsEmpty) {
106#if USE_NOISY_TRACE_IN_THIS_MODULE_
111 using NTSTATUS = LONG;
112#ifndef STATUS_SUCCESS
113 const auto STATUS_SUCCESS{((NTSTATUS)0x00000000L)};
115#ifndef STATUS_BUFFER_TOO_SMALL
116 constexpr auto STATUS_BUFFER_TOO_SMALL{(NTSTATUS)0xC0000023L};
118 std::wstring keyPath;
121 using NtQueryKeyType = DWORD (__stdcall*) (HANDLE KeyHandle,
int KeyInformationClass, PVOID KeyInformation, ULONG Length, PULONG ResultLength);
122 NtQueryKeyType func =
reinterpret_cast<NtQueryKeyType
> (dll.GetProcAddress (
"NtQueryKey"));
124 DWORD result = func (fKey_, 3, 0, 0, &size);
125 if (result == STATUS_BUFFER_TOO_SMALL) {
127 wchar_t* buffer =
new (std::nothrow)
wchar_t[size /
sizeof (
wchar_t)];
128 if (buffer != NULL) {
129 result = func (fKey_, 3, buffer, size, &size);
130 if (result == STATUS_SUCCESS) {
131 buffer[size /
sizeof (wchar_t)] = L
'\0';
132 keyPath = std::wstring (buffer + 2);
143 Require (fKey_ != INVALID_HANDLE_VALUE);
147 size_t lastBackSlash = valuePath.
rfind (
'\\');
148 if (lastBackSlash != SDKString::npos) {
154 catch (
const system_error& e) {
156 if (e.code () == errc::no_such_file_or_directory) {
163 return ExtractValue_ (fKey_, valuePath.
As<wstring> ().c_str (),
true);
168 Require (fKey_ != INVALID_HANDLE_VALUE);
175 auto myContext = make_shared<Context_> ();
176 myContext->fParentKey = fKey_;
177 auto getNext = [myContext] () -> optional<shared_ptr<RegistryKey>> {
179 DWORD cbName =
static_cast<DWORD
> (achKeyBuf.size ());
181 auto retCode = ::RegEnumKeyEx (myContext->fParentKey, myContext->fCurIndex, achKeyBuf.begin (), &cbName,
nullptr,
nullptr,
nullptr,
nullptr);
182 if (retCode == ERROR_NO_MORE_ITEMS) {
185 if (retCode == ERROR_MORE_DATA) {
186 achKeyBuf.GrowToSize (achKeyBuf.size () * 2);
190 myContext->fCurIndex++;
191 Assert (cbName <= achKeyBuf.size ());
192 achKeyBuf.resize (cbName);
193#if USE_NOISY_TRACE_IN_THIS_MODULE_
194 DbgTrace (
"returning next child key: {}"_f, achKeyBuf);
198 return Traversal::CreateGenerator<shared_ptr<RegistryKey>> (getNext);
204 for (
int i = 0;; ++i) {
206 DWORD cbName =
static_cast<DWORD
> (achKeyBuf.size ());
208 auto retCode = ::RegEnumValue (fKey_, i, achKeyBuf.begin (), &cbName,
nullptr,
nullptr,
nullptr,
nullptr);
209 if (retCode == ERROR_NO_MORE_ITEMS) {
212 if (retCode == ERROR_MORE_DATA) {
213 achKeyBuf.GrowToSize (achKeyBuf.size () * 2);
#define WeakAssert(c)
A WeakAssert() is for things that aren't guaranteed to be true, but are overwhelmingly likely to be t...
#define Stroika_Foundation_Debug_OptionalizeTraceArgs(...)
String is like std::u32string, except it is much easier to use, often much more space efficient,...
static String FromSDKString(const SDKChar *from)
nonvirtual size_t rfind(Character c) const
nonvirtual SDKString AsSDKString() const
nonvirtual String substr(size_t from, size_t count=npos) const
nonvirtual bool Add(ArgByValueType< key_type > key, ArgByValueType< mapped_type > newElt, AddReplaceMode addReplaceMode=AddReplaceMode::eAddReplaces)
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
Exception<> is a replacement (subclass) for any std c++ exception class (e.g. the default 'std::excep...
Logically halfway between std::array and std::vector; Smart 'direct memory array' - which when needed...
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...