Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Registry.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#if qStroika_Foundation_Common_Platform_Windows
7#include <Windows.h>
8#else
9#error "WINDOWS REQUIRED FOR THIS MODULE"
10#endif
11
12#include "Stroika/Foundation/Execution/DLLSupport.h"
13#include "Stroika/Foundation/Execution/Platform/Windows/Exception.h"
14
16
17#include "Registry.h"
18
19using namespace Stroika::Foundation;
21using namespace Stroika::Foundation::Common::Platform::Windows;
23using namespace Stroika::Foundation::Debug;
25
26// Comment this in to turn on aggressive noisy DbgTrace in this module
27//#define USE_NOISY_TRACE_IN_THIS_MODULE_ 1
28
29/*
30 ********************************************************************************
31 ********************************* RegistryKey **********************************
32 ********************************************************************************
33 */
34RegistryKey::RegistryKey (HKEY parentKey, const String& path, REGSAM samDesired)
35 : fKey_{OpenPath_ (parentKey, path, samDesired)}
36 , fOwned_{true}
37{
38}
39
40/*
41 * Walk the given path (in segments) - and make sure each exists, and create each segment if it doesn't
42 * exist.Finally - do a regular registry open with access permissions 'samDesired'.< / p>
43 */
44HKEY RegistryKey::OpenPath_ (HKEY parentKey, const String& path, REGSAM samDesired)
45{
46#if USE_NOISY_TRACE_IN_THIS_MODULE_
48 Stroika_Foundation_Debug_OptionalizeTraceArgs (L"{}::RegistryKey::OpenPath_", L"parentKey={}, path='{}'"_f, parentKey, path)};
49#endif
50 Require (parentKey != nullptr);
51 Require (parentKey != INVALID_HANDLE_VALUE);
52 Require (samDesired == KEY_READ); // @todo - for now - later allow others so long as they are non-destructive/readonly
53 HKEY result{};
54 ThrowIfNotERROR_SUCCESS (::RegOpenKeyEx (parentKey, path.AsSDKString ().c_str (), 0, samDesired, &result));
55 Ensure (result != nullptr and result != INVALID_HANDLE_VALUE);
56 return result;
57}
58
59namespace {
60 // missingReturnsEmpty flag because I happen to have defined the semantics for Lookup () that way, and its reasonable
61 // so lets not change now... --LGP 2020-07-05
62 VariantValue ExtractValue_ (HKEY key, const TCHAR* path, bool missingReturnsEmpty)
63 {
64 DWORD dwType = 0;
65 DWORD dwCountInBytes = 0;
66 LONG lResult = ::RegQueryValueExW (key, path, nullptr, &dwType, nullptr, &dwCountInBytes);
67 if (lResult == ERROR_SUCCESS) {
68 switch (dwType) {
69 case REG_SZ:
70 case REG_EXPAND_SZ: {
71 wstring strValue;
72 if (dwCountInBytes != 0) {
73 Assert (dwCountInBytes % sizeof (wchar_t) == 0); // @todo - we should bullet proof this code more but for now, assert if an issue
74 size_t nChars = dwCountInBytes / 2 - 1; // includes NUL-byte
75 strValue.resize (nChars);
76 ThrowIfNotERROR_SUCCESS (::RegQueryValueExW (key, path, nullptr, &dwType, (LPBYTE) & (*strValue.begin ()), &dwCountInBytes));
77 Assert (strValue[nChars] == '\0');
78 }
79 return VariantValue{strValue};
80 } break;
81 case REG_DWORD: {
82 DWORD result{};
83 Assert (dwCountInBytes == sizeof (DWORD));
84 ThrowIfNotERROR_SUCCESS (::RegQueryValueExW (key, path, nullptr, &dwType, (LPBYTE)&result, &dwCountInBytes));
85 Assert (dwCountInBytes == sizeof (DWORD));
86 return VariantValue{result};
87 } break;
88 default: {
89 WeakAssert (false); // NYI
90 Execution::Throw (Execution::Exception<> ("Unsupported registry format"sv));
91 } break;
92 }
93 }
94 // Just treat this as an error and revisit later if we get issues and maybe change / document behavior for Lookup()
95 else if (lResult == ERROR_FILE_NOT_FOUND and missingReturnsEmpty) {
96 return VariantValue{}; // @todo reconsider if we should throw here or not??? -- LGP 2020-07-04
97 }
98 else {
99 Execution::Throw (Execution::SystemErrorException<> (lResult, system_category ()));
100 }
101 }
102}
103
105{
106#if USE_NOISY_TRACE_IN_THIS_MODULE_
107 Debug::TraceContextBumper trcCtx{Stroika_Foundation_Debug_OptionalizeTraceArgs ("{}::RegistryKey::GetFullPathOfKey")};
108#endif
109 // Based on https://stackoverflow.com/questions/937044/determine-path-to-registry-key-from-hkey-handle-in-c
110
111 using NTSTATUS = LONG;
112#ifndef STATUS_SUCCESS
113 const auto STATUS_SUCCESS{((NTSTATUS)0x00000000L)};
114#endif
115#ifndef STATUS_BUFFER_TOO_SMALL
116 constexpr auto STATUS_BUFFER_TOO_SMALL{(NTSTATUS)0xC0000023L};
117#endif
118 std::wstring keyPath;
119 if (fKey_ != NULL) {
120 Execution::DLLLoader dll{L"ntdll.dll"};
121 using NtQueryKeyType = DWORD (__stdcall*) (HANDLE KeyHandle, int KeyInformationClass, PVOID KeyInformation, ULONG Length, PULONG ResultLength);
122 NtQueryKeyType func = reinterpret_cast<NtQueryKeyType> (dll.GetProcAddress ("NtQueryKey"));
123 DWORD size{0};
124 DWORD result = func (fKey_, 3, 0, 0, &size);
125 if (result == STATUS_BUFFER_TOO_SMALL) {
126 size = size + 2;
127 wchar_t* buffer = new (std::nothrow) wchar_t[size / sizeof (wchar_t)]; // size is in bytes
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);
133 }
134 delete[] buffer;
135 }
136 }
137 }
138 return keyPath;
139}
140
142{
143 Require (fKey_ != INVALID_HANDLE_VALUE);
144 {
145 // RegQueryValueExW doesn't support this directly, but its quite handy, and we document we support this
146 // (allowing path syntax in the valuePath to lookup)
147 size_t lastBackSlash = valuePath.rfind ('\\');
148 if (lastBackSlash != SDKString::npos) {
149 // @todo - check on TYPE of exception and if cuz not there, return empty, and if cuz of permissions (etc)
150 // pass along exception (note https://docs.microsoft.com/en-us/windows/desktop/api/winreg/nf-winreg-regopenkeyexa doesnt document returned error codes for not found)
151 try {
152 return RegistryKey{fKey_, valuePath.substr (0, lastBackSlash)}.Lookup (valuePath.substr (lastBackSlash + 1));
153 }
154 catch (const system_error& e) {
155 // catch/translate because the part not found could be in the PATH and then RegistryKey would throw
156 if (e.code () == errc::no_such_file_or_directory) { // windows error ERROR_FILE_NOT_FOUND
157 return VariantValue{};
158 }
160 }
161 }
162 }
163 return ExtractValue_ (fKey_, valuePath.As<wstring> ().c_str (), true);
164}
165
166Traversal::Iterable<shared_ptr<RegistryKey>> RegistryKey::EnumerateSubKeys () const
167{
168 Require (fKey_ != INVALID_HANDLE_VALUE);
169
170 // Use a generator to avoid keeping tons of registry key objects in memory at the same time (in case this is a limited resource)
171 struct Context_ {
172 HKEY fParentKey;
173 int fCurIndex{0};
174 };
175 auto myContext = make_shared<Context_> ();
176 myContext->fParentKey = fKey_;
177 auto getNext = [myContext] () -> optional<shared_ptr<RegistryKey>> {
178 Memory::StackBuffer<TCHAR> achKeyBuf{Memory::eUninitialized, 1024};
179 DWORD cbName = static_cast<DWORD> (achKeyBuf.size ()); // size of name string
180 retry:
181 auto retCode = ::RegEnumKeyEx (myContext->fParentKey, myContext->fCurIndex, achKeyBuf.begin (), &cbName, nullptr, nullptr, nullptr, nullptr);
182 if (retCode == ERROR_NO_MORE_ITEMS) {
183 return nullopt; // done
184 }
185 if (retCode == ERROR_MORE_DATA) {
186 achKeyBuf.GrowToSize (achKeyBuf.size () * 2);
187 goto retry;
188 }
189 ThrowIfNotERROR_SUCCESS (retCode);
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);
195#endif
196 return make_shared<RegistryKey> (myContext->fParentKey, String::FromSDKString (achKeyBuf));
197 };
198 return Traversal::CreateGenerator<shared_ptr<RegistryKey>> (getNext);
199}
200
202{
204 for (int i = 0;; ++i) {
205 Memory::StackBuffer<TCHAR> achKeyBuf{Memory::eUninitialized, 1024};
206 DWORD cbName = static_cast<DWORD> (achKeyBuf.size ()); // size of name string
207 retry:
208 auto retCode = ::RegEnumValue (fKey_, i, achKeyBuf.begin (), &cbName, nullptr, nullptr, nullptr, nullptr);
209 if (retCode == ERROR_NO_MORE_ITEMS) {
210 break;
211 }
212 if (retCode == ERROR_MORE_DATA) {
213 achKeyBuf.GrowToSize (achKeyBuf.size () * 2);
214 goto retry;
215 }
216 ThrowIfNotERROR_SUCCESS (retCode);
217 result.Add (String::FromSDKString (achKeyBuf), ExtractValue_ (fKey_, achKeyBuf.begin (), false));
218 }
219 return result;
220}
#define WeakAssert(c)
A WeakAssert() is for things that aren't guaranteed to be true, but are overwhelmingly likely to be t...
Definition Assertions.h:438
#define DbgTrace
Definition Trace.h:309
#define Stroika_Foundation_Debug_OptionalizeTraceArgs(...)
Definition Trace.h:270
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
static String FromSDKString(const SDKChar *from)
Definition String.inl:447
nonvirtual size_t rfind(Character c) const
Definition String.inl:1069
nonvirtual SDKString AsSDKString() const
Definition String.inl:802
nonvirtual String substr(size_t from, size_t count=npos) const
Definition String.inl:1086
nonvirtual DataExchange::VariantValue Lookup(const Characters::String &valuePath) const
Definition Registry.cpp:141
nonvirtual Characters::String GetFullPathOfKey() const
Definition Registry.cpp:104
nonvirtual bool Add(ArgByValueType< key_type > key, ArgByValueType< mapped_type > newElt, AddReplaceMode addReplaceMode=AddReplaceMode::eAddReplaces)
Definition Mapping.inl:190
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...
Definition Exceptions.h:157
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.
Definition Iterable.h:237
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43