1 /**
2  * ICOファイルに関する共通処理
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.registory;
8 
9 
10 version (NON_WINREG) {
11 } else {
12 	version = IS_WINREG;
13 }
14 
15 version (Windows) {
16 	version (IS_WINREG) {
17 		pragma(lib, "advapi32");
18 
19 		private static import core.sys.windows.w32api;
20 		private static import core.sys.windows.winbase;
21 		private static import core.sys.windows.windef;
22 		private static import core.sys.windows.winerror;
23 		private static import core.sys.windows.winnt;
24 		private static import core.sys.windows.winreg;
25 
26 		static assert(core.sys.windows.w32api._WIN32_WINNT >= 0x502);
27 
28 		/**
29 		 * WindowsレジストリのHKEY
30 		 */
31 		public enum windows_shortcut_arrow_hKey = core.sys.windows.winreg.HKEY_LOCAL_MACHINE;
32 
33 		/**
34 		 * Windowsレジストリのキーのタイプ
35 		 */
36 		public enum windows_shortcut_arrow_dwType = core.sys.windows.winreg.REG_EXPAND_SZ;
37 	}
38 }
39 
40 /**
41  * Windowsレジストリのサブキーの名前
42  */
43 public enum windows_shortcut_arrow_SubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Icons\0"w;
44 
45 /**
46  * Windowsレジストリの値の名前
47  */
48 public enum windows_shortcut_arrow_ValueName = "29\0"w;
49 
50 /**
51  * レジストリにICOファイルのPATHを登録する。
52  */
53 version (Windows)
54 version (IS_WINREG)
55 extern (C)
56 nothrow @nogc
57 public bool add_registory(const core.sys.windows.winnt.LPCWSTR ico_path)
58 
59 	do
60 	{
61 		core.sys.windows.windef.HKEY hkResult = void;
62 
63 		core.sys.windows.windef.DWORD cbData = cast(core.sys.windows.windef.DWORD)(core.sys.windows.winbase.lstrlenW(ico_path) * wchar.sizeof);
64 
65 		if (cbData < 1) {
66 			return false;
67 		}
68 
69 		core.sys.windows.windef.LONG result = core.sys.windows.winreg.RegCreateKeyExW(.windows_shortcut_arrow_hKey, &(.windows_shortcut_arrow_SubKey[0]), 0, null, core.sys.windows.winnt.REG_OPTION_NON_VOLATILE, core.sys.windows.winnt.KEY_SET_VALUE | core.sys.windows.winnt.KEY_WOW64_64KEY, null, &hkResult, null);
70 
71 		if (result != core.sys.windows.winerror.ERROR_SUCCESS) {
72 			return false;
73 		}
74 
75 		if (core.sys.windows.winreg.RegSetValueExW(hkResult, &(.windows_shortcut_arrow_ValueName[0]), 0, .windows_shortcut_arrow_dwType, cast(const (core.sys.windows.windef.BYTE)*)(ico_path), cbData) != core.sys.windows.winerror.ERROR_SUCCESS) {
76 			core.sys.windows.winreg.RegCloseKey(hkResult);
77 
78 			return false;
79 		}
80 
81 		return core.sys.windows.winreg.RegCloseKey(hkResult) == core.sys.windows.winerror.ERROR_SUCCESS;
82 	}
83 
84 /**
85  * レジストリの特定の値を削除する
86  */
87 version (Windows)
88 version (IS_WINREG)
89 extern (C)
90 nothrow @nogc
91 public bool delete_registory()
92 
93 	do
94 	{
95 		core.sys.windows.windef.HKEY hkResult = void;
96 
97 		core.sys.windows.windef.LONG result = core.sys.windows.winreg.RegOpenKeyExW(.windows_shortcut_arrow_hKey, &(.windows_shortcut_arrow_SubKey[0]), 0, core.sys.windows.winnt.KEY_SET_VALUE | core.sys.windows.winnt.KEY_WOW64_64KEY, &hkResult);
98 
99 		if (result != core.sys.windows.winerror.ERROR_SUCCESS) {
100 			return false;
101 		}
102 
103 		if (core.sys.windows.winreg.RegDeleteValueW(hkResult, &(.windows_shortcut_arrow_ValueName[0])) != core.sys.windows.winerror.ERROR_SUCCESS) {
104 			core.sys.windows.winreg.RegCloseKey(hkResult);
105 
106 			return false;
107 		}
108 
109 		return core.sys.windows.winreg.RegCloseKey(hkResult) == core.sys.windows.winerror.ERROR_SUCCESS;
110 	}