1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.util.win32;
17
18 import org.eclipse.swt.internal.win32.OS;
19 import org.eclipse.swt.internal.win32.TCHAR;
20 import org.seasar.uruma.core.UrumaConstants;
21 import org.seasar.uruma.exception.Win32ApiException;
22 import org.seasar.uruma.util.AssertionUtil;
23
24
25
26
27
28
29 public class RegistryUtil {
30 private RegistryUtil() {
31
32 }
33
34
35
36
37 public static int HKEY_LOCAL_MACHINE = OS.HKEY_LOCAL_MACHINE;
38
39
40
41
42 public static int HKEY_CLASSES_ROOT = OS.HKEY_CLASSES_ROOT;
43
44
45
46
47 public static int HKEY_CURRENT_USER = OS.HKEY_CURRENT_USER;
48
49 private static int KEY_ALL_ACCESS = 0xF003F;
50
51
52
53
54
55
56
57
58
59
60
61 public static RegistryHandle regOpenKey(final int hKey, final String entry) {
62 AssertionUtil.assertNotNull("entry", entry);
63 RegistryHandle handle = new RegistryHandle();
64 int retCode = OS.RegOpenKeyEx(hKey, new TCHAR(OS.CP_INSTALLED, entry,
65 true), 0, OS.KEY_READ, handle.getPhkResult());
66 if (retCode == 0) {
67 return handle;
68 } else {
69 throw new Win32ApiException("RegOpenKeyEX", retCode);
70 }
71 }
72
73
74
75
76
77
78
79 public static void regCloseKey(final RegistryHandle handle) {
80 AssertionUtil.assertNotNull("handle", handle);
81 if (handle.getPointer() != 0) {
82 OS.RegCloseKey(handle.getPointer());
83 }
84 }
85
86
87
88
89
90
91
92
93
94
95 public static String regQueryValue(final RegistryHandle handle,
96 final String valueName) {
97 AssertionUtil.assertNotNull("handle", handle);
98 AssertionUtil.assertNotNull("valueName", valueName);
99
100 TCHAR buf = new TCHAR(OS.CP_INSTALLED, 256);
101 final int[] len = new int[] { 256 };
102 int retCode = OS.RegQueryValueEx(handle.getPointer(), new TCHAR(
103 OS.CP_INSTALLED, valueName, true), 0, null, buf, len);
104 if (retCode == 0) {
105 return buf.toString(0, buf.strlen());
106 } else {
107 regCloseKey(handle);
108 throw new Win32ApiException("RegQueryValueEx", retCode);
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public static String getRegistryValue(final int hKey, final String entry,
125 final String key) {
126 try {
127 RegistryHandle handle = RegistryUtil.regOpenKey(hKey, entry);
128 String value = RegistryUtil.regQueryValue(handle, key);
129 RegistryUtil.regCloseKey(handle);
130 return value;
131 } catch (Win32ApiException ex) {
132 return null;
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145
146 public static String getRegistryValue(final int hKey, final String entry) {
147 return getRegistryValue(hKey, entry, UrumaConstants.NULL_STRING);
148 }
149 }