annotate SMART/Java/WindowsRegistry.java @ 22:1e3f2c2657a3

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:26:07 -0400
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 import java.lang.reflect.InvocationTargetException;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 import java.lang.reflect.Method;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 import java.util.HashMap;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 import java.util.Map;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 import java.util.ArrayList;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 import java.util.List;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 import java.util.prefs.Preferences;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 public class WindowsRegistry {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 public static final int HKEY_CURRENT_USER = 0x80000001;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 public static final int HKEY_LOCAL_MACHINE = 0x80000002;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 public static final int REG_SUCCESS = 0;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 public static final int REG_NOTFOUND = 2;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 public static final int REG_ACCESSDENIED = 5;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 private static final int KEY_ALL_ACCESS = 0xf003f;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 private static final int KEY_READ = 0x20019;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 private static Preferences userRoot = Preferences.userRoot();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 private static Preferences systemRoot = Preferences.systemRoot();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 private static Class<? extends Preferences> userClass = userRoot.getClass();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 private static Method regOpenKey = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 private static Method regCloseKey = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 private static Method regQueryValueEx = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 private static Method regEnumValue = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 private static Method regQueryInfoKey = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 private static Method regEnumKeyEx = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 private static Method regCreateKeyEx = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 private static Method regSetValueEx = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 private static Method regDeleteKey = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 private static Method regDeleteValue = null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 static {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 try {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 new Class[] { int.class, byte[].class, int.class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 regOpenKey.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 new Class[] { int.class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 regCloseKey.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 new Class[] { int.class, byte[].class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 regQueryValueEx.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 new Class[] { int.class, int.class, int.class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 regEnumValue.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1",
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 new Class[] { int.class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 regQueryInfoKey.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 regEnumKeyEx = userClass.getDeclaredMethod(
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 "WindowsRegEnumKeyEx", new Class[] { int.class, int.class,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 int.class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 regEnumKeyEx.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 regCreateKeyEx = userClass.getDeclaredMethod(
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 "WindowsRegCreateKeyEx", new Class[] { int.class,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 byte[].class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 regCreateKeyEx.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 regSetValueEx = userClass.getDeclaredMethod(
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 "WindowsRegSetValueEx", new Class[] { int.class,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 byte[].class, byte[].class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 regSetValueEx.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 regDeleteValue = userClass.getDeclaredMethod(
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 "WindowsRegDeleteValue", new Class[] { int.class,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 byte[].class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 regDeleteValue.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 regDeleteKey = userClass.getDeclaredMethod(
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 "WindowsRegDeleteKey", new Class[] { int.class,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 byte[].class });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 regDeleteKey.setAccessible(true);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 catch (Exception e) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 e.printStackTrace();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 private WindowsRegistry() { }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 /**
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 * Read a value from key and value name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 * @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 * @param key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 * @param valueName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 * @return the value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 * @throws IllegalArgumentException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 * @throws IllegalAccessException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 * @throws InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 */
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 public static String readString(int hkey, String key, String valueName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 if (hkey == HKEY_LOCAL_MACHINE) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 return readString(systemRoot, hkey, key, valueName);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 else if (hkey == HKEY_CURRENT_USER) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 return readString(userRoot, hkey, key, valueName);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 else {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 throw new IllegalArgumentException("hkey=" + hkey);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 /**
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 * Read value(s) and value name(s) form given key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 * @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 * @param key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 * @return the value name(s) plus the value(s)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 * @throws IllegalArgumentException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 * @throws IllegalAccessException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 * @throws InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 */
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 public static Map<String, String> readStringValues(int hkey, String key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 if (hkey == HKEY_LOCAL_MACHINE) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 return readStringValues(systemRoot, hkey, key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 else if (hkey == HKEY_CURRENT_USER) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 return readStringValues(userRoot, hkey, key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 else {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 throw new IllegalArgumentException("hkey=" + hkey);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 /**
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 * Read the value name(s) from a given key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 * @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 * @param key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 * @return the value name(s)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 * @throws IllegalArgumentException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 * @throws IllegalAccessException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 * @throws InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 */
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 public static List<String> readStringSubKeys(int hkey, String key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 if (hkey == HKEY_LOCAL_MACHINE) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 return readStringSubKeys(systemRoot, hkey, key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 else if (hkey == HKEY_CURRENT_USER) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 return readStringSubKeys(userRoot, hkey, key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 else {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 throw new IllegalArgumentException("hkey=" + hkey);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 /**
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 * Create a key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 * @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 * @param key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 * @throws IllegalArgumentException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 * @throws IllegalAccessException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 * @throws InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 */
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 public static void createKey(int hkey, String key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 int [] ret;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 if (hkey == HKEY_LOCAL_MACHINE) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 ret = createKey(systemRoot, hkey, key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 regCloseKey.invoke(systemRoot, new Object[] { new Integer(ret[0]) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 else if (hkey == HKEY_CURRENT_USER) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 ret = createKey(userRoot, hkey, key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 regCloseKey.invoke(userRoot, new Object[] { new Integer(ret[0]) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 else {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 throw new IllegalArgumentException("hkey=" + hkey);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 if (ret[1] != REG_SUCCESS) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 throw new IllegalArgumentException("rc=" + ret[1] + " key=" + key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 /**
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 * Write a value in a given key/value name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 * @param hkey
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 * @param key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 * @param valueName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 * @param value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 * @throws IllegalArgumentException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 * @throws IllegalAccessException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 * @throws InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 */
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 public static void writeStringValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 (int hkey, String key, String valueName, String value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 if (hkey == HKEY_LOCAL_MACHINE) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 writeStringValue(systemRoot, hkey, key, valueName, value);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 else if (hkey == HKEY_CURRENT_USER) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 writeStringValue(userRoot, hkey, key, valueName, value);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 else {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 throw new IllegalArgumentException("hkey=" + hkey);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 /**
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 * Delete a given key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 * @param hkey
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 * @param key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 * @throws IllegalArgumentException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 * @throws IllegalAccessException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 * @throws InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 */
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 public static void deleteKey(int hkey, String key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 int rc = -1;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 if (hkey == HKEY_LOCAL_MACHINE) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 rc = deleteKey(systemRoot, hkey, key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 else if (hkey == HKEY_CURRENT_USER) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 rc = deleteKey(userRoot, hkey, key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 if (rc != REG_SUCCESS) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 throw new IllegalArgumentException("rc=" + rc + " key=" + key);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 /**
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 * delete a value from a given key/value name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 * @param hkey
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 * @param key
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 * @param value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 * @throws IllegalArgumentException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 * @throws IllegalAccessException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 * @throws InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 */
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 public static void deleteValue(int hkey, String key, String value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 int rc = -1;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 if (hkey == HKEY_LOCAL_MACHINE) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 rc = deleteValue(systemRoot, hkey, key, value);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 else if (hkey == HKEY_CURRENT_USER) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 rc = deleteValue(userRoot, hkey, key, value);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 if (rc != REG_SUCCESS) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 throw new IllegalArgumentException("rc=" + rc + " key=" + key + " value=" + value);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 // =====================
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 private static int deleteValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 (Preferences root, int hkey, String key, String value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 if (handles[1] != REG_SUCCESS) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 return handles[1]; // can be REG_NOTFOUND, REG_ACCESSDENIED
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 int rc =((Integer) regDeleteValue.invoke(root,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 new Integer(handles[0]), toCstr(value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 })).intValue();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 return rc;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 private static int deleteKey(Preferences root, int hkey, String key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 int rc =((Integer) regDeleteKey.invoke(root,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 new Object[] { new Integer(hkey), toCstr(key) })).intValue();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 return rc; // can REG_NOTFOUND, REG_ACCESSDENIED, REG_SUCCESS
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 private static String readString(Preferences root, int hkey, String key, String value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 if (handles[1] != REG_SUCCESS) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 return null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 new Integer(handles[0]), toCstr(value) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 return (valb != null ? new String(valb).trim() : null);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 private static Map<String,String> readStringValues
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 (Preferences root, int hkey, String key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 HashMap<String, String> results = new HashMap<String,String>();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 if (handles[1] != REG_SUCCESS) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 return null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 int[] info = (int[]) regQueryInfoKey.invoke(root,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 new Object[] { new Integer(handles[0]) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 int count = info[2]; // count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 int maxlen = info[3]; // value length max
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 for(int index=0; index<count; index++) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 byte[] name = (byte[]) regEnumValue.invoke(root, new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 new Integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 (handles[0]), new Integer(index), new Integer(maxlen + 1)});
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 String value = readString(hkey, key, new String(name));
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 results.put(new String(name).trim(), value);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 return results;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 private static List<String> readStringSubKeys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 (Preferences root, int hkey, String key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 List<String> results = new ArrayList<String>();
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 new Integer(hkey), toCstr(key), new Integer(KEY_READ)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 if (handles[1] != REG_SUCCESS) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335 return null;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 int[] info = (int[]) regQueryInfoKey.invoke(root,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 new Object[] { new Integer(handles[0]) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 int count = info[2]; // count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 int maxlen = info[3]; // value length max
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 for(int index=0; index<count; index++) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 byte[] name = (byte[]) regEnumKeyEx.invoke(root, new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 new Integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345 (handles[0]), new Integer(index), new Integer(maxlen + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347 results.add(new String(name).trim());
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349 regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350 return results;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353 private static int [] createKey(Preferences root, int hkey, String key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 return (int[]) regCreateKeyEx.invoke(root,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 new Object[] { new Integer(hkey), toCstr(key) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361 private static void writeStringValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362 (Preferences root, int hkey, String key, String valueName, String value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
363 throws IllegalArgumentException, IllegalAccessException,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
364 InvocationTargetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
365 {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
366 int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
367 new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
368
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
369 regSetValueEx.invoke(root,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
370 new Object[] {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
371 new Integer(handles[0]), toCstr(valueName), toCstr(value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
372 });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
373 regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
374 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
375
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
376 // utility
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
377 private static byte[] toCstr(String str) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
378 byte[] result = new byte[str.length() + 1];
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
379
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
380 for (int i = 0; i < str.length(); i++) {
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
381 result[i] = (byte) str.charAt(i);
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
382 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
383 result[str.length()] = 0;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
384 return result;
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
385 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
386 }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
387