Skip to content

Commit 9c1437d

Browse files
committed
use wmi in sys info
1 parent adcd0d5 commit 9c1437d

File tree

2 files changed

+48
-55
lines changed

2 files changed

+48
-55
lines changed

Client/include/Client/SysInfo.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "Core/Json.hpp"
55
#include "Core/Result.hpp"
66

7+
#include <string>
8+
79

810
namespace PotatoAlert::Client {
911

Client/src/SysInfo.win32.cpp

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
#include "Core/Result.hpp"
77
#include "Core/String.hpp"
88

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")
1115

1216
#include <optional>
1317
#include <string>
@@ -20,54 +24,54 @@ using PotatoAlert::Core::String::TrimExtraNulls;
2024

2125
namespace {
2226

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
2633

27-
typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
28-
29-
RTL_OSVERSIONINFOW GetRealOSVersion()
34+
Result<std::string> WmiGetOsCaption()
3035
{
31-
if (HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll"))
36+
HRESULT hRes = ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
37+
if (hRes != S_OK && hRes != S_FALSE)
3238
{
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()));
4240
}
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);
4745

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);
5749

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);
6454

65-
TrimExtraNulls(lpData);
66-
return lpData;
67-
}
6855

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;
6972
}
7073

74+
}
7175

7276
Result<SysInfo> PotatoAlert::Client::GetSysInfo()
7377
{
@@ -79,20 +83,7 @@ Result<SysInfo> PotatoAlert::Client::GetSysInfo()
7983
return PA_ERROR(std::error_code((int)GetLastError(), std::system_category()));
8084
}
8185

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());
9687

9788
SYSTEM_INFO sysInfo;
9889
GetNativeSystemInfo(&sysInfo);
@@ -121,6 +112,6 @@ Result<SysInfo> PotatoAlert::Client::GetSysInfo()
121112
{
122113
.Os = OsType::Windows,
123114
.Arch = arch,
124-
.Release = *productName,
115+
.Release = productName,
125116
};
126117
}

0 commit comments

Comments
 (0)