diff 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
line wrap: on
line diff
--- a/dep/animia/src/win/win32.cc	Wed Nov 15 02:34:59 2023 -0500
+++ b/dep/animia/src/win/win32.cc	Wed Nov 15 13:28:18 2023 -0500
@@ -17,14 +17,21 @@
 
 	std::wstring buffer(kMaxSize, L'\0');
 	const auto size = ::GetClassNameW(hwnd, &buffer.front(), buffer.length());
+	/* for some reason GetClassName returns the actual size of the buffer *with* the
+	   terminating NULL byte */
+	buffer.resize(size);
 	return buffer;
 }
 
 static std::wstring GetWindowText(HWND hwnd) {
-	const int size = ::GetWindowTextLengthW(hwnd);
+	const int estimated_size = ::GetWindowTextLengthW(hwnd);
 
-	std::wstring buffer(size, L'\0');
-	::GetWindowTextW(hwnd, &buffer.front(), buffer.length());
+	std::wstring buffer(estimated_size + 1, L'\0');
+	const auto size = ::GetWindowTextW(hwnd, &buffer.front(), buffer.length());
+	/* GetWindowTextLength docs:
+	   "Under certain conditions, the GetWindowTextLength function may return a value
+	    that is larger than the actual length of the text." */
+	buffer.resize(size);
 	return buffer;
 }
 
@@ -55,7 +62,8 @@
 	if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size))
 		return std::wstring();
 
-	return buffer.substr(0, buf_size + 1);
+	buffer.resize(buf_size);
+	return buffer;
 }
 
 ////////////////////////////////////////////////////////////////////////////////