https://git.reactos.org/?p=reactos.git;a=commitdiff;h=0cd80c17f61d374ffd08d…
commit 0cd80c17f61d374ffd08d8d1a157d5e8c3e3cc89
Author:     Katayama Hirofumi MZ <katayama.hirofumi.mz(a)gmail.com>
AuthorDate: Mon Feb 20 22:00:49 2023 +0900
Commit:     GitHub <noreply(a)github.com>
CommitDate: Mon Feb 20 22:00:49 2023 +0900
    [NOTEPAD] Simplify GetSelectionText (#5083)
    Use EM_GETHANDLE message to get text. CORE-18837
---
 base/applications/notepad/dialog.c | 53 ++++++++++----------------------------
 1 file changed, 13 insertions(+), 40 deletions(-)
diff --git a/base/applications/notepad/dialog.c b/base/applications/notepad/dialog.c
index 12424dd30a9..27f651c67c1 100644
--- a/base/applications/notepad/dialog.c
+++ b/base/applications/notepad/dialog.c
@@ -242,68 +242,41 @@ BOOL HasFileExtension(LPCTSTR szFilename)
 int GetSelectionTextLength(HWND hWnd)
 {
-    DWORD dwStart = 0;
-    DWORD dwEnd = 0;
-
+    DWORD dwStart = 0, dwEnd = 0;
     SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
-
     return dwEnd - dwStart;
 }
 int GetSelectionText(HWND hWnd, LPTSTR lpString, int nMaxCount)
 {
-    DWORD dwStart = 0;
-    DWORD dwEnd = 0;
-    DWORD dwSize;
-    HRESULT hResult;
-    LPTSTR lpTemp;
-
-    if (!lpString)
-    {
-        return 0;
-    }
+    DWORD dwStart = 0, dwEnd = 0;
+    INT cchText = GetWindowTextLength(hWnd);
+    LPTSTR pszText;
+    HLOCAL hLocal;
+    HRESULT hr;
     SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
-
-    if (dwStart == dwEnd)
-    {
+    if (!lpString || dwStart == dwEnd || cchText == 0)
         return 0;
-    }
-    dwSize = GetWindowTextLength(hWnd) + 1;
-    lpTemp = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(TCHAR));
-    if (!lpTemp)
-    {
-        return 0;
-    }
-
-    dwSize = GetWindowText(hWnd, lpTemp, dwSize);
-
-    if (!dwSize)
-    {
-        HeapFree(GetProcessHeap(), 0, lpTemp);
+    hLocal = (HLOCAL)SendMessage(hWnd, EM_GETHANDLE, 0, 0);
+    pszText = (LPTSTR)LocalLock(hLocal);
+    if (!pszText)
         return 0;
-    }
-    hResult = StringCchCopyN(lpString, nMaxCount, lpTemp + dwStart, dwEnd - dwStart);
-    HeapFree(GetProcessHeap(), 0, lpTemp);
+    hr = StringCchCopyN(lpString, nMaxCount, pszText + dwStart, dwEnd - dwStart);
+    LocalUnlock(hLocal);
-    switch (hResult)
+    switch (hr)
     {
         case S_OK:
-        {
             return dwEnd - dwStart;
-        }
         case STRSAFE_E_INSUFFICIENT_BUFFER:
-        {
             return nMaxCount - 1;
-        }
         default:
-        {
             return 0;
-        }
     }
 }