-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListWindows.c
307 lines (257 loc) · 7.97 KB
/
ListWindows.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#include <strsafe.h>
#include <dwmapi.h>
#include <assert.h>
typedef struct ListWindowsClient ListWindowsClient;
typedef struct ListWindowsWorkspace ListWindowsWorkspace;
typedef struct ListWindowsClientData ListWindowsClientData;
static const WCHAR UNICODE_BOM = 0xFEFF;
struct ListWindowsWorkspace
{
ListWindowsClient *clients;
ListWindowsClient *lastClient;
size_t maxProcessNameLen;
};
struct ListWindowsClient
{
ListWindowsClientData *data;
ListWindowsClient *next;
};
struct ListWindowsClientData
{
HWND hwnd;
DWORD processId;
CHAR *processName;
CHAR *className;
CHAR *title;
BOOL isMinimized;
};
BOOL list_windows_is_alt_tab_window(HWND hwnd)
{
// Start at the root owner
HWND hwndWalk = GetAncestor(hwnd, GA_ROOTOWNER);
// See if we are the last active visible popup
HWND hwndTry;
while ((hwndTry = GetLastActivePopup(hwndWalk)) != hwndTry)
{
if (IsWindowVisible(hwndTry)) break;
hwndWalk = hwndTry;
}
return hwndWalk == hwnd;
}
BOOL list_windows_is_window_cloaked(HWND hwnd)
{
BOOL isCloaked = FALSE;
return (SUCCEEDED(DwmGetWindowAttribute(hwnd, DWMWA_CLOAKED,
&isCloaked, sizeof(isCloaked))) && isCloaked);
}
BOOL CALLBACK list_windows_prop_enum_callback(HWND hwndSubclass, LPTSTR lpszString, HANDLE hData, ULONG_PTR dwData)
{
UNREFERENCED_PARAMETER(hwndSubclass);
if(((DWORD_PTR)lpszString & 0xffff0000) != 0)
{
if (wcscmp(lpszString, L"ApplicationViewCloakType") == 0)
{
BOOL *hasAppropriateApplicationViewCloakTypePtr = (BOOL *)dwData;
//0 seems to be when it is running on the current desktop
*hasAppropriateApplicationViewCloakTypePtr = hData == 0;
return FALSE;
}
}
return TRUE;
}
BOOL list_windows_is_root_window(HWND hwnd, LONG styles, LONG exStyles)
{
BOOL isWindowVisible = IsWindowVisible(hwnd);
HWND desktopWindow = GetDesktopWindow();
if(hwnd == desktopWindow)
{
return FALSE;
}
if(isWindowVisible == FALSE)
{
return FALSE;
}
if(exStyles & WS_EX_TOOLWINDOW)
{
return FALSE;
}
if(list_windows_is_window_cloaked(hwnd))
{
return FALSE;
}
TCHAR className[256] = {0};
GetClassName(hwnd, className, sizeof(className)/sizeof(TCHAR));
if(wcsstr(className, L"ApplicationFrameWindow"))
{
BOOL hasCorrectCloakedProperty = FALSE;
EnumPropsEx(hwnd, list_windows_prop_enum_callback, (ULONG_PTR)&hasCorrectCloakedProperty);
if(hasCorrectCloakedProperty)
{
return TRUE;
}
else
{
return FALSE;
}
}
if(exStyles & WS_EX_NOACTIVATE)
{
return FALSE;
}
if(styles & WS_CHILD)
{
return FALSE;
}
HWND parentHwnd = GetParent(hwnd);
if(parentHwnd)
{
return FALSE;
}
BOOL isAltTabWindow = list_windows_is_alt_tab_window(hwnd);
if(!isAltTabWindow)
{
return FALSE;
}
return TRUE;
}
ListWindowsClient* clientFactory_create_from_hwnd(HWND hwnd)
{
DWORD processId = 0;
GetWindowThreadProcessId(hwnd, &processId);
HANDLE hProcess;
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
CHAR processImageFileName[1024] = {0};
DWORD dwFileSize = 1024;
DWORD processImageFileNameResult = QueryFullProcessImageNameA(
hProcess,
0,
processImageFileName,
&dwFileSize
);
CloseHandle(hProcess);
if(processImageFileNameResult == 0)
{
wchar_t buf[256];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
}
CHAR title[1024];
GetWindowTextA(
hwnd,
title,
sizeof(title)/sizeof(TCHAR)
);
CHAR className[256] = {0};
GetClassNameA(hwnd, className, sizeof(className)/sizeof(TCHAR));
BOOL isMinimized = IsIconic(hwnd);
LPCSTR processShortFileName = PathFindFileNameA(processImageFileName);
ListWindowsClientData *clientData = calloc(1, sizeof(ListWindowsClientData));
assert(clientData);
clientData->hwnd = hwnd;
clientData->processId = processId;
clientData->className = _strdup(className);
clientData->processName = _strdup(processShortFileName);
clientData->title = _strdup(title);
clientData->isMinimized = isMinimized;
ListWindowsClient *c;
c = calloc(1, sizeof(ListWindowsClient));
assert(c);
c->data = clientData;
return c;
}
static void client_free(ListWindowsClient *self)
{
free(self->data->title);
free(self->data->processName);
free(self->data->className);
free(self->data);
free(self);
}
static BOOL CALLBACK enum_windows_callback(HWND hwnd, LPARAM lparam)
{
HWND shellHwnd = GetShellWindow();
if(hwnd == shellHwnd)
{
return TRUE;
}
ListWindowsWorkspace *workspace = (ListWindowsWorkspace*)lparam;
LONG styles = GetWindowLong(hwnd, GWL_STYLE);
LONG exStyles = GetWindowLong(hwnd, GWL_EXSTYLE);
BOOL isRootWindow = list_windows_is_root_window(hwnd, styles, exStyles);
if(!isRootWindow)
{
return TRUE;
}
if(!(styles & WS_VISIBLE))
{
return TRUE;
}
ListWindowsClient *client = clientFactory_create_from_hwnd(hwnd);
if(strstr(client->data->className, "Progman"))
{
return TRUE;
}
if(strstr(client->data->title, "ApplicationFrameWindow"))
{
return TRUE;
}
if(workspace->clients == NULL)
{
workspace->clients = client;
workspace->lastClient = client;
}
else
{
workspace->lastClient->next = client;
workspace->lastClient = client;
}
size_t processFileNameLen = strlen(client->data->processName);
if(processFileNameLen > workspace->maxProcessNameLen)
{
workspace->maxProcessNameLen = processFileNameLen;
}
return TRUE;
}
int list_windows_run(int maxItems, CHAR** toFill, void *state)
{
UNREFERENCED_PARAMETER(state);
ListWindowsWorkspace *workspace = calloc(1, sizeof(ListWindowsWorkspace));
assert(workspace);
EnumWindows(enum_windows_callback, (LPARAM)workspace);
size_t cchStringSize;
CHAR temp[1024];
StringCchPrintfA(temp, 1024, "%-*s %-*s %-*s %s\n", 8, "HWND", 8, "PID", (int)workspace->maxProcessNameLen, "Name", "Title");
assert(SUCCEEDED(StringCchLengthA(temp, 1024, &cchStringSize)));
CHAR header[1024];
sprintf_s(
header,
1024,
"%-*s %-*s %-*s %s\n", 8, "HWND", 8, "PID", (int)workspace->maxProcessNameLen, "Name", "Title");
toFill[0] = _strdup(header);
ListWindowsClient *c = workspace->clients;
int numberOfResults = 1;
CHAR lineBuf[1024];
while(c && numberOfResults <= maxItems)
{
StringCchPrintfA(temp, 1024, "%08Ix %08lu %-*s %s\n",
(uintptr_t)c->data->hwnd, c->data->processId, (int)workspace->maxProcessNameLen, c->data->processName, c->data->title);
assert(SUCCEEDED(StringCchLengthA(temp, 1024, &cchStringSize)));
sprintf_s(
lineBuf,
1024,
"%08Ix %08lu %-*s %s\n",
(uintptr_t)c->data->hwnd, c->data->processId, (int)workspace->maxProcessNameLen, c->data->processName, c->data->title);
toFill[numberOfResults] = _strdup(lineBuf);
ListWindowsClient *cPrev = c;
c = c->next;
client_free(cPrev);
numberOfResults++;
}
free(workspace);
return numberOfResults;
}