1 /**
2  * アイコンキャッシュをクリアする
3  *
4  * Author: $(LINK2 https://twitter.com/dokutoku3, dokutoku)
5  * License: $(LINK2 https://creativecommons.org/publicdomain/zero/1.0/, CC0 1.0 Universal)
6  */
7 module remove_shortcut_arrow.cache;
8 
9 
10 version (Windows):
11 
12 private static import core.sys.windows.winbase;
13 private static import core.sys.windows.windef;
14 private static import core.sys.windows.winnt;
15 private static import remove_shortcut_arrow.command;
16 private static import remove_shortcut_arrow.path;
17 
18 private enum pre_local_setting = "\\Local Settings\0"w;
19 
20 private enum pre_application_data = "\\Application Data\0"w;
21 
22 private enum pre_iconcachedb = "\\IconCache.db\0"w;
23 
24 private enum iconcache_path_buffer_length = 1024 + "\0".length;
25 
26 /**
27  * 効果不明
28  */
29 extern (C)
30 nothrow @nogc
31 public bool remove_current_user_iconcache()
32 
33 	do
34 	{
35 		enum pre_str_length = (.pre_local_setting.length - "\0".length) + (.pre_application_data.length - "\0".length) + (.pre_iconcachedb.length - "\0".length);
36 
37 		wchar[.iconcache_path_buffer_length] userprofile_path = void;
38 
39 		if (core.sys.windows.winbase.GetEnvironmentVariableW(&("UserProfile\0"w[0]), &(userprofile_path[0]), cast(core.sys.windows.windef.DWORD)(userprofile_path.length - pre_str_length - remove_shortcut_arrow.path.windows_extra_path_prefix_str_length)) <= 0) {
40 			return false;
41 		}
42 
43 		int length = core.sys.windows.winbase.lstrlenW(&(userprofile_path[0]));
44 
45 		if (length <= 0) {
46 			return false;
47 		}
48 
49 		if (userprofile_path[length - 1] == '\\') {
50 			return false;
51 		}
52 
53 		wchar[.iconcache_path_buffer_length] iconcache_path1 = void;
54 
55 		if (length > (core.sys.windows.windef.MAX_PATH - pre_str_length - "\0".length)) {
56 			iconcache_path1 = remove_shortcut_arrow.path.windows_extra_path_prefix;
57 		} else {
58 			iconcache_path1[0] = '\0';
59 		}
60 
61 		core.sys.windows.winbase.lstrcatW(&(iconcache_path1[0]), &(userprofile_path[0]));
62 		core.sys.windows.winbase.lstrcatW(&(iconcache_path1[0]), &(.pre_local_setting[0]));
63 
64 		version (none) {
65 			wchar[.iconcache_path_buffer_length] iconcache_path2 = iconcache_path1;
66 		}
67 
68 		core.sys.windows.winbase.lstrcatW(&(iconcache_path1[0]), &(.pre_application_data[0]));
69 		core.sys.windows.winbase.lstrcatW(&(iconcache_path1[0]), &(.pre_iconcachedb[0]));
70 
71 		if (core.sys.windows.winbase.DeleteFileW(&(iconcache_path1[0])) == 0) {
72 			version (none) {
73 				/*
74 				 * 削除に失敗した場合は、%UserProfile%\Application Data\IconCache.dbを探索する
75 				 *
76 				 * See_Also:
77 				 *      https://www.inasoft.org/webhelp/sdfr4/HLP000100.html
78 				 */
79 				core.sys.windows.winbase.lstrcatW(&(iconcache_path2[0]), &(.pre_application_data[0]));
80 				core.sys.windows.winbase.lstrcatW(&(iconcache_path2[0]), &(.pre_iconcachedb[0]));
81 
82 				if (core.sys.windows.winbase.DeleteFileW(&(iconcache_path2[0])) == 0) {
83 					return false;
84 				}
85 			} else {
86 				return false;
87 			}
88 		}
89 
90 		return true;
91 	}
92 
93 version (CLEAR_ICON_CACHE)
94 extern (Windows)
95 nothrow @nogc
96 int WinMain(core.sys.windows.windef.HINSTANCE hInstance, core.sys.windows.windef.HINSTANCE hPrevInstance, core.sys.windows.winnt.LPSTR pCmdLine, int nCmdShow)
97 
98 	do
99 	{
100 		return (remove_shortcut_arrow.command.exec_clear_icon_cache()) ? (0) : (1);
101 	}