-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiconreload.c
More file actions
808 lines (703 loc) · 17.3 KB
/
Copy pathiconreload.c
File metadata and controls
808 lines (703 loc) · 17.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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/*
/ / / / / / / / / / /
/ / IconReload.c / /
/ / / / / / / / / / /
IconReload
Copyright (c) 2026 Matthew M. Power
Licensed under the Apache License, Version 2.0.
See LICENSE and NOTICE files in this repository.
*/
#define COBJMACROS
#define UNICODE
#define _UNICODE
#include <windows.h> // DWORD, HANDLE, HRESULT, MAX_PATH,
// INPUT_RECORD, KEY_EVENT, VK_ESCAPE,
// STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
// GetStdHandle(), WriteConsoleW(),
// ReadConsoleInputW(), Sleep(),
// GetModuleFileNameW(),
// lstrlenW(), lstrcpynW(), lstrcmpiW(),
// CoInitialize(), CoCreateInstance(),
// CoUninitialize(), SUCCEEDED(), FAILED()
#include <shlobj.h> // SHChangeNotify(), SHCNE_ASSOCCHANGED,
// SHCNF_IDLIST, SHCNF_FLUSH
#include <exdisp.h> // IShellWindows, IWebBrowserApp,
// CLSID_ShellWindows, IID_IShellWindows,
// IID_IWebBrowserApp,
// IShellWindows_get_Count(),
// IShellWindows_Item(),
// IShellWindows_Release(),
// IWebBrowserApp_get_LocationURL(),
// IWebBrowserApp_get_LocationName(),
// IWebBrowserApp_Refresh(),
// IWebBrowserApp_Release()
#include <oleauto.h> // VARIANT, BSTR, VariantInit(), VariantClear(),
// SysFreeString(), V_VT(), V_I4(), VT_I4
#define REFRESH_EXIT 0
#define REFRESH_LOCAL 1
#define REFRESH_ALL 2
/* wait after shell notification
** before refreshing Explorer
*/
#define EXPLORER_SETTLE_MS 250
/* wait between Explorer refreshes
** to avoid blank folder views
*/
#define EXPLORER_STABILIZE_MS 150
/* ==========
** print_text
** ==========
*/
static void print_text(const wchar_t *text)
{
/* number of characters written
*/
DWORD written;
/* bad text check
*/
if (!text) {
return;
}
/* write text to console
*/
WriteConsoleW (
GetStdHandle(STD_OUTPUT_HANDLE),
text,
(DWORD)lstrlenW(text),
&written,
NULL);
}
/* ========
** print_ch
** ========
*/
static void print_ch(wchar_t ch)
{
DWORD written;
WriteConsoleW (
GetStdHandle(STD_OUTPUT_HANDLE),
&ch,
1,
&written,
NULL);
}
/* =============
** print_newline
** =============
*/
static void print_newline(void)
{
print_text(L"\r\n");
}
/* ==========
** print_long
** ==========
*/
static void print_long(long value)
{
/* decimal digit buffer
** built backwards
*/
wchar_t buf[32];
unsigned long n;
int i = 0;
/* handle negative value
*/
if (value < 0) {
print_ch(L'-');
n = (unsigned long)(-value);
} else {
n = (unsigned long)value;
}
/* split number into digits
** from right to left
*/
do {
buf[i++] = (wchar_t)(L'0' + (n % 10));
n /= 10;
} while (n && i < (int)(sizeof(buf) / sizeof(buf[0])));
/* print digits back out
** in normal order
*/
while (i > 0) {
print_ch(buf[--i]);
}
}
/* ===========
** print_hex32
** ===========
*/
static void print_hex32(unsigned long value)
{
/* hex digit lookup table
*/
static const wchar_t hex[] = L"0123456789ABCDEF";
int i;
/* print hex prefix
*/
print_text(L"0x");
/* print eight hex digits
** from high to low
*/
for (i = 7; i >= 0; i--) {
print_ch(hex[(value >> (i * 4)) & 0x0F]);
}
}
/* ========
** read_key
** ========
*/
static int read_key(void)
{
/* console input state
*/
HANDLE input;
INPUT_RECORD rec;
DWORD read;
/* get keyboard input handle
*/
input = GetStdHandle(STD_INPUT_HANDLE);
/* wait until a real
** keypress arrives
*/
for (;;) {
/* read one console event
*/
if (!ReadConsoleInputW(input, &rec, 1, &read)) {
return 27;
}
/* only accept key-down events
*/
if (rec.EventType == KEY_EVENT &&
rec.Event.KeyEvent.bKeyDown) {
/* esc key exits
*/
if (rec.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE) {
return 27;
}
/* normal character key
*/
if (rec.Event.KeyEvent.uChar.UnicodeChar) {
return rec.Event.KeyEvent.uChar.UnicodeChar;
}
}
}
}
/* =============
** press_any_key
** =============
*/
static void press_any_key(void)
{
print_text(L"\r\nPress any key to exit...");
(void)read_key();
}
/* ======================
** ask_refresh_mode
** waits for l, a, or Esc
** ======================
*/
static int ask_refresh_mode(void)
{
int ch;
print_text(L"Refresh Explorer icons in:\r\n");
print_text(L"(L)ocal Explorer window for this folder\r\n");
print_text(L"(A)ll open Explorer windows\r\n");
print_text(L"Esc - exit\r\n");
print_text(L"Choice: ");
for (;;) {
ch = read_key();
/* exit if esc key
*/
if (ch == 27) {
print_newline();
return REFRESH_EXIT;
}
/* refresh local dir window
*/
if (ch == L'l' || ch == L'L') {
print_text(L"l\r\n");
return REFRESH_LOCAL;
}
/* refresh all open dir windows
*/
if (ch == L'a' || ch == L'A') {
print_text(L"a\r\n");
return REFRESH_ALL;
}
}
}
/* ===================
** trim_trailing_slash
** removes final slash
** ===================
*/
static void trim_trailing_slash(wchar_t *path)
{
int len;
if (!path) {
return;
}
len = lstrlenW(path);
/* remove trailing slashes but
** keep drive root like C:\
*/
while (len > 3 &&
(path[len - 1] == L'\\' || path[len - 1] == L'/')) {
path[len - 1] = 0;
len--;
}
}
/* =======================
** hex_value
** returns hex digit value
** =======================
*/
static int hex_value(wchar_t ch)
{
/* decimal: 0-9 become 0-9
*/
if (ch >= L'0' && ch <= L'9') {
return (int)(ch - L'0');
}
/* hex: a-f become 10-15
*/
if (ch >= L'a' && ch <= L'f') {
return (int)(ch - L'a') + 10;
}
/* hex: A-F become 10-15
*/
if (ch >= L'A' && ch <= L'F') {
return (int)(ch - L'A') + 10;
}
return -1;
}
/* ======================
** starts_with_file_url
** checks file:/// prefix
** ======================
*/
static int starts_with_file_url(const wchar_t *url)
{
/* explorer folder URLs start
** with file:///
*/
static const wchar_t prefix[] = L"file:///";
int i;
/* cannot check a NULL URL
*/
if (!url) {
return 0;
}
/* reject anything that does
** not begin with file:///
*/
for (i = 0; prefix[i]; i++) {
if (url[i] != prefix[i]) {
return 0;
}
}
return 1;
}
/* =====================
** file_url_to_path
** converts file:/// URL
** =====================
*/
static int file_url_to_path (
const wchar_t *url,
wchar_t *path,
int path_count )
{
/* input URL pointer
** and output position
*/
const wchar_t *p;
int out = 0;
/* bad input check
*/
if (!url || !path || path_count <= 0) {
return 0;
}
/* start with empty path
*/
path[0] = 0;
/* only handle file:/// URLs
*/
if (!starts_with_file_url(url)) {
return 0;
}
/* skip past file:///
*/
p = url + 8;
/* copy URL into path buffer
** while space remains
*/
while (*p && out + 1 < path_count) {
/* URL slash becomes
** Windows backslash
*/
if (*p == L'/') {
path[out++] = L'\\';
p++;
/* decode %XX escape
** such as %20 for space
*/
} else if (*p == L'%' &&
hex_value(p[1]) >= 0 &&
hex_value(p[2]) >= 0) {
path[out++] = (wchar_t)((hex_value(p[1]) << 4) |
hex_value(p[2]));
p += 3;
/* normal character copy
*/
} else {
path[out++] = *p;
p++;
}
}
/* terminate path string
*/
path[out] = 0;
/* clean final slash
*/
trim_trailing_slash(path);
/* success if path is not empty
*/
return path[0] != 0;
}
/* ===================
** find_last_backslash
** finds final slash
** ===================
*/
static wchar_t *find_last_backslash(wchar_t *path)
{
/* last slash found
*/
wchar_t *last = NULL;
/* bad path check
*/
if (!path) {
return NULL;
}
/* walk through path
** remembering final backslash
*/
while (*path) {
if (*path == L'\\') {
last = path;
}
path++;
}
/* return final backslash
** or NULL if none found
*/
return last;
}
/* =================
** get_local_folder
** gets EXE folder path
** =================
*/
static int get_local_folder (
wchar_t *path,
int path_count )
{
/* final slash location
*/
wchar_t *slash;
/* bad output buffer
*/
if (!path || path_count <= 0) {
return 0;
}
/* get full EXE path
*/
if (!GetModuleFileNameW(NULL, path, (DWORD)path_count)) {
path[0] = 0;
return 0;
}
/* remove EXE filename
** leaving folder path
*/
slash = find_last_backslash(path);
if (slash) {
*slash = 0;
}
/* clean final slash
** if one remains
*/
trim_trailing_slash(path);
/* success if folder path
** is not empty
*/
return path[0] != 0;
}
/* =====================
** paths_match
** compares folder paths
** =====================
*/
static int paths_match (
const wchar_t *a,
const wchar_t *b )
{
/* local path copies
** for safe trimming
*/
wchar_t path_a[MAX_PATH];
wchar_t path_b[MAX_PATH];
/* bad path check
*/
if (!a || !b) {
return 0;
}
/* copy paths into local buffers
*/
lstrcpynW(path_a, a, (int)(sizeof(path_a) / sizeof(path_a[0])));
lstrcpynW(path_b, b, (int)(sizeof(path_b) / sizeof(path_b[0])));
/* normalize final slashes
*/
trim_trailing_slash(path_a);
trim_trailing_slash(path_b);
/* compare without case sensitivity
*/
return lstrcmpiW(path_a, path_b) == 0;
}
/* =====================
** print_hresult
** prints simple HRESULT
** =====================
*/
static void print_hresult (
const wchar_t *what,
HRESULT hr )
{
print_text(what);
print_text(L" failed: ");
print_hex32((unsigned long)hr);
print_newline();
}
/* ======================
** refresh_browser_window
** refreshes one Explorer
** ======================
*/
static int refresh_browser_window(IWebBrowserApp *browser)
{
HRESULT hr;
/* bad browser check
*/
if (!browser) {
return 0;
}
/* first refresh
*/
hr = IWebBrowserApp_Refresh(browser);
if (FAILED(hr)) {
print_hresult(L"IWebBrowserApp_Refresh", hr);
return 0;
}
/* give Explorer time to repaint
** before the safety refresh
*/
Sleep(EXPLORER_STABILIZE_MS);
/* second refresh acts like
** an automatic F5 safety pass
*/
hr = IWebBrowserApp_Refresh(browser);
if (FAILED(hr)) {
print_hresult(L"IWebBrowserApp_Refresh(second pass)", hr);
}
return 1;
}
/* ============================
** refresh_explorer_windows
** uses ShellWindows COM object
** ============================
*/
static int refresh_explorer_windows(int refresh_mode)
{
/* local shell state
*/
IShellWindows *shell_windows = NULL;
HRESULT hr;
long count = 0;
long i;
int refreshed = 0;
wchar_t local_folder[MAX_PATH];
/* COM init
*/
print_text(L"[2] Finding open Explorer windows...\r\n");
hr = CoInitialize(NULL);
if (FAILED(hr)) {
print_hresult(L"CoInitialize", hr);
return 0;
}
/* ShellWindows object
*/
print_text(L" Asking Windows for the Explorer window list...\r\n");
hr = CoCreateInstance (
&CLSID_ShellWindows,
NULL,
CLSCTX_ALL,
&IID_IShellWindows,
(void **)&shell_windows);
if (FAILED(hr) || !shell_windows) {
print_hresult(L"CoCreateInstance(CLSID_ShellWindows)", hr);
CoUninitialize();
return 0;
}
/* Shell window count
*/
hr = IShellWindows_get_Count(shell_windows, &count);
if (FAILED(hr)) {
print_hresult(L"IShellWindows_get_Count", hr);
IShellWindows_Release(shell_windows);
CoUninitialize();
return 0;
}
print_text(L" Found ");
print_long(count);
print_text(L" Explorer window(s).\r\n");
/* local folder
*/
local_folder[0] = 0;
if (refresh_mode == REFRESH_LOCAL) {
if (get_local_folder(local_folder, (int)(sizeof(local_folder) / sizeof(local_folder[0])))) {
print_text(L" Looking for Explorer window at: ");
print_text(local_folder);
print_newline();
} else {
print_text(L" Could not determine this program folder.\r\n");
}
}
/* refresh each Shell window
*/
for (i = 0; i < count; i++) {
/* item index
*/
VARIANT index;
IDispatch *disp = NULL;
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = i;
/* fetch Shell window
*/
hr = IShellWindows_Item(shell_windows, index, &disp);
if (SUCCEEDED(hr) && disp) {
/* browser interface
*/
IWebBrowserApp *browser = NULL;
hr = IDispatch_QueryInterface (
disp,
&IID_IWebBrowserApp,
(void **)&browser );
if (SUCCEEDED(hr) && browser) {
/* local folder match
*/
if (refresh_mode == REFRESH_LOCAL) {
BSTR url = NULL;
wchar_t folder[MAX_PATH];
folder[0] = 0;
if (FAILED(IWebBrowserApp_get_LocationURL(browser, &url)) ||
!url ||
!file_url_to_path (
url,
folder,
(int)(sizeof(folder) / sizeof(folder[0]))) ||
!paths_match(folder, local_folder)) {
if (url) {
SysFreeString(url);
}
IWebBrowserApp_Release(browser);
IDispatch_Release(disp);
VariantClear(&index);
continue;
}
SysFreeString(url);
}
/* window title
*/
BSTR location = NULL;
/* locationName is usually the folder/window title
*/
if (SUCCEEDED(IWebBrowserApp_get_LocationName(browser, &location)) &&
location) {
print_text(L" Refreshing Explorer window: ");
print_text(location);
print_newline();
SysFreeString(location);
} else {
print_text(L" Refreshing Explorer window #");
print_long(i + 1);
print_newline();
}
/* refresh window
** with a safety pass
*/
if (refresh_browser_window(browser)) {
refreshed++;
}
IWebBrowserApp_Release(browser);
} //end if (SUCCEEDED(hr) && browser)
IDispatch_Release(disp);
} // end if (SUCCEEDED(hr) && disp)
VariantClear(&index);
} // end for
/* COM cleanup
*/
IShellWindows_Release(shell_windows);
CoUninitialize();
return refreshed;
}
/* =============
** program entry
** =============
*/
int wmain(void)
{
/* local state
*/
int refreshed;
int refresh_mode;
/* banner
*/
print_text(L"IconReload\r\n");
print_text(L"==========\r\n\r\n");
/* refresh choice
*/
refresh_mode = ask_refresh_mode();
if (refresh_mode == REFRESH_EXIT) {
return 0;
}
/* icon and association refresh
** this is the main point of the
** program
*/
print_text(L"[1] Refreshing Windows icon and association cache...\r\n");
SHChangeNotify (
SHCNE_ASSOCCHANGED,
SHCNF_IDLIST | SHCNF_FLUSH,
NULL,
NULL);
print_text(L" Cache refresh notification sent.\r\n");
print_text(L" Waiting briefly for Explorer to settle.\r\n\r\n");
Sleep(EXPLORER_SETTLE_MS);
/* refresh open explorer windows so
** user does not have to hit F5
*/
refreshed = refresh_explorer_windows(refresh_mode);
/* final report
*/
print_text(L"\r\nDone.\r\n");
print_text(L"Explorer windows refreshed successfully: ");
print_long(refreshed);
print_newline();
if (refreshed == 0) {
print_text(L"Note: No matching Explorer window was found, or Explorer refresh failed.\r\n");
}
press_any_key();
return 0;
}