Adapted from https://learn.microsoft.com/en-us/archive/msdn-magazine/2017/may/c-use-modern-c-to-access-the-windows-registry#reading-a-string-value-from-the-registry
| 103 | // Adapted from |
| 104 | // https://learn.microsoft.com/en-us/archive/msdn-magazine/2017/may/c-use-modern-c-to-access-the-windows-registry#reading-a-string-value-from-the-registry |
| 105 | std::optional<bool> RegGetBool(_In_ HKEY hKey, _In_ const std::wstring& subKey, _In_ const std::wstring& value) { |
| 106 | DWORD dataSize{}; |
| 107 | LONG retCode = RegGetValue( |
| 108 | hKey, |
| 109 | subKey.c_str(), |
| 110 | value.c_str(), |
| 111 | RRF_RT_REG_SZ, |
| 112 | nullptr, |
| 113 | nullptr, |
| 114 | &dataSize |
| 115 | ); |
| 116 | if (retCode != ERROR_SUCCESS) |
| 117 | return std::nullopt; |
| 118 | std::wstring data; |
| 119 | data.resize(dataSize / sizeof(wchar_t)); |
| 120 | retCode = RegGetValue( |
| 121 | hKey, |
| 122 | subKey.c_str(), |
| 123 | value.c_str(), |
| 124 | RRF_RT_REG_SZ, |
| 125 | nullptr, |
| 126 | data.data(), |
| 127 | &dataSize |
| 128 | ); |
| 129 | if (retCode != ERROR_SUCCESS) |
| 130 | return std::nullopt; |
| 131 | DWORD stringLengthInWchars = dataSize / sizeof(wchar_t); |
| 132 | --stringLengthInWchars; // Exclude the NUL written by the Win32 API |
| 133 | data.resize(stringLengthInWchars); |
| 134 | if (data == L"true") |
| 135 | return true; |
| 136 | if (data == L"false") |
| 137 | return false; |
| 138 | return std::nullopt; |
| 139 | } |
| 140 | |
| 141 | // ResolveIt - Uses the Shell's IShellLink and IPersistFile interfaces |
| 142 | // to retrieve the path and description from an existing shortcut. |