Mercurial > minori
annotate dep/animone/src/util/win32.cc @ 306:f4538a4c91ba
cleanup: remove extraneous executable flags
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 19 May 2024 17:48:52 -0400 |
parents | b1f625b0227c |
children | a4257370de16 |
rev | line source |
---|---|
301
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
1 #include "animone/util/win32.h" |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
2 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
3 #include <shlobj.h> /* SHGetKnownFolderPath */ |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
4 #include <subauth.h> /* UNICODE_STRING */ |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
5 #include <windows.h> |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
6 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
7 namespace animone::internal::win32 { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
8 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
9 std::string ToUtf8String(const std::wstring& string) { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
10 if (string.empty()) |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
11 return std::string(); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
12 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
13 long size = ::WideCharToMultiByte(CP_UTF8, 0, string.c_str(), string.length(), nullptr, 0, nullptr, nullptr); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
14 std::string ret(size, '\0'); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
15 ::WideCharToMultiByte(CP_UTF8, 0, string.c_str(), string.length(), &ret.front(), ret.length(), nullptr, nullptr); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
16 return ret; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
17 } |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
18 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
19 std::string ToUtf8String(const UNICODE_STRING& string) { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
20 const auto wctomb = [&string](LPSTR out, int size) -> int { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
21 return ::WideCharToMultiByte(CP_UTF8, 0, string.Buffer, string.Length, out, size, nullptr, nullptr); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
22 }; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
23 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
24 if (string.Length <= 0) |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
25 return std::string(); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
26 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
27 long size = wctomb(nullptr, 0); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
28 std::string ret(size, '\0'); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
29 wctomb(&ret.front(), ret.length()); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
30 return ret; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
31 } |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
32 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
33 std::wstring ToWstring(const std::string& string) { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
34 const auto mbtowc = [&string](LPWSTR out, int size) -> int { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
35 return ::MultiByteToWideChar(CP_UTF8, 0, string.c_str(), string.length(), out, size); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
36 }; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
37 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
38 if (string.empty()) |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
39 return std::wstring(); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
40 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
41 long size = mbtowc(nullptr, 0); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
42 std::wstring ret(size, L'\0'); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
43 mbtowc(&ret.front(), ret.length()); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
44 return ret; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
45 } |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
46 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
47 std::wstring GetProcessPath(DWORD process_id) { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
48 // If we try to open a SYSTEM process, this function fails and the last error |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
49 // code is ERROR_ACCESS_DENIED. |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
50 // |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
51 // Note that if we requested PROCESS_QUERY_INFORMATION access right instead |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
52 // of PROCESS_QUERY_LIMITED_INFORMATION, this function would fail when used |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
53 // to open an elevated process. |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
54 Handle process_handle(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_id)); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
55 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
56 if (!process_handle) |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
57 return std::wstring(); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
58 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
59 std::wstring buffer(MAX_PATH, L'\0'); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
60 DWORD buf_size = buffer.length(); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
61 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
62 // Note that this function requires Windows Vista or above. You may use |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
63 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions. |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
64 if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size)) |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
65 return std::wstring(); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
66 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
67 buffer.resize(buf_size); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
68 return buffer; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
69 } |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
70 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
71 std::wstring GetFileNameFromPath(const std::wstring& path) { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
72 const auto pos = path.find_last_of(L"/\\"); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
73 return pos != std::wstring::npos ? path.substr(pos + 1) : path; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
74 } |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
75 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
76 std::wstring GetFileNameWithoutExtension(const std::wstring& filename) { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
77 const auto pos = filename.find_last_of(L"."); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
78 return pos != std::wstring::npos ? filename.substr(0, pos) : filename; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
79 } |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
80 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
81 static std::wstring GetSystemDirectory() { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
82 PWSTR path_wch; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
83 SHGetKnownFolderPath(FOLDERID_Windows, 0, NULL, &path_wch); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
84 std::wstring path_wstr(path_wch); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
85 CoTaskMemFree(path_wch); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
86 return path_wstr; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
87 } |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
88 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
89 bool IsSystemDirectory(const std::string& path) { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
90 return IsSystemDirectory(ToWstring(path)); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
91 } |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
92 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
93 bool IsSystemDirectory(std::wstring path) { |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
94 ::CharUpperBuffW(&path.front(), path.length()); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
95 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
96 std::wstring windir = GetSystemDirectory(); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
97 ::CharUpperBuffW(&windir.front(), windir.length()); |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
98 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
99 return path.find(windir) == 4; |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
100 } |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
101 |
b1f625b0227c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
102 } // namespace animone::internal::win32 |