comparison dep/animone/src/util/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
comparison
equal deleted inserted replaced
339:eac06513db86 340:74e2365326c6
45 std::wstring ret(size, L'\0'); 45 std::wstring ret(size, L'\0');
46 mbtowc(&ret.front(), ret.length()); 46 mbtowc(&ret.front(), ret.length());
47 return ret; 47 return ret;
48 } 48 }
49 49
50 std::string GetProcessPath(DWORD process_id) {
51 // If we try to open a SYSTEM process, this function fails and the last error
52 // code is ERROR_ACCESS_DENIED.
53 //
54 // Note that if we requested PROCESS_QUERY_INFORMATION access right instead
55 // of PROCESS_QUERY_LIMITED_INFORMATION, this function would fail when used
56 // to open an elevated process.
57 Handle process_handle(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_id));
58
59 if (!process_handle)
60 return std::wstring();
61
62 std::wstring buffer(MAX_PATH, L'\0');
63 DWORD buf_size = buffer.length();
64
65 // Note that this function requires Windows Vista or above. You may use
66 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions.
67 if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size))
68 return std::wstring();
69
70 buffer.resize(buf_size);
71 return ToUtf8String(buffer);
72 }
73
74 std::string GetFileNameFromPath(const std::string& path) {
75 const auto pos = path.find_last_of(L"/\\");
76 return pos != std::wstring::npos ? path.substr(pos + 1) : path;
77 }
78
79 static std::wstring GetSystemDirectory() {
80 PWSTR path_wch;
81 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, SHGFP_TYPE_CURRENT, &path_wch);
82 std::wstring path_wstr(path_wch);
83 CoTaskMemFree(path_wch);
84 return path_wstr;
85 }
86
87 bool IsSystemDirectory(const std::string& path) {
88 return IsSystemDirectory(ToWstring(path));
89 }
90
91 bool IsSystemDirectory(std::wstring path) {
92 ::CharUpperBuffW(&path.front(), path.length());
93
94 std::wstring windir = GetSystemDirectory();
95 ::CharUpperBuffW(&windir.front(), windir.length());
96
97 // XXX wtf is 4?
98 return path.find(windir) == 4;
99 }
100
101 bool VerifyProcessPath(const std::string& path) {
102 return !path.empty() && !IsSystemDirectory(path);
103 }
104
105 bool VerifyProcessFileName(const std::string& name) {
106 static const std::set<std::string> invalid_names = {
107 // System files
108 "explorer.exe", // Windows Explorer
109 "taskeng.exe", // Task Scheduler Engine
110 "taskhost.exe", // Host Process for Windows Tasks
111 "taskhostex.exe", // Host Process for Windows Tasks
112 "taskmgr.exe", // Task Manager
113 "services.exe", // Service Control Manager
114 };
115
116 if (name.empty())
117 return false;
118
119 for (const auto& invalid_name : invalid_names)
120 if (util::EqualStrings(name, invalid_name))
121 return false;
122
123 return true;
124 }
125
126 } // namespace animone::internal::win32 50 } // namespace animone::internal::win32