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;
26using Memory::MakeSharedPtr;
36RegistryKey::RegistryKey (HKEY parentKey,
const String& path, REGSAM samDesired)
37 : fKey_{OpenPath_ (parentKey, path, samDesired)}
46HKEY RegistryKey::OpenPath_ (HKEY parentKey,
const String& path, REGSAM samDesired)
48#if USE_NOISY_TRACE_IN_THIS_MODULE_
52 Require (parentKey !=
nullptr);
53 Require (parentKey != INVALID_HANDLE_VALUE);
54 Require (samDesired == KEY_READ);
57 Ensure (result !=
nullptr and result != INVALID_HANDLE_VALUE);
64 VariantValue ExtractValue_ (HKEY key,
const TCHAR* path,
bool missingReturnsEmpty)
67 DWORD dwCountInBytes = 0;
68 LONG lResult = ::RegQueryValueExW (key, path,
nullptr, &dwType,
nullptr, &dwCountInBytes);
69 if (lResult == ERROR_SUCCESS) {
74 if (dwCountInBytes != 0) {
75 Assert (dwCountInBytes %
sizeof (
wchar_t) == 0);
76 size_t nChars = dwCountInBytes / 2 - 1;
77 strValue.resize (nChars);
78 ThrowIfNotERROR_SUCCESS (::RegQueryValueExW (key, path,
nullptr, &dwType, (LPBYTE) & (*strValue.begin ()), &dwCountInBytes));
79 Assert (strValue[nChars] ==
'\0');
85 Assert (dwCountInBytes ==
sizeof (DWORD));
87 Assert (dwCountInBytes ==
sizeof (DWORD));
97 else if (lResult == ERROR_FILE_NOT_FOUND and missingReturnsEmpty) {
108#if USE_NOISY_TRACE_IN_THIS_MODULE_
113 using NTSTATUS = LONG;
114#ifndef STATUS_SUCCESS
115 const auto STATUS_SUCCESS{((NTSTATUS)0x00000000L)};
117#ifndef STATUS_BUFFER_TOO_SMALL
118 constexpr auto STATUS_BUFFER_TOO_SMALL{(NTSTATUS)0xC0000023L};
120 std::wstring keyPath;
123 using NtQueryKeyType = DWORD (__stdcall*) (HANDLE KeyHandle,
int KeyInformationClass, PVOID KeyInformation, ULONG Length, PULONG ResultLength);
124 NtQueryKeyType func =
reinterpret_cast<NtQueryKeyType
> (dll.GetProcAddress (
"NtQueryKey"));
126 DWORD result = func (fKey_, 3, 0, 0, &size);
127 if (result == STATUS_BUFFER_TOO_SMALL) {
129 wchar_t* buffer =
new (std::nothrow)
wchar_t[size /
sizeof (
wchar_t)];
130 if (buffer != NULL) {
131 result = func (fKey_, 3, buffer, size, &size);
132 if (result == STATUS_SUCCESS) {
133 buffer[size /
sizeof (wchar_t)] = L
'\0';
134 keyPath = std::wstring (buffer + 2);
145 Require (fKey_ != INVALID_HANDLE_VALUE);
149 size_t lastBackSlash = valuePath.
rfind (
'\\');
150 if (lastBackSlash != SDKString::npos) {
156 catch (
const system_error& e) {
158 if (e.code () == errc::no_such_file_or_directory) {
165 return ExtractValue_ (fKey_, valuePath.
As<wstring> ().c_str (),
true);
170 Require (fKey_ != INVALID_HANDLE_VALUE);
177 auto myContext = MakeSharedPtr<Context_> ();
178 myContext->fParentKey = fKey_;
179 auto getNext = [myContext] () -> optional<shared_ptr<RegistryKey>> {
181 DWORD cbName =
static_cast<DWORD
> (achKeyBuf.size ());
183 auto retCode = ::RegEnumKeyEx (myContext->fParentKey, myContext->fCurIndex, achKeyBuf.begin (), &cbName,
nullptr,
nullptr,
nullptr,
nullptr);
184 if (retCode == ERROR_NO_MORE_ITEMS) {
187 if (retCode == ERROR_MORE_DATA) {
188 achKeyBuf.GrowToSize (achKeyBuf.size () * 2);
192 myContext->fCurIndex++;
193 Assert (cbName <= achKeyBuf.size ());
194 achKeyBuf.resize (cbName);
195#if USE_NOISY_TRACE_IN_THIS_MODULE_
196 DbgTrace (
"returning next child key: {}"_f, achKeyBuf);
200 return Traversal::CreateGenerator<shared_ptr<RegistryKey>> (getNext);
206 for (
int i = 0;; ++i) {
208 DWORD cbName =
static_cast<DWORD
> (achKeyBuf.size ());
210 auto retCode = ::RegEnumValue (fKey_, i, achKeyBuf.begin (), &cbName,
nullptr,
nullptr,
nullptr,
nullptr);
211 if (retCode == ERROR_NO_MORE_ITEMS) {
214 if (retCode == ERROR_MORE_DATA) {
215 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.
span< CHAR_T, EXTENT > AdjustNulTerminatedStringSpan(span< CHAR_T, EXTENT > s)
often apply this adjustment to return the non-nul string prefix from a span of characters.
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...