YSTest: Registry.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Registry.cpp
浏览该文件的文档.
1 /*
2 © 2013-2016, 2019 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
29 #include "YCLib/YModules.h"
30 #include YFM_YCLib_Platform
31 #if YCL_Win32
32 # include YFM_Win32_YCLib_Registry
33 # include <ystdex/type_pun.hpp> // for ystdex::replace_cast;
34 #endif
35
36 namespace platform_ex
37 {
38
39 #if YCL_Win32
40
41 inline namespace Windows
42 {
43
44 void
45 RegistryKey::Flush()
46 {
47 YCL_RaiseZ_Win32E(::RegFlushKey(h_key), "RegFlushKey");
48 }
49
50 pair<unsigned long, vector<byte>>
51 RegistryKey::GetRawValue(const wchar_t* name, unsigned long type) const
52 {
53 unsigned long size;
54
55 YCL_RaiseZ_Win32E(::RegQueryValueExW(h_key,
56 platform::Nonnull(name), {}, type == REG_NONE ? &type : nullptr, {},
57 &size), "RegQueryValueExW");
58
59 vector<byte> res(size);
60
61 YCL_RaiseZ_Win32E(::RegQueryValueExW(h_key, name, {}, &type,
62 ystdex::replace_cast<unsigned char*>(&res[0]), &size),
63 "RegQueryValueExW");
64 return {type, std::move(res)};
65 }
66 size_t
67 RegistryKey::GetSubKeyCount() const
68 {
69 unsigned long res;
70
71 YCL_RaiseZ_Win32E(::RegQueryInfoKey(h_key, {}, {}, {},
72 &res, {}, {}, {}, {}, {}, {}, {}), "RegQueryInfoKey");
73 return size_t(res);
74 }
75 vector<wstring>
76 RegistryKey::GetSubKeyNames() const
77 {
78 const auto cnt(GetSubKeyCount());
79 vector<wstring> res;
80
81 if(cnt > 0)
82 {
83 // NOTE: See http://msdn.microsoft.com/en-us/library/windows/desktop/ms724872(v=vs.85).aspx.
84 wchar_t name[256];
85
86 for(res.reserve(cnt); res.size() < cnt; res.emplace_back(name))
87 YCL_RaiseZ_Win32E(::RegEnumKeyExW(h_key,
88 static_cast<unsigned long>(res.size()), name, {}, {}, {}, {},
89 {}), "RegEnumKeyExW");
90 }
91 return res;
92 }
93 size_t
94 RegistryKey::GetValueCount() const
95 {
96 unsigned long res;
97
98 YCL_RaiseZ_Win32E(::RegQueryInfoKey(h_key, {}, {}, {}, {},
99 {}, {}, &res, {}, {}, {}, {}), "RegQueryInfoKey");
100 return size_t(res);
101 }
102 vector<wstring>
103 RegistryKey::GetValueNames() const
104 {
105 const auto cnt(GetValueCount());
106 vector<wstring> res;
107
108 if(cnt > 0)
109 {
110 // NOTE: See http://msdn.microsoft.com/en-us/library/windows/desktop/ms724872(v=vs.85).aspx.
111 wchar_t name[16384];
112
113 for(res.reserve(cnt); res.size() < cnt; res.emplace_back(name))
114 YCL_RaiseZ_Win32E(::RegEnumValueW(h_key,
115 static_cast<unsigned long>(res.size()), name, {}, {}, {}, {},
116 {}), "RegEnumValueW");
117 }
118 return res;
119 }
120
121 wstring
122 FetchRegistryString(const RegistryKey& key, const wchar_t* name)
123 {
124 try
125 {
126 const auto pr(key.GetRawValue(name, REG_SZ));
127
128 if(pr.first == REG_SZ && !pr.second.empty())
129 // TODO: Improve performance?
130 return ystdex::rtrim(wstring(reinterpret_cast<const wchar_t*>(
131 &pr.second[0]), pr.second.size() / 2), wchar_t());
132 }
133 catch(Win32Exception&)
134 {}
135 return {};
136 }
137
138 } // inline namespace Windows;
139
140 #endif
141
142 } // namespace YSLib;
143
platform_ex::Windows::RegistryKey::Flush
void Flush()
platform::Nonnull
YB_PURE _type && Nonnull(_type &&p) ynothrowv
断言并返回非空参数。
Definition: Debug.h:518
size
size_t size
Definition: memory_resource.cpp:295
platform_ex::Windows::RegistryKey::GetRawValue
h_key pair< unsigned long, vector< byte > > GetRawValue(const wchar_t *, unsigned long=REG_NONE) const
取指定名称和类型的值的存储表示。
platform_ex
平台扩展命名空间。
Definition: Android.h:46
platform::size_t
YB_ATTR_nodiscard YF_API size_t
Definition: FileIO.h:1345
platform_ex::Windows::RegistryKey::GetValueCount
size_t GetValueCount() const
platform_ex::Windows::FetchRegistryString
YF_API const wchar_t FetchRegistryString
Definition: Registry.h:127
platform_ex::Windows::RegistryKey::GetValueNames
vector< wstring > GetValueNames() const
ystdex::rtrim
_tString && rtrim(_tString &&str, typename string_traits< _tString >::value_type c)
删除字符串中指定的连续后缀字符。
Definition: string.hpp:1454
ystdex::cpp2017::wstring
basic_string< wchar_t > wstring
Definition: string.hpp:775
platform_ex::Windows::RegistryKey::h_key
::HKEY h_key
Definition: Registry.h:51
type_pun.hpp
共享存储和直接转换。
NPL::A1::name
const char * name
Definition: NPLA1.h:1876
platform_ex::Windows::RegistryKey::GetSubKeyNames
vector< wstring > GetSubKeyNames() const
YModules.h
YCLib 库模块配置文件。
platform_ex::Windows::RegistryKey::GetSubKeyCount
type size_t GetSubKeyCount() const
YCL_RaiseZ_Win32E
#define YCL_RaiseZ_Win32E(_expr,...)
按表达式求值和指定参数抛出 Windows::Win32Exception 对象。
Definition: Win32/include/YCLib/MinGW32.h:156