forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessInfo.cpp
More file actions
142 lines (119 loc) · 3.33 KB
/
Copy pathProcessInfo.cpp
File metadata and controls
142 lines (119 loc) · 3.33 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "DebugViewppLib/ProcessInfo.h"
#include "CobaltFusion/Str.h"
#include "Win32/Win32Lib.h"
#include "DebugViewppLib/Conversions.h"
#include "DebugViewppLib/Colors.h"
#include <cassert>
#include <array>
#include "windows.h"
#include <psapi.h>
#pragma comment(lib, "psapi.lib")
namespace fusion {
namespace debugviewpp {
InternalProcessProperties::InternalProcessProperties() :
pid(0),
color(0)
{
}
InternalProcessProperties::InternalProcessProperties(DWORD pid, const std::wstring& name, COLORREF color) :
pid(pid),
name(name),
color(color)
{
}
ProcessProperties::ProcessProperties(const InternalProcessProperties& iprops) :
uid(0),
pid(iprops.pid),
name(iprops.name),
color(iprops.color)
{
}
ProcessInfo::ProcessInfo() :
m_unqiueId(0)
{
}
void ProcessInfo::Clear()
{
m_unqiueId = 0;
m_processProperties.clear();
}
size_t ProcessInfo::GetPrivateBytes()
{
PROCESS_MEMORY_COUNTERS_EX memoryCounters;
GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&memoryCounters), sizeof(memoryCounters));
return memoryCounters.PrivateUsage;
}
std::wstring ProcessInfo::GetProcessName(HANDLE handle)
{
std::array<wchar_t, MAX_PATH> buf;
auto rc = GetProcessImageFileName(handle, buf.data(), static_cast<DWORD>(buf.size()));
if (rc == 0)
{
return L"";
}
// todo: make function? what does this do? (jan)
const wchar_t* name = buf.data();
for (auto it = buf.data(); *it != 0u; ++it)
{
if (*it == '\\')
{
name = it + 1;
}
}
return name;
}
std::wstring ProcessInfo::GetStartTime(HANDLE handle)
{
FILETIME creation = {};
FILETIME exit = {};
FILETIME kernel = {};
FILETIME user = {};
GetProcessTimes(handle, &creation, &exit, &kernel, &user);
return WStr(GetTimeText(creation)).str();
}
std::wstring ProcessInfo::GetProcessNameByPid(DWORD processId)
{
Win32::Handle hProcess(::OpenProcess(PROCESS_QUERY_INFORMATION, 0, processId));
if (hProcess)
{
return GetProcessName(hProcess.get());
}
return L"";
}
DWORD ProcessInfo::GetUid(DWORD processId, const std::wstring& processName)
{
for (auto& item : m_processProperties)
{
if (item.second.pid == processId && item.second.name == processName)
{
return item.first;
}
}
DWORD index = m_unqiueId;
++m_unqiueId;
m_processProperties[index] = InternalProcessProperties(processId, processName, GetRandomProcessColor());
return index;
}
ProcessProperties ProcessInfo::GetProcessProperties(DWORD processId, const std::wstring& processName)
{
auto uid = GetUid(processId, processName);
ProcessProperties props(m_processProperties[uid]);
props.uid = uid;
return props;
}
ProcessProperties ProcessInfo::GetProcessProperties(DWORD uid) const
{
auto it = m_processProperties.find(uid);
assert(it != m_processProperties.end());
if (it == m_processProperties.end())
{
return ProcessProperties(InternalProcessProperties());
}
return ProcessProperties(it->second);
}
} // namespace debugviewpp
} // namespace fusion