I have trying to make wrapper for winapi function GetWindowText.
Function returns std::wstring but i don’t know how to handle where error happen. I return NULL but i know it’s wrong.
std::wstring GetWindowText(HWND handle)
{
const int size = 1024;
TCHAR wnd_text[size] = {0};
HRESULT hr = ::GetWindowText(handle,
wnd_text, size);
if(SUCCEEDED(hr))
return std::wstring(wnd_text);
else
return NULL;
}
As an alternative to exceptions you could also return the string by reference in the argument list and indicate sucess by returning true or false i.e.
Another alternative which avoids the reference argument is to return an instance of a class that wraps a value but also lets you know whether a value is present e.g.
Note that you can template this wrapper pretty easily. Your function would then be