The best idea is to wrap GetWindowText in a function that first calls GetWindowTextLength (I think it's called), allocates a std::vector of appropriate size, calls GetWindowText to read the text into that buffer and then returns a string that is a copy of the contents of the vector:
Code:
template<typename Ch> void getWindowText(HWND hwnd, Ch *buffer, std::size_t len);
template<> void getWindowText<char>(HWND hwnd, char *buffer, std::size_t len)
{
return ::GetWindowTextA(hwnd, buffer, len);
}
template<> void getWindowText<wchar_t>(HWND hwnd, wchar_t *buffer, std::size_t len)
{
return ::GetWindowTextW(hwnd, buffer, len);
}
template<typename Ch> std::basic_string<Ch> getWindowText(HWND hwnd)
{
std::size_t len = ::GetWindowTextLength(hwnd);
std::vector<Ch> buffer(len);
getWindowText(hwnd, buffer, len);
return std::basic_string<Ch>(buffer.begin(), buffer.end());
}
Syntax errors possible.