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