-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexplorerex-inactive-theme-loader.wh.cpp
More file actions
621 lines (540 loc) · 16.3 KB
/
explorerex-inactive-theme-loader.wh.cpp
File metadata and controls
621 lines (540 loc) · 16.3 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// ==WindhawkMod==
// @id explorerex-inactive-theme-loader
// @name ExplorerEx Inactive Theme Loader
// @description Loads an "inactive theme", or alternate visual style, for the taskbar in ExplorerEx.
// @version 1.1
// @author Isabella Lulamoon (kawapure)
// @github https://github.com/kawapure
// @twitter https://twitter.com/kawaipure
// @include explorer.exe
// @compilerOptions -luxtheme -ldwmapi -lshlwapi
// ==/WindhawkMod==
// ==WindhawkModReadme==
/*
# ExplorerEx inactive theme loader
This mod allows you to use an inactive theme, which is one other than the global theme shared by Winlogon's theme server,
with ExplorerEx for the taskbar and start menu.
*/
// ==/WindhawkModReadme==
// ==WindhawkModSettings==
/*
- theme: "C:\\Windows\\Resources\\Themes\\Aero\\Aero.msstyles"
$name: The full path to the secondary visual style. Blank or invalid entry will fall back to classic theme.
*/
// ==/WindhawkModSettings==
#include <processenv.h>
#include <windhawk_api.h>
#include <windhawk_utils.h>
#include <libloaderapi.h>
#include <memoryapi.h>
#include <uxtheme.h>
#include <winerror.h>
#include <winnt.h>
#include <tlhelp32.h>
#include <dwmapi.h>
#include <shlwapi.h>
#include <vector>
LPCWSTR g_rgszTaskbarClasses[] = {
L"Clock",
L"TrayNotify",
L"TrayNotifyFlyout", // Windows 7
L"TaskBar",
L"TaskBar::ComboBox",
L"TaskBar::Edit",
L"TaskBar::Rebar",
L"TaskBar::Toolbar",
L"TaskbarPearl", // Windows Vista
L"TaskBand",
L"TaskBand::Scrollbar",
L"TaskBand::Toolbar",
L"TaskbandExtendedUI", // Windows Vista
L"TaskBand2",
L"TaskBand2::Scrollbar",
L"TaskbarShowDesktop", // Windows 7
L"Start::Button",
L"StartTop::Button", // Windows Vista
L"StartMiddle::Button", // Windows Vista
L"StartBottom::Button", // Windows Vista
L"StartMenu",
L"StartPanel",
L"MoreProgramsArrow",
L"LogoffButtons",
L"MoreProgramsArrowBack", // Windows Vista
L"MoreProgramsTab", // Windows Vista
L"SoftwareExplorer", // Windows Vista
L"OpenBox", // Windows Vista
L"StartPanelPriv", // Windows Vista
L"TrayNotifHoriz::Button",
L"TrayNotifyHorizHCWhite::Button",
L"TrayNotifyHorizOpen::Button",
L"TrayNotifyHorizOpenHCWhite::Button",
L"TrayNotifyVert::Button",
L"TrayNotifyVertHCWhite::Button",
L"TrayNotifyVertOpen::Button",
L"TrayNotifyVertOpenHCWHite::Button"
};
bool IsTaskbarClass(HWND hWnd, LPCWSTR szClassName)
{
ATOM atValue = (ATOM)(size_t)GetPropW(hWnd, (LPCWSTR)0xA911);
WCHAR szBuf[260] = { L'\0' };
if (atValue)
{
if (GetAtomNameW(atValue, szBuf, ARRAYSIZE(szBuf)))
{
wcscat(szBuf, L"::");
wcscat(szBuf, szClassName);
}
}
for (UINT i = 0; i < ARRAYSIZE(g_rgszTaskbarClasses); i++)
{
if (StrStrIW(szClassName, g_rgszTaskbarClasses[i]))
{
return true;
}
else if (szBuf[0] && StrStrIW(szBuf, g_rgszTaskbarClasses[i]))
{
return true;
}
}
return false;
}
#if __WIN64
#define WINAPI_STR L"__cdecl"
#else
#define WINAPI_STR L"__stdcall"
#endif
#if __WIN64
#define THISCALL_STR L"__cdecl"
#else
#define THISCALL_STR L"__thiscall"
#endif
typedef HRESULT WINAPI (*GetThemeDefaults_t)(
LPCWSTR pszThemeFileName,
LPWSTR pszColorName,
DWORD dwColorNameLen,
LPWSTR pszSizeName,
DWORD dwSizeNameLen
);
GetThemeDefaults_t GetThemeDefaults;
typedef HRESULT WINAPI (*LoaderLoadTheme_t)(
HANDLE hThemeFile,
HINSTANCE hInstance,
LPCWSTR pszThemeFileName,
LPCWSTR pszColorParam,
LPCWSTR pszSizeParam,
OUT HANDLE *hSharableSection,
LPWSTR pszSharableSectionName,
int cchSharableSectionName,
OUT HANDLE *hNonsharableSection,
LPWSTR pszNonsharableSectionName,
int cchNonsharableSectionName,
PVOID pfnCustomLoadHandler,
OUT HANDLE *hReuseSection,
int a,
int b,
BOOL fEmulateGlobal
);
LoaderLoadTheme_t LoaderLoadTheme;
typedef HRESULT WINAPI (*LoaderLoadTheme_t_win11)(
HANDLE hThemeFile,
HINSTANCE hInstance,
LPCWSTR pszThemeFileName,
LPCWSTR pszColorParam,
LPCWSTR pszSizeParam,
OUT HANDLE *hSharableSection,
LPWSTR pszSharableSectionName,
int cchSharableSectionName,
OUT HANDLE *hNonsharableSection,
LPWSTR pszNonsharableSectionName,
int cchNonsharableSectionName,
PVOID pfnCustomLoadHandler,
OUT HANDLE *hReuseSection,
int a,
int b
);
typedef HTHEME WINAPI (*OpenThemeDataFromFile_t)(
HANDLE hThemeFile,
HWND hWnd,
LPCWSTR pszClassList,
DWORD dwFlags
// DWORD unknown,
// bool a
);
OpenThemeDataFromFile_t OpenThemeDataFromFile;
typedef struct _LocalThemeFile
{
char header[7]; // must be "thmfile"
LPVOID sharableSectionView;
HANDLE hSharableSection;
LPVOID nsSectionView;
HANDLE hNsSection;
char end[3]; // must be "end"
} LocalThemeFile;
//=================================================================================================
HANDLE g_hLocalTheme;
using OpenThemeData_t = decltype(&OpenThemeData);
OpenThemeData_t OpenThemeData_orig;
HTHEME WINAPI OpenThemeData_hook(HWND hwnd, LPCWSTR pszClassList)
{
if (pszClassList && IsTaskbarClass(hwnd, pszClassList))
{
HTHEME fromFile = OpenThemeDataFromFile(g_hLocalTheme, hwnd, pszClassList, 0);
if (fromFile)
{
return fromFile;
}
}
return OpenThemeData_orig(hwnd, pszClassList);
}
using OpenThemeDataEx_t = decltype(&OpenThemeDataEx);
OpenThemeDataEx_t OpenThemeDataEx_orig;
HTHEME WINAPI OpenThemeDataEx_hook(HWND hwnd, LPCWSTR pszClassList, DWORD dwFlags)
{
if (pszClassList && IsTaskbarClass(hwnd, pszClassList))
{
if (g_hLocalTheme)
{
HTHEME fromFile = OpenThemeDataFromFile(g_hLocalTheme, hwnd, pszClassList, 0);
if (fromFile)
{
return fromFile;
}
}
else
{
return NULL;
}
}
return OpenThemeDataEx_orig(hwnd, pszClassList, dwFlags);
}
typedef HTHEME __fastcall (*_OpenThemeData_t)(HWND hwnd, PCWSTR pszClassList, DWORD dwFlags, int unk1, bool unk2);
_OpenThemeData_t _OpenThemeData_orig;
HTHEME __fastcall _OpenThemeData_hook(HWND hwnd, PCWSTR pszClassList, DWORD dwFlags, int unk1, bool unk2)
{
if (pszClassList && IsTaskbarClass(hwnd, pszClassList))
{
if (g_hLocalTheme)
{
HTHEME fromFile = OpenThemeDataFromFile(g_hLocalTheme, hwnd, pszClassList, 0);
if (fromFile)
{
return fromFile;
}
}
else
{
return NULL;
}
}
return _OpenThemeData_orig(hwnd, pszClassList, dwFlags, unk1, unk2);
}
/*
* Load a visual style from the provided file path.
*
* This relies on a few internal functions from uxtheme in order to work. It's
* basically the same approach as StartIsBack.
*/
HRESULT LoadThemeFromFilePath(PCWSTR szThemeFileName)
{
HRESULT hr = S_OK;
HMODULE hUxtheme = GetModuleHandleW(L"uxtheme.dll");
if (!hUxtheme)
{
return E_FAIL;
}
GetThemeDefaults = (GetThemeDefaults_t)GetProcAddress(hUxtheme, (LPCSTR)7);
LoaderLoadTheme = (LoaderLoadTheme_t)GetProcAddress(hUxtheme, (LPCSTR)92);
OpenThemeDataFromFile = (OpenThemeDataFromFile_t)GetProcAddress(hUxtheme, (LPCSTR)16);
if (!GetThemeDefaults || !LoaderLoadTheme || !OpenThemeDataFromFile)
{
return E_FAIL;
}
OSVERSIONINFOW verInfo = { 0 };
hr = GetVersionExW(&verInfo) ? S_OK : E_FAIL;
WCHAR defColor[MAX_PATH];
WCHAR defSize[MAX_PATH];
hr = GetThemeDefaults(
szThemeFileName,
defColor,
ARRAYSIZE(defColor),
defSize,
ARRAYSIZE(defSize)
);
HANDLE hSharableSection;
HANDLE hNonsharableSection;
if (verInfo.dwBuildNumber < 20000)
{
hr = LoaderLoadTheme(
NULL,
NULL,
szThemeFileName,
defColor,
defSize,
&hSharableSection,
NULL,
0,
&hNonsharableSection,
NULL,
0,
NULL,
NULL,
NULL,
NULL,
FALSE
);
}
else
{
hr = ((LoaderLoadTheme_t_win11)LoaderLoadTheme)(
NULL,
NULL,
szThemeFileName,
defColor,
defSize,
&hSharableSection,
NULL,
0,
&hNonsharableSection,
NULL,
0,
NULL,
NULL,
NULL,
NULL
);
}
if (SUCCEEDED(hr))
{
g_hLocalTheme = malloc(sizeof(LocalThemeFile));
if (g_hLocalTheme)
{
LocalThemeFile *ltf = (LocalThemeFile *)g_hLocalTheme;
lstrcpyA(ltf->header, "thmfile");
lstrcpyA(ltf->header, "end");
ltf->sharableSectionView = MapViewOfFile(hSharableSection, 4, 0, 0, 0);
ltf->hSharableSection = hSharableSection;
ltf->nsSectionView = MapViewOfFile(hNonsharableSection, 4, 0, 0, 0);
ltf->hNsSection = hNonsharableSection;
}
else
{
hr = E_FAIL;
}
}
else
{
g_hLocalTheme = NULL;
hr = E_FAIL;
}
return hr;
}
/*
* Load the Windhawk mod settings for the current program. If the user enabled a different theme
* for the program, then this will return true. Otherwise, it will return false.
*
* This implementation is heavily copied from the "Text Replace" mod.
*/
bool LoadSettings()
{
PCWSTR themePath = Wh_GetStringSetting(L"theme");
bool hasAppliedTheme = false;
if (*themePath)
{
LoadThemeFromFilePath(themePath);
}
else
{
g_hLocalTheme = NULL;
}
Wh_FreeStringSetting(themePath);
return hasAppliedTheme;
}
#define WM_UAHINIT 0x031B
#define WM_THEMECHANGED_TRIGGER WM_UAHINIT
#define WTC_THEMEACTIVE (1 << 0) // new theme is now active
#define WTC_CUSTOMTHEME (1 << 1) // this msg for custom-themed apps
BOOL CALLBACK EnumWindowsChildCallBackFnc(HWND hwnd, LPARAM lParam)
{
SendMessage(hwnd, WM_UAHINIT, NULL, 0);
SendMessage(hwnd, WM_THEMECHANGED, WPARAM(-1), WTC_THEMEACTIVE | WTC_CUSTOMTHEME);
return TRUE;
}
BOOL CALLBACK EnumWindowsCallBackFnc(HWND hwnd, LPARAM lParam)
{
SendMessage(hwnd, WM_UAHINIT, NULL, 0);
SendMessage(hwnd, WM_THEMECHANGED, WPARAM(-1), WTC_THEMEACTIVE | WTC_CUSTOMTHEME);
SendMessage(hwnd, WM_THEMECHANGED_TRIGGER, WPARAM(-1), (WPARAM(-1) << 4) | ((WTC_THEMEACTIVE | WTC_CUSTOMTHEME) & 0xf));
EnumChildWindows(hwnd, EnumWindowsChildCallBackFnc, lParam);
return TRUE;
}
bool UpdateWindowThemes(DWORD dwProcessId)
{
HANDLE thread_snap = INVALID_HANDLE_VALUE;
THREADENTRY32 te32;
// Take a snapshot of all running threads:
thread_snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (thread_snap == INVALID_HANDLE_VALUE) {
return(FALSE);
}
// Fill in the size of the structure before using it:
te32.dwSize = sizeof(THREADENTRY32);
// Retrieve information about the first thread, and exit if unsuccessful:
if (!Thread32First(thread_snap, &te32)) {
CloseHandle(thread_snap);
return(FALSE);
}
// Now walk the thread list of the system, and display thread IDs of each thread
// associated with the specified process:
do
{
if (te32.th32OwnerProcessID == dwProcessId)
{
EnumThreadWindows(te32.th32ThreadID, EnumWindowsCallBackFnc, NULL);
}
}
while (Thread32Next(thread_snap, &te32));
// clean up the snapshot object.
CloseHandle(thread_snap);
return(TRUE);
}
DWORD WINAPI ThemeChangeThread(LPVOID lpParam)
{
SleepEx(2, NULL);
SleepEx(2, NULL);
UpdateWindowThemes(GetCurrentProcessId());
return 0;
}
static std::vector<int>* patternToByte(const char* pattern)
{
auto bytes = new std::vector<int>();
const auto start = const_cast<char*>(pattern);
const auto end = const_cast<char*>(pattern) + strlen(pattern);
for (auto current = start; current < end; ++current)
{
if (*current == '?')
{
++current;
if (*current == '?')
++current;
bytes->push_back(-1);
}
else { bytes->push_back(strtoul(current, ¤t, 16)); }
}
return bytes;
}
static uintptr_t FindPattern(uintptr_t baseAddress, const char* signature)
{
const auto dosHeader = (PIMAGE_DOS_HEADER)baseAddress;
const auto ntHeaders = (PIMAGE_NT_HEADERS)((unsigned char*)baseAddress + dosHeader->e_lfanew);
const auto sizeOfImage = ntHeaders->OptionalHeader.SizeOfImage;
auto patternBytes = patternToByte(signature);
const auto scanBytes = reinterpret_cast<unsigned char*>(baseAddress);
const auto s = patternBytes->size();
const auto d = patternBytes->data();
for (size_t i = 0; i < sizeOfImage - s; ++i)
{
bool found = true;
for (size_t j = 0; j < s; ++j)
{
if (scanBytes[i + j] != d[j] && d[j] != -1)
{
found = false;
break;
}
}
if (found)
{
uintptr_t address = reinterpret_cast<uintptr_t>(&scanBytes[i]);
delete patternBytes;
return address;
}
}
delete patternBytes;
return NULL;
}
//Ittr: Consolidated function for pattern byte replacements.
static void ChangeImportedPattern(void* dllPattern, const unsigned char* newBytes, SIZE_T size) //thank you wiktor
{
if (dllPattern)
{
DWORD old;
VirtualProtect(dllPattern, size, PAGE_EXECUTE_READWRITE, &old);
memcpy(dllPattern, newBytes, size);
VirtualProtect(dllPattern, size, old, 0);
}
}
// Remove AMAP class from loaded msstyle so that Vista and 7 msstyles are compatible
void RemoveLoadAnimationDataMap()
{
// 48 8B 53 20 48 8B ?? E8 ?? ?? ?? ?? 8B ?? 48 8B ?? E8 ?? ?? ?? ?? 8B ?? EB 05 B8 57 00 07 80
// thank you amrsatrio for the pattern + offsetting method
const char* LoadAnimationDataMap = "48 8B 53 20 48 8B ?? E8 ?? ?? ?? ?? 8B ?? 48 8B";
HMODULE uxTheme = GetModuleHandle(L"uxtheme.dll");
if (uxTheme)
{
char* LADMPattern = (char*)FindPattern((uintptr_t)uxTheme, LoadAnimationDataMap);
if (LADMPattern)
{
LADMPattern += 7;
LADMPattern += 5 + *(int*)(LADMPattern + 1);
unsigned char bytes[] = { 0x31, 0xC0, 0xC3 }; // mov eax 0, ret
ChangeImportedPattern(LADMPattern, bytes, sizeof(bytes));
}
}
}
// The mod is being initialized, load settings, hook functions, and do other
// initialization stuff if required.
BOOL Wh_ModInit()
{
Wh_Log(L"Init " WH_MOD_ID L" version " WH_MOD_VERSION);
HMODULE hUxtheme = GetModuleHandleW(L"uxtheme.dll");
if (hUxtheme)
{
GetThemeDefaults = (GetThemeDefaults_t)GetProcAddress(hUxtheme, (LPCSTR)7);
LoaderLoadTheme = (LoaderLoadTheme_t)GetProcAddress(hUxtheme, (LPCSTR)92);
OpenThemeDataFromFile = (OpenThemeDataFromFile_t)GetProcAddress(hUxtheme, (LPCSTR)16);
FARPROC OpenNcThemeData = GetProcAddress(hUxtheme, (LPCSTR)49);
if (GetThemeDefaults && LoaderLoadTheme && OpenThemeDataFromFile && OpenNcThemeData)
{
Wh_SetFunctionHook(
(void *)OpenThemeData,
(void *)OpenThemeData_hook,
(void **)&OpenThemeData_orig
);
Wh_SetFunctionHook(
(void *)OpenThemeDataEx,
(void *)OpenThemeDataEx_hook,
(void **)&OpenThemeDataEx_orig
);
WindhawkUtils::SYMBOL_HOOK rgHooks[] = {
{
{ L"void * " WINAPI_STR L" _OpenThemeData(struct HWND__ *,unsigned short const *,int,unsigned long,bool)" },
(void **)&_OpenThemeData_orig,
(void *)_OpenThemeData_hook
},
};
if (!WindhawkUtils::HookSymbols(hUxtheme, rgHooks, ARRAYSIZE(rgHooks)))
{
Wh_Log(L"Failed to hook.");
}
// Remove the animation data map now that symbol hooks are done.
// For some reason, I can't seem to get it to work at all with a symbol hook.
RemoveLoadAnimationDataMap();
// Now that everything is set up, apply the theme:
LoadSettings();
}
}
CreateThread(NULL, 0, ThemeChangeThread, NULL, NULL, NULL);
SleepEx(0, NULL);
return TRUE;
}
void Wh_ModSettingsChanged()
{
LoadSettings();
UpdateWindowThemes(GetCurrentProcessId());
}
// The mod is being unloaded, free all allocated resources.
void Wh_ModUninit()
{
UpdateWindowThemes(GetCurrentProcessId());
}