Skip to content

Commit

Permalink
get rid of wmi garbage
Browse files Browse the repository at this point in the history
  • Loading branch information
moonshadow565 committed Nov 1, 2022
1 parent 3b09c8f commit 1daf507
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 89 deletions.
1 change: 0 additions & 1 deletion cslol-tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ if (WIN32)
res/longpath.manifest
res/utf8.manifest
)
target_link_libraries(cslol-lib PRIVATE comsuppw.lib wbemuuid.lib)
endif()

target_compile_definitions(cslol-lib PUBLIC -D_CRT_SECURE_NO_WARNINGS) # lmao, fuck off
Expand Down
8 changes: 6 additions & 2 deletions cslol-tools/lib/lol/io/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ using namespace lol::io;
#define handle_ok(handle) ((std::intptr_t)handle != 0 && (std::intptr_t)handle != -1)

#ifdef _WIN32
# define NOMINMAX
# define WIN32_LEAN_AND_MEAN
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
# define last_error() std::error_code((int)GetLastError(), std::system_category())

Expand Down
2 changes: 1 addition & 1 deletion cslol-tools/lib/lol/patcher/patcher_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ auto patcher::run(std::function<bool(Message, char const*)> update,
ctx.load_config(config_path);
(void)game_path;
for (;;) {
auto process = Process::Find("League of Legends.exe");
auto process = Process::Find("League of Legends.exe", "League of Legends (TM) Client");
if (!process) {
if (!update(M_WAIT_START, "")) return;
std::this_thread::sleep_for(250ms);
Expand Down
2 changes: 1 addition & 1 deletion cslol-tools/lib/lol/patcher/utility/process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace lol::patcher {

inline bool operator!() const noexcept { return !handle_; }

static auto Find(char const *name) -> std::optional<Process>;
static auto Find(char const *name, char const *window = nullptr) -> std::optional<Process>;

auto Base() const -> PtrStorage;

Expand Down
2 changes: 1 addition & 1 deletion cslol-tools/lib/lol/patcher/utility/process_macos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Process::~Process() noexcept {
}
}

auto Process::Find(char const* name) -> std::optional<Process> {
auto Process::Find(char const* name, char const* window) -> std::optional<Process> {
lol_trace_func();
pid_t pids[4096];
int bytes = proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));
Expand Down
99 changes: 18 additions & 81 deletions cslol-tools/lib/lol/patcher/utility/process_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
# include <psapi.h>
# include <tlhelp32.h>
// do not reorder
# include <comdef.h>
# include <comutil.h>
# include <wbemcli.h>
# include <wrl/client.h>
// do not reorder
# define last_error() std::error_code((int)GetLastError(), std::system_category())
# define hassert(...) \
if (auto hres = __VA_ARGS__; FAILED(hres)) _com_issue_error(hres);
Expand All @@ -25,82 +20,24 @@ namespace {
PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE;
static inline constexpr size_t DUMP_SIZE = 0x4000000;

struct deleter {
inline void operator()(HANDLE handle) const noexcept {
if (handle) {
CloseHandle(handle);
}
}
};

static inline auto SafeWinHandle(HANDLE handle) noexcept {
if (handle == INVALID_HANDLE_VALUE) {
handle = nullptr;
}
using handle_value = std::remove_pointer_t<HANDLE>;
return std::unique_ptr<handle_value, deleter>(handle);
}

static DWORD find_pid_wmi(char const* name) {
using namespace Microsoft::WRL;

static auto service = []() {
hassert(CoInitializeEx(0, COINIT_MULTITHREADED));

auto pLoc = ComPtr<IWbemLocator>{};
hassert(CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(pLoc.GetAddressOf())));

auto pSvc = ComPtr<IWbemServices>{};
hassert(pLoc->ConnectServer(bstr_t(L"ROOT\\CIMV2"), 0, 0, 0, 0, 0, 0, pSvc.GetAddressOf()));

hassert(CoSetProxyBlanket(pSvc.Get(),
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE));
return pSvc;
}();

auto query = "SELECT * FROM Win32_Process WHERE Name = '" + std::string(name) + "'";
auto enumerator = ComPtr<IEnumWbemClassObject>{};
hassert(service->ExecQuery(bstr_t("WQL"),
bstr_t(query.c_str()),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
enumerator.GetAddressOf()));

while (enumerator) {
auto clsobj = ComPtr<IWbemClassObject>{};
if (ULONG res = 0; FAILED(enumerator->Next(WBEM_INFINITE, 1, clsobj.GetAddressOf(), &res)) || !res) {
break;
}
auto var = VARIANT{};
clsobj->Get(L"ProcessId", 0, &var, NULL, NULL);
return var.intVal;
}

return 0;
}

static DWORD find_pid(char const* name) {
auto entry = PROCESSENTRY32{.dwSize = sizeof(PROCESSENTRY32)};
auto handle = SafeWinHandle(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
for (bool i = Process32First(handle.get(), &entry); i; i = Process32Next(handle.get(), &entry)) {
std::filesystem::path ExeFile = entry.szExeFile;
if (ExeFile.filename().generic_string() == name) {
return entry.th32ProcessID;
static DWORD find_pid(char const* name, char const* window) {
if (name) {
auto entry = PROCESSENTRY32{.dwSize = sizeof(PROCESSENTRY32)};
auto handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
for (bool i = Process32First(handle, &entry); i; i = Process32Next(handle, &entry)) {
std::filesystem::path ExeFile = entry.szExeFile;
if (ExeFile.filename().generic_string() == name) {
CloseHandle(handle);
return entry.th32ProcessID;
}
}
CloseHandle(handle);
}

if (static bool cache_wmi_failed = 0; !cache_wmi_failed) {
try {
return find_pid_wmi(name);
} catch (_com_error const& error) {
cache_wmi_failed = true;
logw("Failed to use WMI: {}", static_cast<const char*>(error.ErrorMessage()));
if (window) {
if (auto hwnd = FindWindowExA(nullptr, nullptr, nullptr, window)) {
auto pid = DWORD{};
GetWindowThreadProcessId(hwnd, &pid);
return pid;
}
}
return 0;
Expand Down Expand Up @@ -141,8 +78,8 @@ Process::~Process() noexcept {
}
}

auto Process::Find(char const* name) -> std::optional<Process> {
if (auto pid = find_pid(name)) {
auto Process::Find(char const* name, char const* window) -> std::optional<Process> {
if (auto pid = find_pid(name, window)) {
auto process = Process(pid);
if (!process.handle_) {
lol_throw_msg("OpenProcess: {}", last_error());
Expand Down
8 changes: 6 additions & 2 deletions cslol-tools/lib/lol/utility/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
using namespace lol;

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# define NOMINMAX
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
// do not reorder
# include <processenv.h>
Expand Down

0 comments on commit 1daf507

Please sign in to comment.