-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprocess.cpp
39 lines (33 loc) · 956 Bytes
/
process.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "process.hpp"
#include <algorithm>
#include <tlhelp32.h>
#include <Psapi.h>
#include <array>
std::uint32_t native::process::id_from_name(std::string_view process_name) noexcept
{
DWORD process_list[516], bytes_needed;
if (K32EnumProcesses(process_list, sizeof(process_list), &bytes_needed))
{
for (size_t index = 0; index < bytes_needed / sizeof(std::uint32_t); index++)
{
auto proc = process(process_list[index], PROCESS_ALL_ACCESS);
if (proc && process_name == proc.get_name())
return process_list[index];
}
}
return 0x00;
}
std::uint32_t native::process::get_id() const noexcept
{
return GetProcessId(this->handle().unsafe_handle());
}
std::string native::process::get_name() const noexcept
{
char buffer[MAX_PATH];
GetModuleBaseNameA(this->handle().unsafe_handle(), nullptr, buffer, MAX_PATH);
return std::string(buffer);
}
const safe_handle& native::process::handle() const noexcept
{
return this->m_handle;
}