comparison dep/animia/src/win/win32.cc @ 153:bd439dd6ffc5

*: make win stuff actually work, rename bsd.cc to xnu.cc It's been OS X only for ages, and these functions are different between most BSDs anyway
author Paper <mrpapersonic@gmail.com>
date Wed, 15 Nov 2023 13:28:18 -0500
parents 8700806c2cc2
children cdf79282d647
comparison
equal deleted inserted replaced
152:8700806c2cc2 153:bd439dd6ffc5
15 // WNDCLASSEX structure 15 // WNDCLASSEX structure
16 constexpr int kMaxSize = 256; 16 constexpr int kMaxSize = 256;
17 17
18 std::wstring buffer(kMaxSize, L'\0'); 18 std::wstring buffer(kMaxSize, L'\0');
19 const auto size = ::GetClassNameW(hwnd, &buffer.front(), buffer.length()); 19 const auto size = ::GetClassNameW(hwnd, &buffer.front(), buffer.length());
20 /* for some reason GetClassName returns the actual size of the buffer *with* the
21 terminating NULL byte */
22 buffer.resize(size);
20 return buffer; 23 return buffer;
21 } 24 }
22 25
23 static std::wstring GetWindowText(HWND hwnd) { 26 static std::wstring GetWindowText(HWND hwnd) {
24 const int size = ::GetWindowTextLengthW(hwnd); 27 const int estimated_size = ::GetWindowTextLengthW(hwnd);
25 28
26 std::wstring buffer(size, L'\0'); 29 std::wstring buffer(estimated_size + 1, L'\0');
27 ::GetWindowTextW(hwnd, &buffer.front(), buffer.length()); 30 const auto size = ::GetWindowTextW(hwnd, &buffer.front(), buffer.length());
31 /* GetWindowTextLength docs:
32 "Under certain conditions, the GetWindowTextLength function may return a value
33 that is larger than the actual length of the text." */
34 buffer.resize(size);
28 return buffer; 35 return buffer;
29 } 36 }
30 37
31 static DWORD GetWindowProcessId(HWND hwnd) { 38 static DWORD GetWindowProcessId(HWND hwnd) {
32 DWORD process_id = 0; 39 DWORD process_id = 0;
53 // Note that this function requires Windows Vista or above. You may use 60 // Note that this function requires Windows Vista or above. You may use
54 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions. 61 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions.
55 if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size)) 62 if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size))
56 return std::wstring(); 63 return std::wstring();
57 64
58 return buffer.substr(0, buf_size + 1); 65 buffer.resize(buf_size);
66 return buffer;
59 } 67 }
60 68
61 //////////////////////////////////////////////////////////////////////////////// 69 ////////////////////////////////////////////////////////////////////////////////
62 70
63 static bool VerifyWindowStyle(HWND hwnd) { 71 static bool VerifyWindowStyle(HWND hwnd) {