QtCanpool 3.3.0
载入中...
搜索中...
未找到
windowkit_win.cpp
浏览该文件的文档.
1
5#include "windowkit_win.h"
6
8
9static RTL_OSVERSIONINFOW GetRealOSVersionImpl()
10{
11 HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
12 using RtlGetVersionPtr = NTSTATUS(WINAPI *)(PRTL_OSVERSIONINFOW);
13 // GetProcAddress returns FARPROC; converting it to the actual signature is the
14 // documented usage, but GCC's -Wcast-function-type flags it anyway.
15 QT_WARNING_PUSH
16 QT_WARNING_DISABLE_GCC("-Wcast-function-type")
17 auto pRtlGetVersion = reinterpret_cast<RtlGetVersionPtr>(::GetProcAddress(hMod, "RtlGetVersion"));
18 QT_WARNING_POP
19 RTL_OSVERSIONINFOW rovi{};
20 rovi.dwOSVersionInfoSize = sizeof(rovi);
21 pRtlGetVersion(&rovi);
22 return rovi;
23}
24
25namespace Private
26{
27
28RTL_OSVERSIONINFOW GetRealOSVersion()
29{
30 static const auto result = GetRealOSVersionImpl();
31 return result;
32}
33
34}
35
36#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
37WindowsRegistryKey::WindowsRegistryKey(HKEY parentHandle, QStringView subKey, REGSAM permissions, REGSAM access)
38{
39 if (::RegOpenKeyExW(parentHandle, reinterpret_cast<const wchar_t *>(subKey.utf16()), 0, permissions | access,
40 &m_key) != ERROR_SUCCESS) {
41 m_key = nullptr;
42 }
43}
44
45WindowsRegistryKey::~WindowsRegistryKey()
46{
47 close();
48}
49
50void WindowsRegistryKey::close()
51{
52 if (isValid()) {
53 ::RegCloseKey(m_key);
54 m_key = nullptr;
55 }
56}
57
58QString WindowsRegistryKey::stringValue(QStringView subKey) const
59{
60 QString result;
61 if (!isValid())
62 return result;
63 DWORD type;
64 DWORD size;
65 auto subKeyC = reinterpret_cast<const wchar_t *>(subKey.utf16());
66 if (::RegQueryValueExW(m_key, subKeyC, nullptr, &type, nullptr, &size) != ERROR_SUCCESS ||
67 (type != REG_SZ && type != REG_EXPAND_SZ) || size <= 2) {
68 return result;
69 }
70 // Reserve more for rare cases where trailing '\0' are missing in registry.
71 // Rely on 0-termination since strings of size 256 padded with 0 have been
72 // observed (QTBUG-84455).
73 size += 2;
74 QVarLengthArray<unsigned char> buffer(static_cast<int>(size));
75 std::fill(buffer.data(), buffer.data() + size, 0u);
76 if (::RegQueryValueExW(m_key, subKeyC, nullptr, &type, buffer.data(), &size) == ERROR_SUCCESS)
77 result = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(buffer.constData()));
78 return result;
79}
80
81QPair<DWORD, bool> WindowsRegistryKey::dwordValue(QStringView subKey) const
82{
83 if (!isValid())
84 return qMakePair(0, false);
85 DWORD type;
86 auto subKeyC = reinterpret_cast<const wchar_t *>(subKey.utf16());
87 if (::RegQueryValueExW(m_key, subKeyC, nullptr, &type, nullptr, nullptr) != ERROR_SUCCESS || type != REG_DWORD) {
88 return qMakePair(0, false);
89 }
90 DWORD value = 0;
91 DWORD size = sizeof(value);
92 const bool ok = ::RegQueryValueExW(m_key, subKeyC, nullptr, nullptr, reinterpret_cast<unsigned char *>(&value),
93 &size) == ERROR_SUCCESS;
94 return qMakePair(value, ok);
95}
96#endif
97
RTL_OSVERSIONINFOW GetRealOSVersion()
#define QX_WINDOW_END_NAMESPACE
#define QX_WINDOW_BEGIN_NAMESPACE