diff 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
line wrap: on
line diff
--- a/dep/animone/src/fd/win32.cc	Wed Jun 19 23:21:19 2024 -0400
+++ b/dep/animone/src/fd/win32.cc	Thu Jun 20 03:03:05 2024 -0400
@@ -12,6 +12,7 @@
  * then they're pretty much forced to keeping this the same anyway.
  */
 #include "animone/fd/win32.h"
+#include "animone/util.h"
 #include "animone.h"
 #include "animone/util/win32.h"
 
@@ -19,6 +20,7 @@
 #include <string>
 #include <unordered_map>
 #include <vector>
+#include <optional>
 
 #include <fileapi.h>
 #include <handleapi.h>
@@ -141,19 +143,9 @@
 /* ------------------------------------------------------------------- */
 
 static bool GetSystemDirectory(std::wstring& str) {
-	PWSTR path_wch;
-
-	if (FAILED(::SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, SHGFP_TYPE_CURRENT, &path_wch)))
-		return false;
-
-	str.assign(path_wch);
+	str.assign(MAX_PATH, '\0');
 
-	::CoTaskMemFree(path_wch);
-	return true;
-}
-
-static bool IsSystemDirectory(const std::string& path) {
-	return IsSystemDirectory(ToWstring(path));
+	return SUCCEEDED(::SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, SHGFP_TYPE_CURRENT, &str.front()));
 }
 
 static bool IsSystemDirectory(std::wstring path) {
@@ -168,6 +160,10 @@
 	return path.find(windir) == 4;
 }
 
+static bool IsSystemDirectory(const std::string& path) {
+	return IsSystemDirectory(ToWstring(path));
+}
+
 static bool IsFileHandle(HANDLE handle, unsigned short object_type_index) {
 	/* this is filled in at runtime because it's not guaranteed to be (and isn't)
 	 * constant between different versions of Windows */
@@ -179,7 +175,7 @@
 		/* XXX what? */
 		return true;
 	} else if (GetHandleType(handle) == L"File") {
-		file_type_index.reset(object_type_index);
+		file_type_index = object_type_index;
 		return true;
 	}
 
@@ -212,7 +208,7 @@
 
 /* ------------------------------------------------------------------- */
 
-static std::string GetProcessPath(DWORD process_id) {
+static bool GetProcessPath(DWORD process_id, std::string& path) {
 	// If we try to open a SYSTEM process, this function fails and the last error
 	// code is ERROR_ACCESS_DENIED.
 	//
@@ -222,7 +218,7 @@
 	Handle process_handle(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_id));
 
 	if (!process_handle)
-		return std::wstring();
+		return false;
 
 	std::wstring buffer(MAX_PATH, L'\0');
 	DWORD buf_size = buffer.length();
@@ -230,14 +226,15 @@
 	// Note that this function requires Windows Vista or above. You may use
 	// GetProcessImageFileName or GetModuleFileNameEx on earlier versions.
 	if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size))
-		return std::wstring();
+		return false;
 
 	buffer.resize(buf_size);
-	return ToUtf8String(buffer);
+	path = ToUtf8String(buffer);
+	return true;
 }
 
 static std::string GetFilenameFromPath(const std::string& path) {
-	const auto pos = path.find_last_of(L"/\\");
+	const auto pos = path.find_last_of("/\\");
 	return pos != std::wstring::npos ? path.substr(pos + 1) : path;
 }
 
@@ -270,11 +267,10 @@
 /* extern functions */
 
 bool GetProcessName(pid_t pid, std::string& name) {
-	std::string path = GetProcessPath(pid);
-	if (path.empty() || !VerifyProcessPath(path))
+	if (!GetProcessPath(pid, name) || !VerifyProcessPath(name))
 		return false;
 
-	name = GetFilenameFromPath(path);
+	name = GetFilenameFromPath(name);
 	if (!VerifyProcessFilename(name))
 		return false;
 
@@ -297,7 +293,7 @@
 		if (!GetProcessName(pe32.th32ProcessID, name))
 			continue;
 
-		if (!process_proc({.platform = ExecutablePlatform::Win32, .pid = pe32.th32ProcessID, .comm = name}))
+		if (!process_proc({.pid = pe32.th32ProcessID, .platform = ExecutablePlatform::Win32, .comm = name}))
 			return false;
 	} while (::Process32Next(process_snap.get(), &pe32));