annotate dep/animone/src/fd/win32.cc @ 355:83e6ba09338f

gui/pages/seasons: fix function disambiguation on win32
author Paper <paper@paper.us.eu.org>
date Sun, 14 Jul 2024 23:46:18 -0400
parents 1faa72660932
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
310
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
1 /*
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
2 * fd/win32.cc: support for windows
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
3 *
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
4 * this file is noticably more complex than *nix, and that's because
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
5 * there is no "simple" way to get the paths of a file. In fact, this thing requires
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
6 * you to use *internal functions* that can't even be linked to, hence why we have to
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
7 * use GetProcAddress and such. what a mess.
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
8 *
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
9 * Speaking of which, because this file uses internal functions of the OS, it is not
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
10 * even guaranteed to work far into the future. however, just like with macOS, these
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
11 * things have stayed the same since Vista so if Microsoft *really* wants compatibility
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
12 * then they're pretty much forced to keeping this the same anyway.
a4257370de16 dep/animone: prepare for v1.0 release; it should be ready by now
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
13 */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
14 #include "animone/fd/win32.h"
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
15 #include "animone/util.h"
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
16 #include "animone.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
17 #include "animone/util/win32.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
18
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
19 #include <stdexcept>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
20 #include <string>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
21 #include <unordered_map>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
22 #include <vector>
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
23 #include <optional>
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
24
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
25 #include <fileapi.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
26 #include <handleapi.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
27 #include <libloaderapi.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
28 #include <ntdef.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
29 #include <psapi.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
30 #include <shlobj.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
31 #include <stringapiset.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
32 #include <tlhelp32.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
33 #include <windows.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
34 #include <winternl.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
35
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
36 /* SystemExtendedHandleInformation is only available in NT 5.1+ (XP and higher) and provides information for
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
37 * 32-bit PIDs, unlike SystemHandleInformation */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
38 static constexpr SYSTEM_INFORMATION_CLASS SystemExtendedHandleInformation = static_cast<SYSTEM_INFORMATION_CLASS>(0x40);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
39 static constexpr NTSTATUS STATUS_INFO_LENGTH_MISMATCH = 0xC0000004UL;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
40
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
41 struct SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
42 PVOID Object;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
43 ULONG_PTR UniqueProcessId;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
44 HANDLE HandleValue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
45 ACCESS_MASK GrantedAccess;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
46 USHORT CreatorBackTraceIndex;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
47 USHORT ObjectTypeIndex;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
48 ULONG HandleAttributes;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
49 ULONG Reserved;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
50 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
51
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
52 struct SYSTEM_HANDLE_INFORMATION_EX {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
53 ULONG_PTR NumberOfHandles;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
54 ULONG_PTR Reserved;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
55 SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX Handles[1];
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
56 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
57
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
58 namespace animone::internal::win32 {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
59
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
60 class Ntdll {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
61 public:
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
62 Ntdll() {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
63 ntdll = ::GetModuleHandleW(L"ntdll.dll");
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
64 nt_query_system_information = reinterpret_cast<decltype(::NtQuerySystemInformation)*>(
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
65 ::GetProcAddress(ntdll, "NtQuerySystemInformation"));
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
66 nt_query_object = reinterpret_cast<decltype(::NtQueryObject)*>(::GetProcAddress(ntdll, "NtQueryObject"));
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
67 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
68
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
69 NTSTATUS QuerySystemInformation(SYSTEM_INFORMATION_CLASS cls, PVOID sysinfo, ULONG len,
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
70 PULONG retlen){
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
71 return nt_query_system_information(cls, sysinfo, len, retlen);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
72 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
73
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
74 NTSTATUS QueryObject(HANDLE handle, OBJECT_INFORMATION_CLASS cls, PVOID objinf, ULONG objinflen, PULONG retlen) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
75 return nt_query_object(handle, cls, objinf, objinflen, retlen);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
76 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
77
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
78 private:
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
79 HMODULE ntdll;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
80 decltype(::NtQuerySystemInformation)* nt_query_system_information;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
81 decltype(::NtQueryObject)* nt_query_object;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
82 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
83
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
84 Ntdll ntdll;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
85
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
86 static HANDLE DuplicateHandle(HANDLE process_handle, HANDLE handle) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
87 HANDLE dup_handle = nullptr;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
88 const bool result =
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
89 ::DuplicateHandle(process_handle, handle, ::GetCurrentProcess(), &dup_handle, 0, false, DUPLICATE_SAME_ACCESS);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
90 return result ? dup_handle : nullptr;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
91 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
92
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
93 static std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> GetSystemHandleInformation() {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
94 /* we should really put a cap on this */
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
95 ULONG cb = 1 << 19;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
96 NTSTATUS status = STATUS_NO_MEMORY;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
97 std::unique_ptr<SYSTEM_HANDLE_INFORMATION_EX> info;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
98
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
99 do {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
100 info.reset(reinterpret_cast<SYSTEM_HANDLE_INFORMATION_EX*>(malloc(cb *= 2)));
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
101 if (!info)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
102 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
103
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
104 status = ntdll.QuerySystemInformation(SystemExtendedHandleInformation, info.get(), cb, &cb);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
105 } while (status == STATUS_INFO_LENGTH_MISMATCH);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
106
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
107 if (!NT_SUCCESS(status))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
108 return {};
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
109
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
110 std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> res;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
111
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
112 ULONG_PTR handles = info->NumberOfHandles;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
113 if (!handles)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
114 return {};
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
115
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
116 res.reserve(handles);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
117
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
118 SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX* entry = info->Handles;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
119 do {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
120 if (entry)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
121 res.push_back(*(entry++));
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
122 } while (--handles);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
123
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
124 return res;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
125 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
126
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
127 static std::wstring GetHandleType(HANDLE handle) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
128 OBJECT_TYPE_INFORMATION info = {0};
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
129 ntdll.QueryObject(handle, ObjectTypeInformation, &info, sizeof(info), NULL);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
130 return std::wstring(info.TypeName.Buffer, info.TypeName.Length);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
131 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
132
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
133 static std::wstring GetFinalPathNameByHandle(HANDLE handle) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
134 std::wstring buffer;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
135
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
136 DWORD size = ::GetFinalPathNameByHandleW(handle, nullptr, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
137 buffer.resize(size);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
138 ::GetFinalPathNameByHandleW(handle, &buffer.front(), buffer.size(), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
139
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
140 return buffer;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
141 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
142
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
143 /* ------------------------------------------------------------------- */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
144
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
145 static bool GetSystemDirectory(std::wstring& str) {
343
1faa72660932 *: transfer back to cmake from autotools
Paper <paper@paper.us.eu.org>
parents: 342
diff changeset
146 /* libtool can't link with -luuid properly. */
1faa72660932 *: transfer back to cmake from autotools
Paper <paper@paper.us.eu.org>
parents: 342
diff changeset
147 constexpr uint32_t WindowsDir = 0x24;
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
148 str.assign(MAX_PATH, '\0');
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
149
343
1faa72660932 *: transfer back to cmake from autotools
Paper <paper@paper.us.eu.org>
parents: 342
diff changeset
150 return SUCCEEDED(::SHGetFolderPathW(NULL, WindowsDir, NULL, SHGFP_TYPE_CURRENT, &str.front()));
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
151 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
152
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
153 static bool IsSystemDirectory(std::wstring path) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
154 std::wstring windir;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
155 if (!GetSystemDirectory(windir))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
156 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
157
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
158 ::CharUpperBuffW(&path.front(), path.length());
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
159 ::CharUpperBuffW(&windir.front(), windir.length());
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
160
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
161 // XXX wtf is 4?
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
162 return path.find(windir) == 4;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
163 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
164
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
165 static bool IsSystemDirectory(const std::string& path) {
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
166 return IsSystemDirectory(ToWstring(path));
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
167 }
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
168
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
169 static bool IsFileHandle(HANDLE handle, unsigned short object_type_index) {
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
170 /* this is filled in at runtime because it's not guaranteed to be (and isn't)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
171 * constant between different versions of Windows */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
172 static std::optional<unsigned short> file_type_index;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
173
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
174 if (file_type_index.has_value()) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
175 return object_type_index == file_type_index.value();
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
176 } else if (!handle) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
177 /* XXX what? */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
178 return true;
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
179 } else if (GetHandleType(handle) == L"File") {
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
180 file_type_index = object_type_index;
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
181 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
182 }
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
183
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
184 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
185 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
186
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
187 static bool IsFileMaskOk(ACCESS_MASK access_mask) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
188 if (!(access_mask & FILE_READ_DATA))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
189 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
190
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
191 if ((access_mask & FILE_APPEND_DATA) || (access_mask & FILE_WRITE_EA) || (access_mask & FILE_WRITE_ATTRIBUTES))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
192 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
193
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
194 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
195 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
196
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
197 static bool IsFilePathOk(const std::wstring& path) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
198 if (path.empty())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
199 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
200
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
201 if (IsSystemDirectory(path))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
202 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
203
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
204 const auto file_attributes = GetFileAttributesW(path.c_str());
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
205 if ((file_attributes == INVALID_FILE_ATTRIBUTES) || (file_attributes & FILE_ATTRIBUTE_DIRECTORY))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
206 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
207
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
208 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
209 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
210
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
211 /* ------------------------------------------------------------------- */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
212
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
213 static bool GetProcessPath(DWORD process_id, std::string& path) {
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
214 // If we try to open a SYSTEM process, this function fails and the last error
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
215 // code is ERROR_ACCESS_DENIED.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
216 //
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
217 // Note that if we requested PROCESS_QUERY_INFORMATION access right instead
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
218 // of PROCESS_QUERY_LIMITED_INFORMATION, this function would fail when used
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
219 // to open an elevated process.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
220 Handle process_handle(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_id));
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
221
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
222 if (!process_handle)
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
223 return false;
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
224
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
225 std::wstring buffer(MAX_PATH, L'\0');
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
226 DWORD buf_size = buffer.length();
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
227
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
228 // Note that this function requires Windows Vista or above. You may use
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
229 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
230 if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size))
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
231 return false;
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
232
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
233 buffer.resize(buf_size);
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
234 path = ToUtf8String(buffer);
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
235 return true;
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
236 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
237
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
238 static std::string GetFilenameFromPath(const std::string& path) {
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
239 const auto pos = path.find_last_of("/\\");
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
240 return pos != std::wstring::npos ? path.substr(pos + 1) : path;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
241 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
242
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
243 static bool VerifyProcessPath(const std::string& path) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
244 return !path.empty() && !IsSystemDirectory(path);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
245 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
246
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
247 static bool VerifyProcessFilename(const std::string& name) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
248 static const std::set<std::string> invalid_names = {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
249 // System files
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
250 "explorer.exe", // Windows Explorer
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
251 "taskeng.exe", // Task Scheduler Engine
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
252 "taskhost.exe", // Host Process for Windows Tasks
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
253 "taskhostex.exe", // Host Process for Windows Tasks
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
254 "taskmgr.exe", // Task Manager
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
255 "services.exe", // Service Control Manager
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
256 };
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
257
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
258 if (name.empty())
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
259 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
260
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
261 for (const auto& invalid_name : invalid_names)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
262 if (util::EqualStrings(name, invalid_name))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
263 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
264
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
265 return true;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
266 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
267
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
268 /* ------------------------------------------------------------------- */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
269 /* extern functions */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
270
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
271 bool GetProcessName(pid_t pid, std::string& name) {
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
272 if (!GetProcessPath(pid, name) || !VerifyProcessPath(name))
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
273 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
274
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
275 name = GetFilenameFromPath(name);
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
276 if (!VerifyProcessFilename(name))
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
277 return false;
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
278
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
279 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
280 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
281
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
282 bool EnumerateOpenProcesses(process_proc_t process_proc) {
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
283 Handle process_snap(::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
284 if (process_snap.get() == INVALID_HANDLE_VALUE)
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
285 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
286
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
287 PROCESSENTRY32 pe32;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
288 pe32.dwSize = sizeof(PROCESSENTRY32);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
289
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
290 if (!::Process32First(process_snap.get(), &pe32))
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
291 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
292
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
293 do {
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
294 std::string name;
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
295 if (!GetProcessName(pe32.th32ProcessID, name))
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
296 continue;
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
297
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
298 if (!process_proc({.pid = pe32.th32ProcessID, .platform = ExecutablePlatform::Win32, .comm = name}))
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
299 return false;
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
300 } while (::Process32Next(process_snap.get(), &pe32));
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
302 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
303 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
304
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
305 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
306 if (!open_file_proc)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
307 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
308
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
309 std::unordered_map<pid_t, Handle> proc_handles;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
310
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
311 for (const pid_t& pid : pids) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
312 const HANDLE handle = ::OpenProcess(PROCESS_DUP_HANDLE, false, pid);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
313 if (handle != INVALID_HANDLE_VALUE)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
314 proc_handles[pid] = Handle(handle);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
315 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
316
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
317 if (proc_handles.empty())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
318 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
319
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
320 std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> info = GetSystemHandleInformation();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
321
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
322 for (const auto& h : info) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
323 const pid_t pid = h.UniqueProcessId;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
324 if (!pids.count(pid))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
325 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
326
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
327 if (!IsFileHandle(nullptr, h.ObjectTypeIndex))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
328 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
329
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
330 if (!IsFileMaskOk(h.GrantedAccess))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
331 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
332
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
333 Handle handle(DuplicateHandle(proc_handles[pid].get(), h.HandleValue));
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
334 if (handle.get() == INVALID_HANDLE_VALUE)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
335 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
336
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
337 if (::GetFileType(handle.get()) != FILE_TYPE_DISK)
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
338 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
339
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
340 const std::wstring path = GetFinalPathNameByHandle(handle.get());
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
341 if (!IsFilePathOk(path))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
342 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
343
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
344 if (!open_file_proc({pid, ToUtf8String(path)}))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
345 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
346 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
347
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
348 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
349 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
350
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
351 } // namespace animone::internal::win32