ReactOS 0.4.16-dev-1537-g4e425b5
closewnd.h
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API tests
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Close windows after tests
5 * COPYRIGHT: Copyright 2024-2025 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6 */
7
8#pragma once
9
10#define WaitForWindow(hWnd, Func, Seconds) \
11 for (UINT waited = 0; !Func(hWnd) && waited < (Seconds) * 1000; waited += 250) Sleep(250);
12
14{
15 for (UINT waited = 0, interval = 50; waited < wait; waited += interval)
16 {
18 return TRUE;
21 }
22 return FALSE;
23}
24
25typedef struct WINDOW_LIST
26{
30
31
33{
34 free(pList->m_phWnds);
35 pList->m_phWnds = NULL;
36 pList->m_chWnds = 0;
37}
38
40{
42 return TRUE;
43
45 SIZE_T cb = (pList->m_chWnds + 1) * sizeof(HWND);
46 HWND *phWnds = (HWND *)realloc(pList->m_phWnds, cb);
47 if (!phWnds)
48 return FALSE;
49 phWnds[pList->m_chWnds++] = hwnd;
50 pList->m_phWnds = phWnds;
51 return TRUE;
52}
53
55{
56 pList->m_phWnds = NULL;
57 pList->m_chWnds = 0;
59}
60
62{
64 pList->m_phWnds = NULL;
65 pList->m_chWnds = 0;
66 for (SIZE_T tries = 5, count; tries--;)
67 {
68 if (tries)
71 Sleep(250);
73 count = list.m_chWnds;
75 if (count == pList->m_chWnds)
76 break;
77 }
78}
79
81{
82 for (SIZE_T i = 0; i < list.m_chWnds; ++i)
83 {
84 if (list.m_phWnds[i] == hWnd)
85 return hWnd;
86 }
87 return NULL;
88}
89
90static inline BOOL SendAltF4Input()
91{
92 INPUT inputs[4];
93 ZeroMemory(&inputs, sizeof(inputs));
94 inputs[0].type = inputs[1].type = inputs[2].type = inputs[3].type = INPUT_KEYBOARD;
95 inputs[0].ki.wVk = inputs[3].ki.wVk = VK_LMENU;
96 inputs[1].ki.wVk = inputs[2].ki.wVk = VK_F4;
97 inputs[2].ki.dwFlags = inputs[3].ki.dwFlags = KEYEVENTF_KEYUP;
98 return SendInput(_countof(inputs), inputs, sizeof(INPUT)) == _countof(inputs);
99}
100
101static inline VOID CloseNewWindows(PWINDOW_LIST pExisting, PWINDOW_LIST pNew)
102{
103 for (SIZE_T i = 0; i < pNew->m_chWnds; ++i)
104 {
105 HWND hWnd = pNew->m_phWnds[i];
106 if (!IsWindowVisible(hWnd) || FindInWindowList(*pExisting, hWnd))
107 continue;
108
110 WaitForForegroundWindow(hWnd); // SetForegroundWindow may take some time
114 {
115 if (WaitForForegroundWindow(hWnd)) // We can't fake keyboard input if the target is not foreground
116 {
118 WaitForWindow(hWnd, IsWindowVisible, 1); // Closing a window may take some time
119 }
120
122 {
123 CHAR szClass[64];
124 GetClassNameA(hWnd, szClass, _countof(szClass));
125 trace("Unable to close window %p (%s)\n", hWnd, szClass);
126 }
127 }
128 }
129}
130
131#ifdef __cplusplus
132static inline VOID CloseNewWindows(PWINDOW_LIST InitialList)
133{
134 WINDOW_LIST newwindows;
135 GetWindowListForClose(&newwindows);
136 CloseNewWindows(InitialList, &newwindows);
137 FreeWindowList(&newwindows);
138}
139#endif
140
141static inline HWND FindNewWindow(PWINDOW_LIST List1, PWINDOW_LIST List2)
142{
143 for (SIZE_T i2 = 0; i2 < List2->m_chWnds; ++i2)
144 {
145 HWND hWnd = List2->m_phWnds[i2];
147 continue;
148
149 BOOL bFoundInList1 = FALSE;
150 for (SIZE_T i1 = 0; i1 < List1->m_chWnds; ++i1)
151 {
152 if (hWnd == List1->m_phWnds[i1])
153 {
154 bFoundInList1 = TRUE;
155 break;
156 }
157 }
158
159 if (!bFoundInList1)
160 return hWnd;
161 }
162 return NULL;
163}
164
166{
167 if (!IsWindowVisible(hwnd))
168 return TRUE;
169
170 *(PINT)lParam += 1;
171 return TRUE;
172}
173
174static inline INT GetWindowCount(VOID)
175{
176 INT nCount = 0;
178 return nCount;
179}
#define trace
Definition: atltest.h:70
HWND hWnd
Definition: settings.c:17
Definition: list.h:37
static HWND FindNewWindow(PWINDOW_LIST List1, PWINDOW_LIST List2)
Definition: closewnd.h:141
static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
Definition: closewnd.h:39
static VOID GetWindowListForClose(PWINDOW_LIST pList)
Definition: closewnd.h:61
static VOID FreeWindowList(PWINDOW_LIST pList)
Definition: closewnd.h:32
static BOOL SendAltF4Input()
Definition: closewnd.h:90
static BOOL CALLBACK CountVisibleWindowsProc(HWND hwnd, LPARAM lParam)
Definition: closewnd.h:165
static INT GetWindowCount(VOID)
Definition: closewnd.h:174
#define WaitForWindow(hWnd, Func, Seconds)
Definition: closewnd.h:10
static BOOL WaitForForegroundWindow(HWND hWnd, UINT wait=500)
Definition: closewnd.h:13
static HWND FindInWindowList(const WINDOW_LIST &list, HWND hWnd)
Definition: closewnd.h:80
struct WINDOW_LIST * PWINDOW_LIST
static VOID CloseNewWindows(PWINDOW_LIST pExisting, PWINDOW_LIST pNew)
Definition: closewnd.h:101
static VOID GetWindowList(PWINDOW_LIST pList)
Definition: closewnd.h:54
LPARAM lParam
Definition: combotst.c:139
#define realloc
Definition: debug_ros.c:6
#define free
Definition: debug_ros.c:5
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CALLBACK
Definition: compat.h:35
unsigned int BOOL
Definition: ntddk_ex.h:94
FxChildList * pList
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLuint64EXT * result
Definition: glext.h:11304
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
static HMODULE MODULEINFO DWORD cb
Definition: module.c:33
unsigned int UINT
Definition: ndis.h:50
#define list
Definition: rosglue.h:35
#define _countof(array)
Definition: sndvol32.h:70
SIZE_T m_chWnds
Definition: closewnd.h:27
HWND * m_phWnds
Definition: closewnd.h:28
KEYBDINPUT ki
Definition: winable.h:62
DWORD type
Definition: winable.h:59
DWORD dwFlags
Definition: winable.h:49
WORD wVk
Definition: winable.h:47
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
uint32_t DWORD_PTR
Definition: typedefs.h:65
ULONG_PTR SIZE_T
Definition: typedefs.h:80
int32_t INT
Definition: typedefs.h:58
VOID WINAPI SwitchToThisWindow(HWND hwnd, BOOL fAltTab)
Definition: window.c:82
UINT WINAPI SendInput(UINT, LPINPUT, int)
Definition: ntwrapper.h:344
#define INPUT_KEYBOARD
Definition: winable.h:10
#define ZeroMemory
Definition: winbase.h:1753
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
LONG_PTR LPARAM
Definition: windef.h:208
int * PINT
Definition: windef.h:177
#define WM_SYSCOMMAND
Definition: winuser.h:1769
HWND WINAPI GetForegroundWindow(void)
Definition: ntwrapper.h:392
BOOL WINAPI PostMessageW(_In_opt_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
int WINAPI GetClassNameA(_In_ HWND hWnd, _Out_writes_to_(nMaxCount, return) LPSTR lpClassName, _In_ int nMaxCount)
LRESULT WINAPI SendMessageTimeoutW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM, _In_ UINT, _In_ UINT, _Out_opt_ PDWORD_PTR)
BOOL WINAPI EnumWindows(_In_ WNDENUMPROC lpEnumFunc, _In_ LPARAM lParam)
#define SC_CLOSE
Definition: winuser.h:2628
#define VK_F4
Definition: winuser.h:2294
BOOL WINAPI IsWindowEnabled(_In_ HWND)
#define SMTO_ABORTIFHUNG
Definition: winuser.h:1234
BOOL WINAPI IsWindowVisible(_In_ HWND)
#define KEYEVENTF_KEYUP
Definition: winuser.h:1113
#define VK_LMENU
Definition: winuser.h:2322
char CHAR
Definition: xmlstorage.h:175