Try this:
std::basic_string<TCHAR> MainClass::GetString()
{
TCHAR name[256] = {0};
int len = GetWindowText(hWnd, name, 256);
return std::basic_string<TCHAR>(name, len);
}
Or this:
std::basic_string<TCHAR> MainClass::GetString()
{
int len = GetWindowTextLength(hWnd);
if (len == 0) return std::basic_string<TCHAR>();
std::vector<TCHAR> name(len+1);
len = GetWindowText(hWnd, &name[0], name.size());
return std::basic_string<TCHAR>(&name[0], len);
}
Either way, then you can do this:
std::basic_string<TCHAR> name = GetString();
Alternatively:
int MainClass::GetString(LPTSTR name, int maxlen)
{
return GetWindowText(hWnd, name, maxlen);
}
TCHAR name[256];
int namelen = GetString(name, 256);
3
solved Function doesn’t return correct TCHAR [closed]