-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_list.cpp
More file actions
91 lines (74 loc) · 2 KB
/
Copy pathwindow_list.cpp
File metadata and controls
91 lines (74 loc) · 2 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
#include "window_list.hpp"
#include <vector>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
namespace droidcli::cli {
#ifdef _WIN32
namespace {
BOOL CALLBACK enum_windows_callback(HWND window, LPARAM lparam)
{
auto* out_windows = reinterpret_cast<core::Array<OpenWindowInfo>*>(lparam);
// Same filter Alt+Tab effectively applies: actually visible, and has a
// title (filters out the many invisible/utility/tooltip windows every
// running app also owns).
if (!IsWindowVisible(window))
{
return TRUE;
}
const int title_length = GetWindowTextLengthA(window);
if (title_length <= 0)
{
return TRUE;
}
std::vector<char> title_buffer(static_cast<size_t>(title_length) + 1, '\0');
GetWindowTextA(window, title_buffer.data(), title_length + 1);
const core::String title(title_buffer.data());
if (title.empty())
{
return TRUE;
}
DWORD pid = 0;
GetWindowThreadProcessId(window, &pid);
if (pid == 0)
{
return TRUE;
}
core::String process_name;
const HANDLE process_handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (process_handle != nullptr)
{
char image_path[MAX_PATH] = {};
DWORD path_size = sizeof(image_path);
if (QueryFullProcessImageNameA(process_handle, 0, image_path, &path_size) && path_size > 0)
{
const core::String full_path(image_path);
const size_t last_slash = full_path.find_last_of("\\/");
process_name = last_slash == core::String::npos ? full_path : full_path.substr(last_slash + 1);
}
CloseHandle(process_handle);
}
OpenWindowInfo info;
info.title = title;
info.process_name = process_name;
info.pid = static_cast<int64_t>(pid);
out_windows->push_back(info);
return TRUE;
}
} // namespace
core::Array<OpenWindowInfo> list_open_windows()
{
core::Array<OpenWindowInfo> windows;
EnumWindows(enum_windows_callback, reinterpret_cast<LPARAM>(&windows));
return windows;
}
#else
core::Array<OpenWindowInfo> list_open_windows()
{
return {};
}
#endif
} // namespace droidcli::cli