YSTest: Registry.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

Registry.cpp
浏览该文件的文档.
1 /*
2 © 2013-2016 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 #endif
34
35 namespace platform_ex
36 {
37
38 #if YCL_Win32
39
40 inline namespace Windows
41 {
42
43 void
44 RegistryKey::Flush()
45 {
46 YCL_RaiseZ_Win32E(::RegFlushKey(h_key), "RegFlushKey");
47 }
48
49 pair<unsigned long, vector<byte>>
50 RegistryKey::GetRawValue(const wchar_t* name, unsigned long type) const
51 {
52 unsigned long size;
53
54 YCL_RaiseZ_Win32E(::RegQueryValueExW(h_key,
55 platform::Nonnull(name), {}, type == REG_NONE ? &type : nullptr, {},
56 &size), "RegQueryValueExW");
57
58 vector<byte> res(size);
59
60 YCL_RaiseZ_Win32E(::RegQueryValueExW(h_key, name,
61 {}, &type, &res[0], &size), "RegQueryValueExW");
62 return {type, std::move(res)};
63 }
64 size_t
65 RegistryKey::GetSubKeyCount() const
66 {
67 unsigned long res;
68
69 YCL_RaiseZ_Win32E(::RegQueryInfoKey(h_key, {}, {}, {},
70 &res, {}, {}, {}, {}, {}, {}, {}), "RegQueryInfoKey");
71 return size_t(res);
72 }
73 vector<wstring>
74 RegistryKey::GetSubKeyNames() const
75 {
76 const auto cnt(GetSubKeyCount());
77 vector<wstring> res;
78
79 if(cnt > 0)
80 {
81 // NOTE: See http://msdn.microsoft.com/en-us/library/windows/desktop/ms724872(v=vs.85).aspx .
82 wchar_t name[256];
83
84 for(res.reserve(cnt); res.size() < cnt; res.emplace_back(name))
85 YCL_RaiseZ_Win32E(::RegEnumKeyExW(h_key,
86 static_cast<unsigned long>(res.size()), name, {}, {}, {}, {},
87 {}), "RegEnumKeyExW");
88 }
89 return res;
90 }
91 size_t
92 RegistryKey::GetValueCount() const
93 {
94 unsigned long res;
95
96 YCL_RaiseZ_Win32E(::RegQueryInfoKey(h_key, {}, {}, {}, {},
97 {}, {}, &res, {}, {}, {}, {}), "RegQueryInfoKey");
98 return size_t(res);
99 }
100 vector<wstring>
101 RegistryKey::GetValueNames() const
102 {
103 const auto cnt(GetValueCount());
104 vector<wstring> res;
105
106 if(cnt > 0)
107 {
108 // NOTE: See http://msdn.microsoft.com/en-us/library/windows/desktop/ms724872(v=vs.85).aspx .
109 wchar_t name[16384];
110
111 for(res.reserve(cnt); res.size() < cnt; res.emplace_back(name))
112 YCL_RaiseZ_Win32E(::RegEnumValueW(h_key,
113 static_cast<unsigned long>(res.size()), name, {}, {}, {}, {},
114 {}), "RegEnumValueW");
115 }
116 return res;
117 }
118
119 wstring
120 FetchRegistryString(const RegistryKey& key, const wchar_t* name)
121 {
122 try
123 {
124 const auto pr(key.GetRawValue(name, REG_SZ));
125
126 if(pr.first == REG_SZ && !pr.second.empty())
127 // TODO: Improve performance?
128 return ystdex::rtrim(wstring(reinterpret_cast<const wchar_t*>(
129 &pr.second[0]), pr.second.size() / 2), wchar_t());
130 }
131 catch(Win32Exception&)
132 {}
133 return {};
134 }
135
136 } // inline namespace Windows;
137
138 #endif
139
140 } // namespace YSLib;
141
platform_ex::Windows::RegistryKey::GetValueCount
size_t GetValueCount() const
platform::containers::wstring
basic_string< wchar_t > wstring
Definition: Container.h:108
YCL_RaiseZ_Win32E
#define YCL_RaiseZ_Win32E(_expr,...)
按表达式求值和指定参数抛出 Windows::Win32Exception 对象。
Definition: MinGW32.h:147
platform::size_t
YF_API size_t
Definition: FileIO.h:1353
platform_ex::Windows::RegistryKey::GetSubKeyCount
type size_t GetSubKeyCount() const
platform_ex::Windows::RegistryKey::GetSubKeyNames
vector< wstring > GetSubKeyNames() const
YModules.h
ystdex::rtrim
_tString && rtrim(_tString &&str, typename string_traits< _tString >::value_type c)
删除字符串中指定的连续后缀字符。
Definition: string.hpp:676
platform_ex::Windows::RegistryKey::GetRawValue
h_key pair< unsigned long, vector< byte > > GetRawValue(const wchar_t *, unsigned long=REG_NONE) const
取指定名称和类型的值的存储表示。
platform::Nonnull
_type && Nonnull(_type &&p) ynothrowv
断言并返回非空参数。
Definition: Debug.h:422
platform_ex::Windows::RegistryKey::Flush
void Flush()
platform_ex
平台扩展命名空间。
Definition: Android.h:45
YSLib::UI::name
yconstfn const string & name
Definition: Loader.h:130
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
platform_ex::Windows::RegistryKey::h_key
::HKEY h_key
Definition: Registry.h:51
platform_ex::Windows::RegistryKey::GetValueNames
vector< wstring > GetValueNames() const