Mercurial > minori
annotate dep/animia/src/fd/win32.cc @ 198:bc1ae1810855
dep/animia: switch from using classes to global functions
the old idea was ok, but sort of hackish; this method doesn't use classes
at all, and this way (especially important!) we can do wayland stuff AND x11
at the same time, which wasn't really possible without stupid workarounds in
the other method
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 24 Dec 2023 02:59:42 -0500 |
parents | c413e475f496 |
children |
rev | line source |
---|---|
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include "animia/fd/win32.h" |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
2 #include "animia/util/win32.h" |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
3 #include "animia.h" |
140
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
4 |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
5 #include <stdexcept> |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
6 #include <string> |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
7 #include <unordered_map> |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
8 #include <vector> |
1e696863b54c
dep/animia: remove superfluous includes
Paper <mrpapersonic@gmail.com>
parents:
139
diff
changeset
|
9 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 #include <fileapi.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 #include <handleapi.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
12 #include <libloaderapi.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
13 #include <ntdef.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
14 #include <psapi.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
15 #include <shlobj.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
16 #include <stringapiset.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
17 #include <tlhelp32.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 #include <windows.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
19 #include <winternl.h> |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
20 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
21 /* This file is noticably more complex than Unix and Linux, and that's because |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
22 * there is no "simple" way to get the paths of a file. In fact, this thing requires |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
23 * you to use *internal functions* that can't even be linked to, hence why we have to |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
24 * use GetProcAddress and such. What a mess. |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
25 * |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
26 * Speaking of which, because this file uses internal functions of the OS, it is not |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
27 * guaranteed to work far into the future. However, it has worked since NT 6.0 (Vista) |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
28 * at least, so it's unlikely to be changed much ever. |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
29 */ |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
31 /* SystemExtendedHandleInformation is only available in NT 5.1+ (XP and higher) and provides information for |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
32 * 32-bit PIDs, unlike SystemHandleInformation |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
156
diff
changeset
|
33 */ |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
34 constexpr SYSTEM_INFORMATION_CLASS SystemExtendedHandleInformation = static_cast<SYSTEM_INFORMATION_CLASS>(0x40); |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
35 constexpr NTSTATUS STATUS_INFO_LENGTH_MISMATCH = 0xC0000004UL; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
36 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
37 /* this is filled in at runtime because it's not guaranteed to be (and isn't) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
38 constant between different versions of Windows */ |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 static unsigned short file_type_index = 0; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
40 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 struct SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 PVOID Object; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 ULONG_PTR UniqueProcessId; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
44 HANDLE HandleValue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 ACCESS_MASK GrantedAccess; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 USHORT CreatorBackTraceIndex; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 USHORT ObjectTypeIndex; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
48 ULONG HandleAttributes; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 ULONG Reserved; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 }; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 struct SYSTEM_HANDLE_INFORMATION_EX { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 ULONG_PTR NumberOfHandles; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 ULONG_PTR Reserved; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX Handles[1]; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 }; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
58 namespace animia::internal::win32 { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
60 static HANDLE DuplicateHandle(HANDLE process_handle, HANDLE handle) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
61 HANDLE dup_handle = nullptr; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
62 const bool result = |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
63 ::DuplicateHandle(process_handle, handle, ::GetCurrentProcess(), &dup_handle, 0, false, DUPLICATE_SAME_ACCESS); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
64 return result ? dup_handle : nullptr; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
65 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
66 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
67 static PVOID GetNTDLLAddress(LPCSTR proc_name) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
68 return reinterpret_cast<PVOID>(::GetProcAddress(::GetModuleHandleA("ntdll.dll"), proc_name)); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
69 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
70 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
71 static NTSTATUS QuerySystemInformation(SYSTEM_INFORMATION_CLASS cls, PVOID sysinfo, ULONG len, PULONG retlen) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
72 static const auto func = |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
73 reinterpret_cast<decltype(::NtQuerySystemInformation)*>(GetNTDLLAddress("NtQuerySystemInformation")); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
74 return func(cls, sysinfo, len, retlen); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
75 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
76 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
77 static NTSTATUS QueryObject(HANDLE handle, OBJECT_INFORMATION_CLASS cls, PVOID objinf, ULONG objinflen, PULONG retlen) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
78 static const auto func = reinterpret_cast<decltype(::NtQueryObject)*>(GetNTDLLAddress("NtQueryObject")); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
79 return func(handle, cls, objinf, objinflen, retlen); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
80 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
81 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
82 static std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> GetSystemHandleInformation() { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
83 std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> res; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
84 /* we should really put a cap on this */ |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
85 ULONG cb = 1 << 19; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
86 |
156
cdf79282d647
dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents:
152
diff
changeset
|
87 for (NTSTATUS status = STATUS_INFO_LENGTH_MISMATCH; status == STATUS_INFO_LENGTH_MISMATCH;) { |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
88 /* why are we doing this? */ |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
89 status = STATUS_NO_MEMORY; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
90 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
91 SYSTEM_HANDLE_INFORMATION_EX* info = (SYSTEM_HANDLE_INFORMATION_EX*)malloc(cb *= 2); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
92 if (!info) |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
93 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
94 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
95 res.reserve(cb); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
96 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
97 status = QuerySystemInformation(SystemExtendedHandleInformation, info, cb, &cb); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
98 if (0 <= status) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
99 ULONG_PTR handles = info->NumberOfHandles; |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
100 if (handles) { |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
101 res.reserve(res.size() + handles); |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
102 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
103 SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX* entry = info->Handles; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
104 do { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
105 if (entry) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
106 res.push_back(*entry); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
107 } while (entry++, --handles); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
108 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
109 } |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
110 |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
111 free(info); |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
112 } |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
113 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
114 return res; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
115 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
116 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
117 static OBJECT_TYPE_INFORMATION QueryObjectTypeInfo(HANDLE handle) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
118 OBJECT_TYPE_INFORMATION info; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
119 QueryObject(handle, ObjectTypeInformation, &info, sizeof(info), NULL); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
120 return info; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
121 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
122 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
123 static std::string GetHandleType(HANDLE handle) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
124 OBJECT_TYPE_INFORMATION info = QueryObjectTypeInfo(handle); |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
125 return ToUtf8String(info.TypeName); |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
126 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
127 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
128 static std::string GetFinalPathNameByHandle(HANDLE handle) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
129 std::wstring buffer; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
130 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
131 int result = ::GetFinalPathNameByHandleW(handle, NULL, 0, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
132 buffer.resize(result); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
133 ::GetFinalPathNameByHandleW(handle, &buffer.front(), buffer.size(), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
134 buffer.resize(buffer.find('\0')); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
135 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
136 return ToUtf8String(buffer); |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
137 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
138 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
139 static bool IsFileHandle(HANDLE handle, unsigned short object_type_index) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
140 if (file_type_index) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
141 return object_type_index == file_type_index; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
142 else if (!handle) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
143 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
144 else if (GetHandleType(handle) == "File") { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
145 file_type_index = object_type_index; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
146 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
147 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
148 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
149 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
150 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
151 static bool IsFileMaskOk(ACCESS_MASK access_mask) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
152 if (!(access_mask & FILE_READ_DATA)) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
153 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
154 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
155 if ((access_mask & FILE_APPEND_DATA) || (access_mask & FILE_WRITE_EA) || (access_mask & FILE_WRITE_ATTRIBUTES)) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
156 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
157 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
158 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
159 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
160 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
161 static bool IsFilePathOk(const std::string& path) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
162 if (path.empty()) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
163 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
164 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
165 if (IsSystemDirectory(path)) |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
166 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
167 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
168 const auto file_attributes = GetFileAttributesA(path.c_str()); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
169 if ((file_attributes == INVALID_FILE_ATTRIBUTES) || (file_attributes & FILE_ATTRIBUTE_DIRECTORY)) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
170 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
171 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
172 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
173 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
174 |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
182
diff
changeset
|
175 bool EnumerateOpenProcesses(process_proc_t process_proc) { |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
176 HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
177 if (hProcessSnap == INVALID_HANDLE_VALUE) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
178 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
179 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
180 PROCESSENTRY32 pe32; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
181 pe32.dwSize = sizeof(PROCESSENTRY32); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
182 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
183 if (!::Process32First(hProcessSnap, &pe32)) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
184 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
185 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
186 if (!process_proc({pe32.th32ProcessID, pe32.szExeFile})) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
187 return false; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
188 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
189 while (::Process32Next(hProcessSnap, &pe32)) |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
190 if (!process_proc({pe32.th32ProcessID, pe32.szExeFile})) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
191 return false; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
192 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
193 ::CloseHandle(hProcessSnap); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
194 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
195 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
196 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
197 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
198 /* this could be changed to being a callback, but... I'm too lazy right now :) */ |
198
bc1ae1810855
dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents:
182
diff
changeset
|
199 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) { |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
200 if (!open_file_proc) |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
201 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
202 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
203 std::unordered_map<pid_t, Handle> proc_handles; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
204 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
205 for (const pid_t& pid : pids) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
206 const HANDLE handle = ::OpenProcess(PROCESS_DUP_HANDLE, false, pid); |
150
ffa535b6d630
*: avoid usage of std::[pair,tuple]
Paper <mrpapersonic@gmail.com>
parents:
148
diff
changeset
|
207 if (handle != INVALID_HANDLE_VALUE) |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
208 proc_handles[pid] = Handle(handle); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
209 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
210 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
211 if (proc_handles.empty()) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
212 return false; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
213 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
214 std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> info = GetSystemHandleInformation(); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
215 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
216 for (const auto& h : info) { |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
217 const pid_t pid = h.UniqueProcessId; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
218 if (!pids.count(pid)) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
219 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
220 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
221 if (!IsFileHandle(nullptr, h.ObjectTypeIndex)) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
222 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
223 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
224 if (!IsFileMaskOk(h.GrantedAccess)) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
225 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
226 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
227 Handle handle(DuplicateHandle(proc_handles[pid].get(), h.HandleValue)); |
150
ffa535b6d630
*: avoid usage of std::[pair,tuple]
Paper <mrpapersonic@gmail.com>
parents:
148
diff
changeset
|
228 if (handle.get() == INVALID_HANDLE_VALUE) |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
229 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
230 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
231 if (GetFileType(handle.get()) != FILE_TYPE_DISK) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
232 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
233 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
234 const std::string path = GetFinalPathNameByHandle(handle.get()); |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
235 if (!IsFilePathOk(path)) |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
236 continue; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
237 |
152
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
238 if (!open_file_proc({pid, path})) |
8700806c2cc2
dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
239 return false; |
138
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
240 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
241 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
242 return true; |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
243 } |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
244 |
28842a8d0c6b
dep/animia: huge refactor (again...)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
245 } // namespace animia::internal::win32 |