6
6
#include " Core/Result.hpp"
7
7
#include " Core/String.hpp"
8
8
9
- #include " win32.h"
10
- #include " VersionHelpers.h"
9
+ // #define WIN32_USER
10
+ // #include "win32.h"
11
+ // #include <Windows.h>
12
+ #include < atlbase.h>
13
+ #include < wbemidl.h>
14
+ #pragma comment(lib, "wbemuuid.lib")
11
15
12
16
#include < optional>
13
17
#include < string>
@@ -20,54 +24,54 @@ using PotatoAlert::Core::String::TrimExtraNulls;
20
24
21
25
namespace {
22
26
23
- #if 0
24
- typedef LONG NTSTATUS;
25
- #define STATUS_SUCCESS (0x00000000)
27
+ #define CheckHRes (hRes ) \
28
+ if (FAILED(hRes)) \
29
+ { \
30
+ return PA_ERROR (std::error_code (hRes, std::system_category ())); \
31
+ } \
32
+ void
26
33
27
- typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
28
-
29
- RTL_OSVERSIONINFOW GetRealOSVersion()
34
+ Result<std::string> WmiGetOsCaption ()
30
35
{
31
- if (HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll"))
36
+ HRESULT hRes = ::CoInitializeEx (nullptr , COINIT_APARTMENTTHREADED);
37
+ if (hRes != S_OK && hRes != S_FALSE)
32
38
{
33
- if (RtlGetVersionPtr fxPtr = (RtlGetVersionPtr)::GetProcAddress(hMod, "RtlGetVersion"))
34
- {
35
- RTL_OSVERSIONINFOW rovi = { 0 };
36
- rovi.dwOSVersionInfoSize = sizeof(rovi);
37
- if (STATUS_SUCCESS == fxPtr(&rovi))
38
- {
39
- return rovi;
40
- }
41
- }
39
+ return PA_ERROR (std::error_code (hRes, std::system_category ()));
42
40
}
43
- RTL_OSVERSIONINFOW rovi = { 0 };
44
- return rovi;
45
- }
46
- # endif
41
+ hRes = :: CoInitializeSecurity ( nullptr , - 1 , nullptr , nullptr ,
42
+ RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE,
43
+ nullptr , EOAC_NONE, nullptr );
44
+ CheckHRes (hRes);
47
45
48
- static std::optional<std::string> GetRegistryString (HKEY key, std::string_view valueName, DWORD sizeBytes = 0 )
49
- {
50
- if (sizeBytes == 0 )
51
- {
52
- if (RegQueryValueExA (key, valueName.data (), nullptr , nullptr , nullptr , &sizeBytes) != ERROR_SUCCESS)
53
- {
54
- return {};
55
- }
56
- }
46
+ CComPtr<IWbemLocator> pLocator;
47
+ hRes = pLocator.CoCreateInstance (CLSID_WbemLocator);
48
+ CheckHRes (hRes);
57
49
58
- std::string lpData;
59
- lpData.resize (sizeBytes / sizeof (CHAR));
60
- if (RegQueryValueExA (key, valueName.data (), nullptr , nullptr , (LPBYTE)lpData.data (), &sizeBytes) != ERROR_SUCCESS)
61
- {
62
- return {};
63
- }
50
+ CComPtr<IWbemServices> pService;
51
+ hRes = pLocator->ConnectServer (CComBSTR (L" root\\ cimv2" ), nullptr , nullptr , nullptr , WBEM_FLAG_CONNECT_USE_MAX_WAIT,
52
+ nullptr , nullptr , &pService);
53
+ CheckHRes (hRes);
64
54
65
- TrimExtraNulls (lpData);
66
- return lpData;
67
- }
68
55
56
+ CComPtr<IEnumWbemClassObject> pEnum;
57
+ hRes = pService->ExecQuery (CComBSTR (L" WQL" ), CComBSTR (L" Select Caption from Win32_OperatingSystem" ), WBEM_FLAG_FORWARD_ONLY, nullptr , &pEnum);
58
+ CheckHRes (hRes);
59
+
60
+ ULONG uObjectCount = 0 ;
61
+ CComPtr<IWbemClassObject> pWmiObject;
62
+ hRes = pEnum->Next (WBEM_INFINITE, 1 , &pWmiObject, &uObjectCount);
63
+ CheckHRes (hRes);
64
+
65
+ CComVariant cvtCaption;
66
+ hRes = pWmiObject->Get (L" Caption" , 0 , &cvtCaption, nullptr , nullptr );
67
+ CheckHRes (hRes);
68
+
69
+ CoUninitialize ();
70
+
71
+ return CW2A (cvtCaption.bstrVal ).m_psz ;
69
72
}
70
73
74
+ }
71
75
72
76
Result<SysInfo> PotatoAlert::Client::GetSysInfo ()
73
77
{
@@ -79,20 +83,7 @@ Result<SysInfo> PotatoAlert::Client::GetSysInfo()
79
83
return PA_ERROR (std::error_code ((int )GetLastError (), std::system_category ()));
80
84
}
81
85
82
- HKEY hKey;
83
- PA_DEFER
84
- {
85
- RegCloseKey (hKey);
86
- };
87
- if (RegOpenKeyExW (HKEY_LOCAL_MACHINE, LR"( SOFTWARE\Microsoft\Windows NT\CurrentVersion)" , 0 , KEY_READ, &hKey) != ERROR_SUCCESS)
88
- {
89
- return PA_ERROR (std::error_code ((int )GetLastError (), std::system_category ()));
90
- }
91
- std::optional productName = GetRegistryString (hKey, " ProductName" );
92
- if (!productName)
93
- {
94
- return PA_ERROR (std::error_code ((int )GetLastError (), std::system_category ()));
95
- }
86
+ PA_TRY (productName, WmiGetOsCaption ());
96
87
97
88
SYSTEM_INFO sysInfo;
98
89
GetNativeSystemInfo (&sysInfo);
@@ -121,6 +112,6 @@ Result<SysInfo> PotatoAlert::Client::GetSysInfo()
121
112
{
122
113
.Os = OsType::Windows,
123
114
.Arch = arch,
124
- .Release = * productName,
115
+ .Release = productName,
125
116
};
126
117
}
0 commit comments