annotate dep/animone/src/fd/win32.cc @ 342:adb79bdde329

dep/animone: fix tons of issues for example, the window ID stuff was just... completely wrong. since we're supporting multiple different window systems, it *has* to be a union rather than just a single integer type. HWND is also not a DWORD, it's a pointer(!), so now it's stored as a std::uintptr_t. (this probably breaks things)
author Paper <paper@paper.us.eu.org>
date Thu, 20 Jun 2024 03:03:05 -0400
parents 74e2365326c6
children 1faa72660932
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) {
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
146 str.assign(MAX_PATH, '\0');
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
147
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
148 return SUCCEEDED(::SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, SHGFP_TYPE_CURRENT, &str.front()));
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
149 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
150
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
151 static bool IsSystemDirectory(std::wstring path) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
152 std::wstring windir;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
153 if (!GetSystemDirectory(windir))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
154 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
155
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
156 ::CharUpperBuffW(&path.front(), path.length());
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
157 ::CharUpperBuffW(&windir.front(), windir.length());
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
158
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
159 // XXX wtf is 4?
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
160 return path.find(windir) == 4;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
161 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
162
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
163 static bool IsSystemDirectory(const std::string& path) {
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
164 return IsSystemDirectory(ToWstring(path));
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
165 }
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
166
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
167 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
168 /* 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
169 * constant between different versions of Windows */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
170 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
171
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
172 if (file_type_index.has_value()) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
173 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
174 } else if (!handle) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
175 /* XXX what? */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
176 return true;
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
177 } else if (GetHandleType(handle) == L"File") {
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
178 file_type_index = object_type_index;
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
179 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
180 }
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
181
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
182 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
183 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
184
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
185 static bool IsFileMaskOk(ACCESS_MASK access_mask) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
186 if (!(access_mask & FILE_READ_DATA))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
187 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
188
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
189 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
190 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
191
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
192 return true;
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
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
195 static bool IsFilePathOk(const std::wstring& path) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
196 if (path.empty())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
197 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
198
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
199 if (IsSystemDirectory(path))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
200 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
201
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
202 const auto file_attributes = GetFileAttributesW(path.c_str());
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
203 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
204 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
205
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
206 return true;
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
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
209 /* ------------------------------------------------------------------- */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
210
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
211 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
212 // 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
213 // code is ERROR_ACCESS_DENIED.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
214 //
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
215 // 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
216 // 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
217 // to open an elevated process.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
218 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
219
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
220 if (!process_handle)
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
221 return false;
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
222
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
223 std::wstring buffer(MAX_PATH, L'\0');
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
224 DWORD buf_size = buffer.length();
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
225
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
226 // 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
227 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
228 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
229 return false;
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
230
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
231 buffer.resize(buf_size);
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
232 path = ToUtf8String(buffer);
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
233 return true;
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
234 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
235
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
236 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
237 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
238 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
239 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
240
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
241 static bool VerifyProcessPath(const std::string& path) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
242 return !path.empty() && !IsSystemDirectory(path);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
243 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
244
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
245 static bool VerifyProcessFilename(const std::string& name) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
246 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
247 // System files
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
248 "explorer.exe", // Windows Explorer
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
249 "taskeng.exe", // Task Scheduler Engine
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
250 "taskhost.exe", // Host Process for Windows Tasks
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
251 "taskhostex.exe", // Host Process for Windows Tasks
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
252 "taskmgr.exe", // Task Manager
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
253 "services.exe", // Service Control Manager
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
254 };
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
255
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
256 if (name.empty())
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
257 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
258
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
259 for (const auto& invalid_name : invalid_names)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
260 if (util::EqualStrings(name, invalid_name))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
261 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
262
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
263 return true;
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
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 /* extern functions */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
268
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
269 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
270 if (!GetProcessPath(pid, name) || !VerifyProcessPath(name))
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
271 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
272
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
273 name = GetFilenameFromPath(name);
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
274 if (!VerifyProcessFilename(name))
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
275 return false;
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
276
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
277 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
278 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
279
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
280 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
281 Handle process_snap(::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
282 if (process_snap.get() == INVALID_HANDLE_VALUE)
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
283 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
284
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
285 PROCESSENTRY32 pe32;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
286 pe32.dwSize = sizeof(PROCESSENTRY32);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
287
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
288 if (!::Process32First(process_snap.get(), &pe32))
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
289 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
290
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
291 do {
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
292 std::string name;
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
293 if (!GetProcessName(pe32.th32ProcessID, name))
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
294 continue;
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
295
342
adb79bdde329 dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
296 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
297 return false;
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
298 } while (::Process32Next(process_snap.get(), &pe32));
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
299
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
300 return true;
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
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
303 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
304 if (!open_file_proc)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
305 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
306
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
307 std::unordered_map<pid_t, Handle> proc_handles;
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 for (const pid_t& pid : pids) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
310 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
311 if (handle != INVALID_HANDLE_VALUE)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
312 proc_handles[pid] = Handle(handle);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
313 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
314
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
315 if (proc_handles.empty())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
316 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
317
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
318 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
319
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
320 for (const auto& h : info) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
321 const pid_t pid = h.UniqueProcessId;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
322 if (!pids.count(pid))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
323 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
324
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
325 if (!IsFileHandle(nullptr, h.ObjectTypeIndex))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
326 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
327
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
328 if (!IsFileMaskOk(h.GrantedAccess))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
329 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
330
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
331 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
332 if (handle.get() == INVALID_HANDLE_VALUE)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
333 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
334
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
335 if (::GetFileType(handle.get()) != FILE_TYPE_DISK)
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
336 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
337
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
338 const std::wstring path = GetFinalPathNameByHandle(handle.get());
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
339 if (!IsFilePathOk(path))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
340 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
341
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
342 if (!open_file_proc({pid, ToUtf8String(path)}))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
343 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
344 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
345
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
346 return true;
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
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
349 } // namespace animone::internal::win32