@@ -22,8 +22,7 @@ std::vector<WindowHandle> getWindows() {
2222}
2323
2424WindowHandle getActiveWindow () {
25- HWND foregroundWindow = GetForegroundWindow ();
26- if (foregroundWindow != NULL ) {
25+ if (IsWindow (GetForegroundWindow ())) {
2726 return reinterpret_cast <WindowHandle>(foregroundWindow);
2827 }
2928 return -1 ;
@@ -32,19 +31,21 @@ WindowHandle getActiveWindow() {
3231MMRect getWindowRect (const WindowHandle windowHandle) {
3332 HWND hWnd = reinterpret_cast <HWND>(windowHandle);
3433 RECT windowRect;
35- if (GetWindowRect (hWnd, &windowRect)) {
34+ if (IsWindow (hWnd) && GetWindowRect (hWnd, &windowRect)) {
3635 return MMRectMake (windowRect.left , windowRect.top , windowRect.right - windowRect.left , windowRect.bottom - windowRect.top );
3736 }
3837 return MMRectMake (0 , 0 , 0 , 0 );
3938}
4039
4140std::string getWindowTitle (const WindowHandle windowHandle) {
4241 HWND hWnd = reinterpret_cast <HWND>(windowHandle);
43- auto BUFFER_SIZE = GetWindowTextLength (hWnd) + 1 ;
44- if (BUFFER_SIZE) {
45- LPSTR windowTitle = new CHAR[BUFFER_SIZE];
46- if (GetWindowText (hWnd, windowTitle, BUFFER_SIZE)) {
47- return std::string (windowTitle);
42+ if (IsWindow (hWnd)) {
43+ auto BUFFER_SIZE = GetWindowTextLength (hWnd) + 1 ;
44+ if (BUFFER_SIZE) {
45+ LPSTR windowTitle = new CHAR[BUFFER_SIZE];
46+ if (GetWindowText (hWnd, windowTitle, BUFFER_SIZE)) {
47+ return std::string (windowTitle);
48+ }
4849 }
4950 }
5051 return " " ;
0 commit comments