Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Registry.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Common_Platform_Windows_Registry_h_
5#define _Stroika_Foundation_Common_Platform_Windows_Registry_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#if qStroika_Foundation_Common_Platform_Windows
10#include <Windows.h>
11#else
12#error "WINDOWS REQUIRED FOR THIS MODULE"
13#endif
14
16#include "Stroika/Foundation/Common/Common.h"
18
19namespace Stroika::Foundation::Common::Platform::Windows {
20
21 /**
22 * Simple utility class to allow (read-only) access to the windows registry.
23 *
24 * I considered making this support modification, but as JSON configuration files seem a better path than using
25 * the registry, and this is mainly for backward compatibility with older systems, I chose to keep this read-only.
26 *
27 * \par Example Usage
28 * \code
29 * if (auto tmp = RegistryKey{HKEY_CLASSES_ROOT}.Lookup (Format (L"MIME\\Database\\Content Type\\%s\\Extension", ct.c_str ()))) {
30 * return tmp.As<String> ();
31 * }
32 * \endcode
33 *
34 * \note It would be convenient to be able to retrieve the parent key and name for this given HKEY,
35 * but this doesn't appear well supported (but possible):
36 * https://stackoverflow.com/questions/937044/determine-path-to-registry-key-from-hkey-handle-in-c
37 */
39 public:
40 /**
41 * Require HKEY argument (parent or actual) to be != null, and != INVALID_HANDLE_VALUE
42 *
43 * If the KEY/String overload is used, and the key cannot be opened, and exception is raised: you can
44 * never get an invalid RegistryKey object.
45 *
46 * The overload taking 'owned' argument determines if the key is destroyed at the end of ownership.
47 *
48 * 'samDesired' must be KEY_READ or some other permission that doesn't cause creation.
49 */
50 constexpr RegistryKey (HKEY hkey, bool owned = false);
51 RegistryKey (HKEY parentKey, const Characters::String& path, REGSAM samDesired = KEY_READ);
52 RegistryKey () = delete;
53 RegistryKey (const RegistryKey&) = delete;
54
55 public:
56 /**
57 */
58 const RegistryKey& operator= (const RegistryKey&) const = delete;
59
60 public:
61 /**
62 */
63 ~RegistryKey ();
64
65 public:
66 /**
67 */
68 nonvirtual operator HKEY () const;
69
70 public:
71 /**
72 * Returns \-delimited string, for example:
73 * L"\\REGISTRY\\USER\\S-1-5-21-1083356233-3619400068-4035759904-1001_Classes\\*"
74 */
75 nonvirtual Characters::String GetFullPathOfKey () const;
76
77 public:
78 /**
79 * Returns empty object (null) if missing. Can also return String type, or numeric for DWORDs.
80 *
81 * \note @todo - should return empty VariantValue if detectably missing, but for permissions issues, raise exception
82 *
83 * \note Unlike the underlying Windows registry APIs, you can pass in a prefName with backslashes, and it will be
84 * interpreted as lookup relative to 'this' registry key
85 */
86 nonvirtual DataExchange::VariantValue Lookup (const Characters::String& valuePath) const;
87
88 public:
89 /**
90 */
91 nonvirtual Traversal::Iterable<shared_ptr<RegistryKey>> EnumerateSubKeys () const;
92
93 public:
94 /**
95 */
97
98 private:
99 static HKEY OpenPath_ (HKEY parentKey, const Characters::String& path, REGSAM samDesired);
100
101 private:
102 bool fOwned_;
103 HKEY fKey_;
104 };
105
106}
107
108/*
109 ********************************************************************************
110 ***************************** Implementation Details ***************************
111 ********************************************************************************
112 */
113#include "Registry.inl"
114
115#endif /*_Stroika_Foundation_Common_Platform_Windows_Registry_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual DataExchange::VariantValue Lookup(const Characters::String &valuePath) const
Definition Registry.cpp:141
nonvirtual Characters::String GetFullPathOfKey() const
Definition Registry.cpp:104
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237