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.
bool GetWindowText(HWND handle, std::wstring& windowText)
{
const int size = 1024;
TCHAR wnd_text[size] = {0};
HRESULT hr = ::GetWindowText(handle,
wnd_text, size);
if(SUCCEEDED(hr))
{
windowText = wnd_text;
return true;
}
else
return false;
}
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.
class ValueWrapper
{
public:
ValueWrapper() : present( false ) {}
ValueWrapper( const std::wstring& s ) : value( s ), present( true ) {}
bool isPresent() const { return present; }
const std::wstring& getValue() const { return value; }
private:
std::wstring value;
bool present;
};
Note that you can template this wrapper pretty easily. Your function would then be
ValueWrapper GetWindowText(HWND handle)
{
const int size = 1024;
TCHAR wnd_text[size] = {0};
HRESULT hr = ::GetWindowText(handle,
wnd_text, size);
if(SUCCEEDED(hr))
return ValueWrapper( wnd_text );
else
return ValueWrapper();
}