annotate dep/animone/src/fd/win32.cc @ 340:74e2365326c6

dep/animone: add experimental accessibility strategy I also moved most of the functions out of util/win32.cc, because that file is meant for things that are shared between the different functions, and currently that is only wide string conversion helpers.
author Paper <paper@paper.us.eu.org>
date Wed, 19 Jun 2024 23:13:55 -0400
parents a7d4e5107531
children adb79bdde329
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"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
15 #include "animone.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
16 #include "animone/util/win32.h"
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
17
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
18 #include <stdexcept>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
19 #include <string>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
20 #include <unordered_map>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
21 #include <vector>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
22
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
23 #include <fileapi.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
24 #include <handleapi.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
25 #include <libloaderapi.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
26 #include <ntdef.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
27 #include <psapi.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
28 #include <shlobj.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
29 #include <stringapiset.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
30 #include <tlhelp32.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
31 #include <windows.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
32 #include <winternl.h>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
33
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
34 /* 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
35 * 32-bit PIDs, unlike SystemHandleInformation */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
36 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
37 static constexpr NTSTATUS STATUS_INFO_LENGTH_MISMATCH = 0xC0000004UL;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
38
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
39 struct SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
40 PVOID Object;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
41 ULONG_PTR UniqueProcessId;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
42 HANDLE HandleValue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
43 ACCESS_MASK GrantedAccess;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
44 USHORT CreatorBackTraceIndex;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
45 USHORT ObjectTypeIndex;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
46 ULONG HandleAttributes;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
47 ULONG Reserved;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
48 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
49
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
50 struct SYSTEM_HANDLE_INFORMATION_EX {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
51 ULONG_PTR NumberOfHandles;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
52 ULONG_PTR Reserved;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
53 SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX Handles[1];
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
54 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
55
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
56 namespace animone::internal::win32 {
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 class Ntdll {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
59 public:
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
60 Ntdll() {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
61 ntdll = ::GetModuleHandleW(L"ntdll.dll");
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
62 nt_query_system_information = reinterpret_cast<decltype(::NtQuerySystemInformation)*>(
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
63 ::GetProcAddress(ntdll, "NtQuerySystemInformation"));
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
64 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
65 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
66
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
67 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
68 PULONG retlen){
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
69 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
70 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
71
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
72 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
73 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
74 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
75
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
76 private:
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
77 HMODULE ntdll;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
78 decltype(::NtQuerySystemInformation)* nt_query_system_information;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
79 decltype(::NtQueryObject)* nt_query_object;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
80 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
81
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
82 Ntdll ntdll;
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 static HANDLE DuplicateHandle(HANDLE process_handle, HANDLE handle) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
85 HANDLE dup_handle = nullptr;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
86 const bool result =
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
87 ::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
88 return result ? dup_handle : nullptr;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
89 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
90
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
91 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
92 /* we should really put a cap on this */
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
93 ULONG cb = 1 << 19;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
94 NTSTATUS status = STATUS_NO_MEMORY;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
95 std::unique_ptr<SYSTEM_HANDLE_INFORMATION_EX> info;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
96
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
97 do {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
98 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
99 if (!info)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
100 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
101
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
102 status = ntdll.QuerySystemInformation(SystemExtendedHandleInformation, info.get(), cb, &cb);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
103 } while (status == STATUS_INFO_LENGTH_MISMATCH);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
104
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
105 if (!NT_SUCCESS(status))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
106 return {};
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
107
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
108 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
109
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
110 ULONG_PTR handles = info->NumberOfHandles;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
111 if (!handles)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
112 return {};
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
113
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
114 res.reserve(handles);
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 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
117 do {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
118 if (entry)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
119 res.push_back(*(entry++));
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
120 } while (--handles);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
121
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
122 return res;
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
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
125 static std::wstring GetHandleType(HANDLE handle) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
126 OBJECT_TYPE_INFORMATION info = {0};
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
127 ntdll.QueryObject(handle, ObjectTypeInformation, &info, sizeof(info), NULL);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
128 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
129 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
130
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
131 static std::wstring GetFinalPathNameByHandle(HANDLE handle) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
132 std::wstring buffer;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
133
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
134 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
135 buffer.resize(size);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
136 ::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
137
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
138 return buffer;
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
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
141 /* ------------------------------------------------------------------- */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
142
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
143 static bool GetSystemDirectory(std::wstring& str) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
144 PWSTR path_wch;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
145
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
146 if (FAILED(::SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, SHGFP_TYPE_CURRENT, &path_wch)))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
147 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
148
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
149 str.assign(path_wch);
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 ::CoTaskMemFree(path_wch);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
152 return true;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
153 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
154
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
155 static bool IsSystemDirectory(const std::string& path) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
156 return IsSystemDirectory(ToWstring(path));
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
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
159 static bool IsSystemDirectory(std::wstring path) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
160 std::wstring windir;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
161 if (!GetSystemDirectory(windir))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
162 return false;
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 ::CharUpperBuffW(&path.front(), path.length());
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
165 ::CharUpperBuffW(&windir.front(), windir.length());
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
166
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
167 // XXX wtf is 4?
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
168 return path.find(windir) == 4;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
169 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
170
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
171 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
172 /* 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
173 * constant between different versions of Windows */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
174 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
175
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
176 if (file_type_index.has_value()) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
177 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
178 } else if (!handle) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
179 /* XXX what? */
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
180 return true;
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
181 } else if (GetHandleType(handle) == L"File") {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
182 file_type_index.reset(object_type_index);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
183 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
184 }
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
185
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
186 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
187 }
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 static bool IsFileMaskOk(ACCESS_MASK access_mask) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
190 if (!(access_mask & FILE_READ_DATA))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
191 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
192
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
193 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
194 return false;
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 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
197 }
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 static bool IsFilePathOk(const std::wstring& path) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
200 if (path.empty())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
201 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
202
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
203 if (IsSystemDirectory(path))
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 const auto file_attributes = GetFileAttributesW(path.c_str());
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
207 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
208 return false;
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 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
211 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
212
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
213 /* ------------------------------------------------------------------- */
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 static std::string GetProcessPath(DWORD process_id) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
216 // 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
217 // code is ERROR_ACCESS_DENIED.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
218 //
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
219 // 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
220 // 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
221 // to open an elevated process.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
222 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
223
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
224 if (!process_handle)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
225 return std::wstring();
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
226
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
227 std::wstring buffer(MAX_PATH, L'\0');
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
228 DWORD buf_size = buffer.length();
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
229
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
230 // 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
231 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
232 if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
233 return std::wstring();
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 buffer.resize(buf_size);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
236 return ToUtf8String(buffer);
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
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
239 static std::string GetFilenameFromPath(const std::string& path) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
240 const auto pos = path.find_last_of(L"/\\");
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
241 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
242 }
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 static bool VerifyProcessPath(const std::string& path) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
245 return !path.empty() && !IsSystemDirectory(path);
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
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
248 static bool VerifyProcessFilename(const std::string& name) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
249 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
250 // System files
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
251 "explorer.exe", // Windows Explorer
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
252 "taskeng.exe", // Task Scheduler Engine
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
253 "taskhost.exe", // Host Process for Windows Tasks
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
254 "taskhostex.exe", // Host Process for Windows Tasks
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
255 "taskmgr.exe", // Task Manager
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
256 "services.exe", // Service Control Manager
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
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
259 if (name.empty())
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
260 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
261
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
262 for (const auto& invalid_name : invalid_names)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
263 if (util::EqualStrings(name, invalid_name))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
264 return false;
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 return true;
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 /* ------------------------------------------------------------------- */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
270 /* extern functions */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
271
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
272 bool GetProcessName(pid_t pid, std::string& name) {
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
273 std::string path = GetProcessPath(pid);
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
274 if (path.empty() || !VerifyProcessPath(path))
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
275 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
276
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
277 name = GetFilenameFromPath(path);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
278 if (!VerifyProcessFilename(name))
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
279 return false;
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
280
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
281 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
282 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
283
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
284 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
285 Handle process_snap(::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
286 if (process_snap.get() == INVALID_HANDLE_VALUE)
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
287 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
288
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
289 PROCESSENTRY32 pe32;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
290 pe32.dwSize = sizeof(PROCESSENTRY32);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
291
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
292 if (!::Process32First(process_snap.get(), &pe32))
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
293 return false;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
294
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
295 do {
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
296 std::string name;
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
297 if (!GetProcessName(pe32.th32ProcessID, name))
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
298 continue;
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
299
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
300 if (!process_proc({.platform = ExecutablePlatform::Win32, .pid = pe32.th32ProcessID, .comm = name}))
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
301 return false;
337
a7d4e5107531 dep/animone: REFACTOR ALL THE THINGS
Paper <paper@paper.us.eu.org>
parents: 310
diff changeset
302 } while (::Process32Next(process_snap.get(), &pe32));
301
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 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
305 }
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 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
308 if (!open_file_proc)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
309 return false;
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 std::unordered_map<pid_t, Handle> proc_handles;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
312
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
313 for (const pid_t& pid : pids) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
314 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
315 if (handle != INVALID_HANDLE_VALUE)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
316 proc_handles[pid] = Handle(handle);
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
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
319 if (proc_handles.empty())
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
320 return false;
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 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
323
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
324 for (const auto& h : info) {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
325 const pid_t pid = h.UniqueProcessId;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
326 if (!pids.count(pid))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
327 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
328
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
329 if (!IsFileHandle(nullptr, h.ObjectTypeIndex))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
330 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
331
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
332 if (!IsFileMaskOk(h.GrantedAccess))
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
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
335 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
336 if (handle.get() == INVALID_HANDLE_VALUE)
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
337 continue;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
338
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents: 337
diff changeset
339 if (::GetFileType(handle.get()) != FILE_TYPE_DISK)
301
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 const std::wstring path = GetFinalPathNameByHandle(handle.get());
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
343 if (!IsFilePathOk(path))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
344 continue;
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 if (!open_file_proc({pid, ToUtf8String(path)}))
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
347 return false;
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
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
350 return true;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
351 }
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
352
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 285
diff changeset
353 } // namespace animone::internal::win32