comparison dep/animone/src/util/win32.cc @ 337:a7d4e5107531

dep/animone: REFACTOR ALL THE THINGS 1: animone now has its own syntax divergent from anisthesia, making different platforms actually have their own sections 2: process names in animone are now called `comm' (this will probably break things). this is what its called in bsd/linux so I'm just going to use it everywhere 3: the X11 code now checks for the existence of a UTF-8 window title and passes it if available 4: ANYTHING THATS NOT LINUX IS 100% UNTESTED AND CAN AND WILL BREAK! I still actually need to test the bsd code. to be honest I'm probably going to move all of the bsds into separate files because they're all essentially different operating systems at this point
author Paper <paper@paper.us.eu.org>
date Wed, 19 Jun 2024 12:51:15 -0400
parents a4257370de16
children 74e2365326c6
comparison
equal deleted inserted replaced
336:d260549151d6 337:a7d4e5107531
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::wstring GetProcessPath(DWORD process_id) { 50 std::string GetProcessPath(DWORD process_id) {
51 // If we try to open a SYSTEM process, this function fails and the last error 51 // If we try to open a SYSTEM process, this function fails and the last error
52 // code is ERROR_ACCESS_DENIED. 52 // code is ERROR_ACCESS_DENIED.
53 // 53 //
54 // Note that if we requested PROCESS_QUERY_INFORMATION access right instead 54 // Note that if we requested PROCESS_QUERY_INFORMATION access right instead
55 // of PROCESS_QUERY_LIMITED_INFORMATION, this function would fail when used 55 // of PROCESS_QUERY_LIMITED_INFORMATION, this function would fail when used
66 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions. 66 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions.
67 if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size)) 67 if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size))
68 return std::wstring(); 68 return std::wstring();
69 69
70 buffer.resize(buf_size); 70 buffer.resize(buf_size);
71 return buffer; 71 return ToUtf8String(buffer);
72 } 72 }
73 73
74 std::wstring GetFileNameFromPath(const std::wstring& path) { 74 std::string GetFileNameFromPath(const std::string& path) {
75 const auto pos = path.find_last_of(L"/\\"); 75 const auto pos = path.find_last_of(L"/\\");
76 return pos != std::wstring::npos ? path.substr(pos + 1) : path; 76 return pos != std::wstring::npos ? path.substr(pos + 1) : path;
77 } 77 }
78 78
79 std::wstring GetFileNameWithoutExtension(const std::wstring& filename) {
80 const auto pos = filename.find_last_of(L".");
81 return pos != std::wstring::npos ? filename.substr(0, pos) : filename;
82 }
83
84 static std::wstring GetSystemDirectory() { 79 static std::wstring GetSystemDirectory() {
85 PWSTR path_wch; 80 PWSTR path_wch;
86 SHGetKnownFolderPath(FOLDERID_Windows, 0, NULL, &path_wch); 81 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, SHGFP_TYPE_CURRENT, &path_wch);
87 std::wstring path_wstr(path_wch); 82 std::wstring path_wstr(path_wch);
88 CoTaskMemFree(path_wch); 83 CoTaskMemFree(path_wch);
89 return path_wstr; 84 return path_wstr;
90 } 85 }
91 86
97 ::CharUpperBuffW(&path.front(), path.length()); 92 ::CharUpperBuffW(&path.front(), path.length());
98 93
99 std::wstring windir = GetSystemDirectory(); 94 std::wstring windir = GetSystemDirectory();
100 ::CharUpperBuffW(&windir.front(), windir.length()); 95 ::CharUpperBuffW(&windir.front(), windir.length());
101 96
97 // XXX wtf is 4?
102 return path.find(windir) == 4; 98 return path.find(windir) == 4;
103 } 99 }
104 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
105 } // namespace animone::internal::win32 126 } // namespace animone::internal::win32