mumble/plugins/HostWindows.cpp
Davide Beatrici 253b814ba5 FEAT(plugins): Add classes for process-related functions
Since most of the functions are already using C++, why not use classes as well?

This commit introduces the following classes:

- HostLinux: can only be compiled on Linux.
Implements peek() and module().

- HostWindows: can only be compiled on Windows.
Implements peek() and module().

- Process: abstract (cannot be instantiated directly).
Inherits from HostLinux on Linux and from HostWindows on Windows.
Provides functions that can be used with both Linux and Windows processes.
Pure virtual functions are implemented in the following classes:

- ProcessLinux: meant to be used with Linux processes, inherits from Process.
Only implements exportedSymbol(), due to the other functions being universal.
The constructor detects the architecture through the ELF header.

- ProcessWindows: meant to be used with Windows processes, inherits from Process.
Only implements exportedSymbol(), due to the other functions being universal.
The constructor detects the architecture through the NT header.
2020-10-28 19:26:56 +01:00

53 lines
1.3 KiB
C++

// Copyright 2020 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "HostWindows.h"
#include "mumble_plugin_utils.h"
#include <windows.h>
#include <tlhelp32.h>
HostWindows::HostWindows(const procid_t pid) : m_ok(false), m_pid(pid) {
m_handle = OpenProcess(PROCESS_VM_READ, false, m_pid);
if (m_handle) {
m_ok = true;
}
}
HostWindows::~HostWindows() {
if (m_handle) {
CloseHandle(m_handle);
}
}
bool HostWindows::peek(const procptr_t address, void *dst, const size_t size) const {
SIZE_T read;
const BOOL ok = ReadProcessMemory(m_handle, reinterpret_cast< void * >(address), dst, size, &read);
return (ok && read == size);
}
procptr_t HostWindows::module(const std::string &module) const {
const auto handle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, m_pid);
if (handle == INVALID_HANDLE_VALUE) {
return 0;
}
procptr_t ret = 0;
MODULEENTRY32 me;
me.dwSize = sizeof(me);
for (BOOL ok = Module32First(handle, &me); ok; ok = Module32Next(handle, &me)) {
if (me.szModule == utf8ToUtf16(module)) {
ret = reinterpret_cast< procptr_t >(me.modBaseAddr);
break;
}
}
CloseHandle(handle);
return ret;
}