From 0e3e95794fc5a250ab783925fa4d75bfb1aef0cd Mon Sep 17 00:00:00 2001 From: Magnus Olsen Date: Sun, 21 May 2006 09:21:38 +0000 Subject: [PATCH] patch from w3seek : optimize FindWindowEx and fix passing a NULL window class svn path=/trunk/; revision=21960 --- reactos/dll/win32/user32/windows/window.c | 78 +++++++++------ .../subsystems/win32/win32k/ntuser/window.c | 95 ++++++++++--------- 2 files changed, 100 insertions(+), 73 deletions(-) diff --git a/reactos/dll/win32/user32/windows/window.c b/reactos/dll/win32/user32/windows/window.c index 26ad8bc8466..dfee0db790b 100644 --- a/reactos/dll/win32/user32/windows/window.c +++ b/reactos/dll/win32/user32/windows/window.c @@ -516,33 +516,51 @@ FindWindowExA(HWND hwndParent, LPCSTR lpszClass, LPCSTR lpszWindow) { - UNICODE_STRING ucClassName; - UNICODE_STRING ucWindowName; + UNICODE_STRING ucClassName, *pucClassName = NULL; + UNICODE_STRING ucWindowName, *pucWindowName = NULL; HWND Result; - if (lpszClass == NULL) - { - ucClassName.Buffer = NULL; - ucClassName.Length = 0; - } - else if (IS_ATOM(lpszClass)) + if (IS_ATOM(lpszClass)) { ucClassName.Buffer = (LPWSTR)lpszClass; ucClassName.Length = 0; + pucClassName = &ucClassName; } - else + else if (lpszClass != NULL) { - RtlCreateUnicodeStringFromAsciiz(&ucClassName, (LPSTR)lpszClass); + if (!RtlCreateUnicodeStringFromAsciiz(&ucClassName, + (LPSTR)lpszClass)) + { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return NULL; + } + pucClassName = &ucClassName; } - RtlCreateUnicodeStringFromAsciiz(&ucWindowName, (LPSTR)lpszWindow); + if (lpszWindow != NULL) + { + if (!RtlCreateUnicodeStringFromAsciiz(&ucWindowName, + (LPSTR)lpszWindow)) + { + if (!IS_ATOM(lpszClass) && lpszClass != NULL) + RtlFreeUnicodeString(&ucWindowName); - Result = NtUserFindWindowEx(hwndParent, hwndChildAfter, &ucClassName, - &ucWindowName); + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return NULL; + } - if (!IS_ATOM(lpszClass)) + pucWindowName = &ucWindowName; + } + + Result = NtUserFindWindowEx(hwndParent, + hwndChildAfter, + pucClassName, + pucWindowName); + + if (!IS_ATOM(lpszClass) && lpszClass != NULL) RtlFreeUnicodeString(&ucClassName); - RtlFreeUnicodeString(&ucWindowName); + if (lpszWindow != NULL) + RtlFreeUnicodeString(&ucWindowName); return Result; } @@ -557,27 +575,33 @@ FindWindowExW(HWND hwndParent, LPCWSTR lpszClass, LPCWSTR lpszWindow) { - UNICODE_STRING ucClassName; - UNICODE_STRING ucWindowName; + UNICODE_STRING ucClassName, *pucClassName = NULL; + UNICODE_STRING ucWindowName, *pucWindowName = NULL; - if (lpszClass == NULL) + if (IS_ATOM(lpszClass)) { - ucClassName.Buffer = NULL; ucClassName.Length = 0; - } - else if (IS_ATOM(lpszClass)) - { - RtlInitUnicodeString(&ucClassName, NULL); ucClassName.Buffer = (LPWSTR)lpszClass; + pucClassName = &ucClassName; } - else + else if (lpszClass != NULL) { - RtlInitUnicodeString(&ucClassName, lpszClass); + RtlInitUnicodeString(&ucClassName, + lpszClass); + pucClassName = &ucClassName; } - RtlInitUnicodeString(&ucWindowName, lpszWindow); + if (lpszWindow != NULL) + { + RtlInitUnicodeString(&ucWindowName, + lpszWindow); + pucWindowName = &ucWindowName; + } - return NtUserFindWindowEx(hwndParent, hwndChildAfter, &ucClassName, &ucWindowName); + return NtUserFindWindowEx(hwndParent, + hwndChildAfter, + pucClassName, + pucWindowName); } diff --git a/reactos/subsystems/win32/win32k/ntuser/window.c b/reactos/subsystems/win32/win32k/ntuser/window.c index a0b375e909d..b35d07d5f2d 100644 --- a/reactos/subsystems/win32/win32k/ntuser/window.c +++ b/reactos/subsystems/win32/win32k/ntuser/window.c @@ -2433,61 +2433,64 @@ NtUserFindWindowEx(HWND hwndParent, DPRINT("Enter NtUserFindWindowEx\n"); UserEnterShared(); - _SEH_TRY + if (ucClassName != NULL || ucWindowName != NULL) { + _SEH_TRY + { + if (ucClassName != NULL) + { + ClassName = ProbeForReadUnicodeString(ucClassName); + if (ClassName.Length != 0) + { + ProbeForRead(ClassName.Buffer, + ClassName.Length, + sizeof(WCHAR)); + } + else if (!IS_ATOM(ClassName.Buffer)) + { + SetLastWin32Error(ERROR_INVALID_PARAMETER); + _SEH_LEAVE; + } + + if (!IntGetAtomFromStringOrAtom(&ClassName, + &ClassAtom)) + { + _SEH_LEAVE; + } + } + + if (ucWindowName != NULL) + { + WindowName = ProbeForReadUnicodeString(ucWindowName); + if (WindowName.Length != 0) + { + ProbeForRead(WindowName.Buffer, + WindowName.Length, + sizeof(WCHAR)); + } + } + } + _SEH_HANDLE + { + SetLastNtError(_SEH_GetExceptionCode()); + RETURN(NULL); + } + _SEH_END; + if (ucClassName != NULL) { - ClassName = ProbeForReadUnicodeString(ucClassName); - if (ClassName.Length != 0) - { - ProbeForRead(ClassName.Buffer, - ClassName.Length, - sizeof(WCHAR)); - } - else if (ClassName.Buffer != NULL && !IS_ATOM(ClassName.Buffer)) + if (ClassName.Length == 0 && ClassName.Buffer != NULL && + !IS_ATOM(ClassName.Buffer)) { SetLastWin32Error(ERROR_INVALID_PARAMETER); - _SEH_LEAVE; + RETURN(NULL); } - - if (!IntGetAtomFromStringOrAtom(&ClassName, - &ClassAtom)) + else if (ClassAtom == (RTL_ATOM)0) { - _SEH_LEAVE; + /* LastError code was set by IntGetAtomFromStringOrAtom */ + RETURN(NULL); } } - - if (ucWindowName != NULL) - { - WindowName = ProbeForReadUnicodeString(ucWindowName); - if (WindowName.Length != 0) - { - ProbeForRead(WindowName.Buffer, - WindowName.Length, - sizeof(WCHAR)); - } - } - } - _SEH_HANDLE - { - SetLastNtError(_SEH_GetExceptionCode()); - RETURN(NULL); - } - _SEH_END; - - if (ucClassName != NULL) - { - if (ClassName.Length == 0 && ClassName.Buffer != NULL && - !IS_ATOM(ClassName.Buffer)) - { - SetLastWin32Error(ERROR_INVALID_PARAMETER); - RETURN(NULL); - } - else if (ClassAtom == (RTL_ATOM)0) - { - /* LastError code was set by IntGetAtomFromStringOrAtom */ - RETURN(NULL); - } } Desktop = IntGetCurrentThreadDesktopWindow();