-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
194 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#ifndef BAULK_WMIC_HPP | ||
#define BAULK_WMIC_HPP | ||
#include <bela/base.hpp> | ||
#include <bela/codecvt.hpp> | ||
#include <bela/str_join.hpp> | ||
#include <bela/terminal.hpp> | ||
#include <bela/time.hpp> | ||
#include <bela/win32.hpp> | ||
#include <bela/env.hpp> | ||
#include <bela/comutils.hpp> | ||
#include <bela/str_replace.hpp> | ||
#include <dxgi.h> | ||
#include <format> | ||
#include <comdef.h> | ||
#include <Wbemidl.h> | ||
|
||
namespace baulk::wmic { | ||
class Locator { | ||
public: | ||
Locator() = default; | ||
bool Initialize(bela::error_code &ec) { | ||
auto result = CoInitializeSecurity(NULL, | ||
-1, // COM authentication | ||
NULL, // Authentication services | ||
NULL, // Reserved | ||
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication | ||
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation | ||
NULL, // Authentication info | ||
EOAC_NONE, // Additional capabilities | ||
NULL // Reserved | ||
); | ||
|
||
if (FAILED(result)) { | ||
ec = bela::make_system_error_code(L"Failed to initialize security. "); | ||
return false; | ||
} | ||
if (CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, | ||
reinterpret_cast<void **>(&locator)) != S_OK) { | ||
ec = bela::make_system_error_code(L"Failed to create IWbemLocator object. "); | ||
return false; | ||
} | ||
result = locator->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace | ||
NULL, // User name. NULL = current user | ||
NULL, // User password. NULL = current | ||
0, // Locale. NULL indicates current | ||
NULL, // Security flags. | ||
0, // Authority (for example, Kerberos) | ||
0, // Context object | ||
&services // pointer to IWbemServices proxy | ||
); | ||
if (FAILED(result)) { | ||
ec = bela::make_system_error_code(L"connect wmic error "); | ||
return false; | ||
} | ||
result = CoSetProxyBlanket(services.get(), // Indicates the proxy to set | ||
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx | ||
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx | ||
NULL, // Server principal name | ||
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx | ||
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx | ||
NULL, // client identity | ||
EOAC_NONE // proxy capabilities | ||
); | ||
if (FAILED(result)) { | ||
ec = bela::make_system_error_code(L"CoSetProxyBlanket "); | ||
return false; | ||
} | ||
return true; | ||
} | ||
bool SearchOS(bela::error_code &ec) const { | ||
bela::comptr<IEnumWbemClassObject> e; | ||
auto result = services->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_OperatingSystem"), | ||
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &e); | ||
|
||
if (FAILED(result)) { | ||
return false; | ||
} | ||
|
||
// Step 7: ------------------------------------------------- | ||
// Get the data from the query in step 6 ------------------- | ||
|
||
ULONG uReturn = 0; | ||
|
||
while (e) { | ||
bela::comptr<IWbemClassObject> clsObj; | ||
HRESULT hr = e->Next(WBEM_INFINITE, 1, &clsObj, &uReturn); | ||
if (0 == uReturn) { | ||
break; | ||
} | ||
|
||
bela::unique_variant caption; | ||
bela::unique_variant buildNumber; | ||
hr = clsObj->Get(L"Caption", 0, &caption, 0, 0); | ||
bela::FPrintF(stderr, L"Caption: %s\n", caption.bstrVal); | ||
hr = clsObj->Get(L"BuildNumber", 0, &buildNumber, 0, 0); | ||
bela::FPrintF(stderr, L"BuildNumber: %s\n", buildNumber.bstrVal); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool SeachDisk(bela::error_code &ec) const { | ||
bela::comptr<IEnumWbemClassObject> e; | ||
auto result = services->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_DiskDrive"), | ||
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &e); | ||
|
||
if (FAILED(result)) { | ||
return false; | ||
} | ||
|
||
ULONG uReturn = 0; | ||
constexpr auto GiB = 1024 * 1024 * 1024ull; | ||
while (e) { | ||
bela::comptr<IWbemClassObject> clsObj; | ||
HRESULT hr = e->Next(WBEM_INFINITE, 1, &clsObj, &uReturn); | ||
if (0 == uReturn) { | ||
break; | ||
} | ||
bela::unique_variant Manufacturer; | ||
bela::unique_variant Name; | ||
bela::unique_variant Model; | ||
bela::unique_variant Size; | ||
bela::unique_variant MediaType; | ||
hr = clsObj->Get(L"Manufacturer", 0, &Manufacturer, 0, 0); | ||
hr = clsObj->Get(L"Name", 0, &Name, 0, 0); | ||
hr = clsObj->Get(L"Model", 0, &Model, 0, 0); | ||
hr = clsObj->Get(L"Size", 0, &Size, 0, 0); | ||
hr = clsObj->Get(L"MediaType", 0, &MediaType, 0, 0); | ||
int64_t sizeBytes = 0; | ||
(void)bela::SimpleAtoi(Size.bstrVal, &sizeBytes); | ||
bela::FPrintF(stderr, L"Manufacturer: %s Name: %s Model: %s Size: %0.2f GB MediaType: %s\n", Manufacturer.bstrVal, | ||
Name.bstrVal, Model.bstrVal, double(sizeBytes) / GiB, MediaType.bstrVal); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private: | ||
Locator(const Locator &) = delete; | ||
Locator &operator=(const Locator &) = delete; | ||
bela::comptr<IWbemLocator> locator; | ||
bela::comptr<IWbemServices> services; | ||
}; | ||
} // namespace baulk::wmic | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <baulk/wmic.hpp> | ||
|
||
class dotcom_global_initializer { | ||
public: | ||
dotcom_global_initializer() { | ||
auto hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); | ||
if (FAILED(hr)) { | ||
auto ec = bela::make_system_error_code(); | ||
MessageBoxW(nullptr, ec.data(), L"CoInitialize", IDOK); | ||
exit(1); | ||
} | ||
} | ||
~dotcom_global_initializer() { CoUninitialize(); } | ||
}; | ||
|
||
int wmain() { | ||
dotcom_global_initializer di; | ||
baulk::wmic::Locator locator; | ||
bela::error_code ec; | ||
if (!locator.Initialize(ec)) { | ||
bela::FPrintF(stderr, L"wmic initialize error: %v\n", ec); | ||
return 1; | ||
} | ||
locator.SearchOS(ec); | ||
locator.SeachDisk(ec); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ target_link_libraries( | |
winhttp | ||
ws2_32 | ||
DXGI | ||
wbemuuid | ||
Msi) | ||
|
||
if(BAULK_ENABLE_LTO) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters