diff --git a/src/pygetwindow/_pygetwindow_win.py b/src/pygetwindow/_pygetwindow_win.py index 0e126d5..ffc5b8d 100644 --- a/src/pygetwindow/_pygetwindow_win.py +++ b/src/pygetwindow/_pygetwindow_win.py @@ -294,10 +294,12 @@ def title(self): """Returns the window title as a string.""" textLenInCharacters = ctypes.windll.user32.GetWindowTextLengthW(self._hWnd) stringBuffer = ctypes.create_unicode_buffer(textLenInCharacters + 1) # +1 for the \0 at the end of the null-terminated string. + ctypes.windll.kernel32.SetLastError(0) ctypes.windll.user32.GetWindowTextW(self._hWnd, stringBuffer, textLenInCharacters + 1) - - # TODO it's ambiguous if an error happened or the title text is just empty. Look into this later. - return stringBuffer.value + if ctypes.windll.kernel32.GetLastError() != 0: + _raiseWithLastError() + else: + return stringBuffer.value @property def visible(self):