Skip to content

Instantly share code, notes, and snippets.

@pravic
Created October 8, 2013 14:02
Show Gist options
  • Save pravic/6885178 to your computer and use it in GitHub Desktop.
Save pravic/6885178 to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
// 1. std::basic_string have continuos storage
std::wstring GetWindowTextW(HWND hWnd)
{
std::wstring text(::GetWindowTextLengthW(hWnd), L'\0');
size_t len = ::GetWindowTextW(hWnd, &text[0], text.size());
text.resize(len);
return std::move(text);
}
// 2. std::basic_string doesn't have continuos storage
std::wstring GetWindowTextW(HWND hWnd)
{
std::vector<wchar_t> text(::GetWindowTextLengthW(hWnd));
size_t len = ::GetWindowTextW(hWnd, &text[0], text.size());
text.resize(len);
return std::wstring(&text[0], text.size());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment