comparison dep/animone/src/fd/win32.cc @ 267:09c5bd74fe93

win32: make builds work again
author Paper <paper@paper.us.eu.org>
date Thu, 11 Apr 2024 23:39:18 -0400
parents 862d0d8619f6
children 65df2813d0de
comparison
equal deleted inserted replaced
266:1a6a5d3a94cd 267:09c5bd74fe93
32 * 32-bit PIDs, unlike SystemHandleInformation 32 * 32-bit PIDs, unlike SystemHandleInformation
33 * 33 *
34 * TODO: implement SystemHandleInformation for systems older than XP 34 * TODO: implement SystemHandleInformation for systems older than XP
35 */ 35 */
36 static constexpr SYSTEM_INFORMATION_CLASS SystemExtendedHandleInformation = static_cast<SYSTEM_INFORMATION_CLASS>(0x40); 36 static constexpr SYSTEM_INFORMATION_CLASS SystemExtendedHandleInformation = static_cast<SYSTEM_INFORMATION_CLASS>(0x40);
37 static constexpr SYSTEM_INFORMATION_CLASS SystemHandleInformation = static_cast<SYSTEM_INFORMATION_CLASS>(0x10);
38 static constexpr NTSTATUS STATUS_INFO_LENGTH_MISMATCH = 0xC0000004UL; 37 static constexpr NTSTATUS STATUS_INFO_LENGTH_MISMATCH = 0xC0000004UL;
39 38
40 /* this is filled in at runtime because it's not guaranteed to be (and isn't) 39 /* this is filled in at runtime because it's not guaranteed to be (and isn't)
41 * constant between different versions of Windows */ 40 * constant between different versions of Windows */
42 static unsigned short file_type_index = 0; 41 static unsigned short file_type_index = 0;
68 ::GetProcAddress(ntdll, "NtQuerySystemInformation")); 67 ::GetProcAddress(ntdll, "NtQuerySystemInformation"));
69 nt_query_object = reinterpret_cast<decltype(::NtQueryObject)*>(::GetProcAddress(ntdll, "NtQueryObject")); 68 nt_query_object = reinterpret_cast<decltype(::NtQueryObject)*>(::GetProcAddress(ntdll, "NtQueryObject"));
70 } 69 }
71 70
72 NTSTATUS QuerySystemInformation(SYSTEM_INFORMATION_CLASS cls, PVOID sysinfo, ULONG len, 71 NTSTATUS QuerySystemInformation(SYSTEM_INFORMATION_CLASS cls, PVOID sysinfo, ULONG len,
73 PULONG retlen){return nt_query_system_information(cls, sysinfo, len, retlen)} 72 PULONG retlen){
73 return nt_query_system_information(cls, sysinfo, len, retlen);
74 }
74 75
75 NTSTATUS QueryObject(HANDLE handle, OBJECT_INFORMATION_CLASS cls, PVOID objinf, ULONG objinflen, PULONG retlen) { 76 NTSTATUS QueryObject(HANDLE handle, OBJECT_INFORMATION_CLASS cls, PVOID objinf, ULONG objinflen, PULONG retlen) {
76 return nt_query_object(handle, cls, objinf, objinflen, retlen); 77 return nt_query_object(handle, cls, objinf, objinflen, retlen);
77 } 78 }
78 79
79 private: 80 private:
80 HMODULE ntdll; 81 HMODULE ntdll;
81 decltype(::NtQuerySystemInformation)* nt_query_system_information; 82 decltype(::NtQuerySystemInformation)* nt_query_system_information;
82 decltype(::NtQueryObject)* nt_query_object; 83 decltype(::NtQueryObject)* nt_query_object;
83 84 };
84 }
85 85
86 Ntdll ntdll; 86 Ntdll ntdll;
87 87
88 static HANDLE DuplicateHandle(HANDLE process_handle, HANDLE handle) { 88 static HANDLE DuplicateHandle(HANDLE process_handle, HANDLE handle) {
89 HANDLE dup_handle = nullptr; 89 HANDLE dup_handle = nullptr;
94 94
95 static std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> GetSystemHandleInformation() { 95 static std::vector<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> GetSystemHandleInformation() {
96 /* we should really put a cap on this */ 96 /* we should really put a cap on this */
97 ULONG cb = 1 << 19; 97 ULONG cb = 1 << 19;
98 NTSTATUS status = STATUS_NO_MEMORY; 98 NTSTATUS status = STATUS_NO_MEMORY;
99 std::unique_ptr<SYSTEM_HANDLE_INFORMATION_EX> info(malloc(cb)); 99 std::unique_ptr<SYSTEM_HANDLE_INFORMATION_EX> info;
100 100
101 do { 101 do {
102 info.reset(malloc(cb *= 2)); 102 info.reset(reinterpret_cast<SYSTEM_HANDLE_INFORMATION_EX*>(malloc(cb *= 2)));
103 if (!info) 103 if (!info)
104 continue; 104 continue;
105 105
106 status = ntdll.QuerySystemInformation(SystemExtendedHandleInformation, info.get(), cb, &cb); 106 status = ntdll.QuerySystemInformation(SystemExtendedHandleInformation, info.get(), cb, &cb);
107 } while (status == STATUS_INFO_LENGTH_MISMATCH); 107 } while (status == STATUS_INFO_LENGTH_MISMATCH);
177 177
178 return true; 178 return true;
179 } 179 }
180 180
181 bool GetProcessName(pid_t pid, std::string& name) { 181 bool GetProcessName(pid_t pid, std::string& name) {
182 std::wstring wname = GetProcessPath(); 182 std::wstring wname = GetProcessPath(pid);
183 if (wname.empty()) 183 if (wname.empty())
184 return false; 184 return false;
185 185
186 return ToUtf8String(GetFileNameWithoutExtension(GetFileNameFromPath(wname))); 186 name = ToUtf8String(GetFileNameWithoutExtension(GetFileNameFromPath(wname)));
187 return true;
187 } 188 }
188 189
189 bool EnumerateOpenProcesses(process_proc_t process_proc) { 190 bool EnumerateOpenProcesses(process_proc_t process_proc) {
190 HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 191 HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
191 if (hProcessSnap == INVALID_HANDLE_VALUE) 192 if (hProcessSnap == INVALID_HANDLE_VALUE)