[SDK][ATL] Fix CWindow::GetWindowText method of BSTR (#3498)
authorKatayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
Tue, 2 Mar 2021 08:50:00 +0000 (17:50 +0900)
committerGitHub <noreply@github.com>
Tue, 2 Mar 2021 08:50:00 +0000 (17:50 +0900)
- Fix generic text mapping for GetWindowText and GetWindowTextLength functions.
- Fix the position.
- Fix the length.
- Fail elegantly if necessary.

CORE-9281

sdk/lib/atl/atlwin.h

index d899498..2df36b6 100644 (file)
@@ -782,11 +782,14 @@ public:
     BOOL GetWindowText(BSTR& bstrText)
     {
         ATLASSERT(::IsWindow(m_hWnd));
-        int length = ::GetWindowTextLength(m_hWnd);
-        if (!SysReAllocStringLen(&bstrText, NULL, length))
+        INT length = ::GetWindowTextLengthW(m_hWnd);
+        if (!::SysReAllocStringLen(&bstrText, NULL, length))
             return FALSE;
-        ::GetWindowText(m_hWnd, (LPTSTR)&bstrText[2], length);
-        return TRUE;
+        if (::GetWindowTextW(m_hWnd, bstrText, length + 1))
+            return TRUE;
+        ::SysFreeString(bstrText);
+        bstrText = NULL;
+        return FALSE;
     }
 
     int GetWindowTextLength() const