Skip to content

Commit

Permalink
brand improve wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Jul 1, 2023
1 parent f6a5ba7 commit c951254
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright © 2023. Baulk contributors. All Rights Reserved.
cmake_minimum_required(VERSION 3.22)
cmake_minimum_required(VERSION 3.25)
project(Baulk)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE)
Expand All @@ -14,7 +14,7 @@ Please delete them.")
endif()

set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

set(BAULK_ENABLE_LTO OFF)
Expand Down
12 changes: 8 additions & 4 deletions include/baulk/brand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <bela/comutils.hpp>
#include <bela/str_replace.hpp>
#include <dxgi.h>
#include <format>

namespace baulk::brand {
namespace brand_internal {
Expand Down Expand Up @@ -346,14 +347,17 @@ inline void Detector::Swap(Render &render) const {
render.append_meta_line(L"WM", L"Desktop Window Manager");
render.append_meta_line(L"CPU", processor());
render.append_meta_line(L"GPU", bela::StrJoin(graphics, L", "));
render.append_meta_line(L"RAM", bela::StringCat((mem_status.total - mem_status.avail) / MiB, L"MB / ",
mem_status.total / MiB, L"MB (", mem_status.load, L"%)"));
// std::format(L"{:2f} GB / {:2f} GB ({}%)",double(mem_status.total -
// mem_status.avail)/GiB,double(mem_status.total)/GiB);
render.append_meta_line(L"RAM",
std::format(L"{:.2f} GB / {:.2f} GB ({}%)", double(mem_status.total - mem_status.avail) / GiB,
double(mem_status.total) / GiB, mem_status.load));
std::vector<std::wstring> ss;
for (const auto &so : storages) {
ss.emplace_back(so());
}
render.append_meta_line(L"Disk", bela::StrJoin(ss, L", "));
render.append_meta_line(L"Emoji",L"😊 👍 💖 💘 🗂️ 🥝 🧱");
render.append_meta_line(L"Emoji", L"😊 👍 💖 💘 🗂️ 🥝 🧱");
render.append_meta_line(L"", L"");
// COLOR
// https://chrisyeh96.github.io/2020/03/28/terminal-colors.html
Expand Down Expand Up @@ -483,7 +487,7 @@ inline bool Detector::detect_graphics(bela::error_code &ec) {
// Microsoft Basic Render Driver
continue;
}
graphics.emplace_back(bela::StrReplaceAll(desc1.Description, {{L"(R)", L""}}));
graphics.emplace_back(desc1.Description);
}
return true;
}
Expand Down
146 changes: 146 additions & 0 deletions include/baulk/wmic.hpp
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
10 changes: 9 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@ add_executable(extract_test extract.cc base.manifest)
target_link_libraries(extract_test baulk.archive)

add_executable(vfsenv_test vfsenv.cc base.manifest)
target_link_libraries(vfsenv_test belawin)
target_link_libraries(vfsenv_test belawin)

add_executable(wmic_test wmic.cc base.manifest)
target_link_libraries(wmic_test
belawin
belashl
ws2_32
DXGI
wbemuuid)
27 changes: 27 additions & 0 deletions test/wmic.cc
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;
}
1 change: 1 addition & 0 deletions tools/baulk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ target_link_libraries(
winhttp
ws2_32
DXGI
wbemuuid
Msi)

if(BAULK_ENABLE_LTO)
Expand Down
2 changes: 1 addition & 1 deletion tools/baulk/baulk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ std::optional<command_t> ParseArgv(int argc, wchar_t **argv) {
class dotcom_global_initializer {
public:
dotcom_global_initializer() {
auto hr = CoInitialize(NULL);
auto hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr)) {
auto ec = bela::make_system_error_code();
MessageBoxW(nullptr, ec.data(), L"CoInitialize", IDOK);
Expand Down

0 comments on commit c951254

Please sign in to comment.