~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to src/processing/app/windows/Registry.java

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package processing.app.windows;
 
2
 
 
3
import java.io.UnsupportedEncodingException;
 
4
import java.util.HashMap;
 
5
import java.util.TreeMap;
 
6
import java.util.TreeSet;
 
7
 
 
8
import com.sun.jna.ptr.IntByReference;
 
9
 
 
10
/**
 
11
 * Methods for accessing the Windows Registry. Only String and DWORD values supported at the moment.
 
12
 */
 
13
public class Registry {
 
14
  public static enum REGISTRY_ROOT_KEY{CLASSES_ROOT, CURRENT_USER, LOCAL_MACHINE, USERS};
 
15
  private final static HashMap<REGISTRY_ROOT_KEY, Integer> rootKeyMap = new HashMap<REGISTRY_ROOT_KEY, Integer>();
 
16
 
 
17
  static {
 
18
    rootKeyMap.put(REGISTRY_ROOT_KEY.CLASSES_ROOT, WINREG.HKEY_CLASSES_ROOT);
 
19
    rootKeyMap.put(REGISTRY_ROOT_KEY.CURRENT_USER, WINREG.HKEY_CURRENT_USER);
 
20
    rootKeyMap.put(REGISTRY_ROOT_KEY.LOCAL_MACHINE, WINREG.HKEY_LOCAL_MACHINE);
 
21
    rootKeyMap.put(REGISTRY_ROOT_KEY.USERS, WINREG.HKEY_USERS);
 
22
  }
 
23
 
 
24
  /**
 
25
   * Testing.
 
26
   *
 
27
   * @param args arguments
 
28
   * @throws java.lang.Exception on error
 
29
   */
 
30
  public static void main(String[] args) throws Exception {
 
31
  }
 
32
 
 
33
  /**
 
34
   * Gets one of the root keys.
 
35
   *
 
36
   * @param key key type
 
37
   * @return root key
 
38
   */
 
39
  private static int getRegistryRootKey(REGISTRY_ROOT_KEY key) {
 
40
    Advapi32 advapi32;
 
41
    IntByReference pHandle;
 
42
    int handle = 0;
 
43
 
 
44
    advapi32 = Advapi32.INSTANCE;
 
45
    pHandle = new IntByReference();
 
46
 
 
47
    if(advapi32.RegOpenKeyEx(rootKeyMap.get(key), null, 0, 0, pHandle) == WINERROR.ERROR_SUCCESS) {
 
48
      handle = pHandle.getValue();
 
49
    }
 
50
    return(handle);
 
51
  }
 
52
 
 
53
  /**
 
54
   * Opens a key.
 
55
   *
 
56
   * @param rootKey root key
 
57
   * @param subKeyName name of the key
 
58
   * @param access access mode
 
59
   * @return handle to the key or 0
 
60
   */
 
61
  private static int openKey(REGISTRY_ROOT_KEY rootKey, String subKeyName, int access) {
 
62
    Advapi32 advapi32;
 
63
    IntByReference pHandle;
 
64
    int rootKeyHandle;
 
65
 
 
66
    advapi32 = Advapi32.INSTANCE;
 
67
    rootKeyHandle = getRegistryRootKey(rootKey);
 
68
    pHandle = new IntByReference();
 
69
 
 
70
    if(advapi32.RegOpenKeyEx(rootKeyHandle, subKeyName, 0, access, pHandle) == WINERROR.ERROR_SUCCESS) {
 
71
      return(pHandle.getValue());
 
72
 
 
73
    } else {
 
74
      return(0);
 
75
    }
 
76
  }
 
77
 
 
78
  /**
 
79
   * Converts a Windows buffer to a Java String.
 
80
   *
 
81
   * @param buf buffer
 
82
   * @throws java.io.UnsupportedEncodingException on error
 
83
   * @return String
 
84
   */
 
85
  private static String convertBufferToString(byte[] buf) throws UnsupportedEncodingException {
 
86
    return(new String(buf, 0, buf.length - 2, "UTF-16LE"));
 
87
  }
 
88
 
 
89
  /**
 
90
   * Converts a Windows buffer to an int.
 
91
   *
 
92
   * @param buf buffer
 
93
   * @return int
 
94
   */
 
95
  private static int convertBufferToInt(byte[] buf) {
 
96
   return(((int)(buf[0] & 0xff)) + (((int)(buf[1] & 0xff)) << 8) + (((int)(buf[2] & 0xff)) << 16) + (((int)(buf[3] & 0xff)) << 24));
 
97
  }
 
98
 
 
99
  /**
 
100
   * Read a String value.
 
101
   *
 
102
   * @param rootKey root key
 
103
   * @param subKeyName key name
 
104
   * @param name value name
 
105
   * @throws java.io.UnsupportedEncodingException on error
 
106
   * @return String or null
 
107
   */
 
108
  public static String getStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) throws UnsupportedEncodingException {
 
109
    Advapi32 advapi32;
 
110
    IntByReference pType, lpcbData;
 
111
    byte[] lpData = new byte[1];
 
112
    int handle = 0;
 
113
    String ret = null;
 
114
 
 
115
    advapi32 = Advapi32.INSTANCE;
 
116
    pType = new IntByReference();
 
117
    lpcbData = new IntByReference();
 
118
    handle = openKey(rootKey, subKeyName, WINNT.KEY_READ);
 
119
 
 
120
    if(handle != 0) {
 
121
 
 
122
      if(advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) == WINERROR.ERROR_MORE_DATA) {
 
123
        lpData = new byte[lpcbData.getValue()];
 
124
 
 
125
        if(advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) == WINERROR.ERROR_SUCCESS) {
 
126
          ret = convertBufferToString(lpData);
 
127
        }
 
128
      }
 
129
      advapi32.RegCloseKey(handle);
 
130
    }
 
131
    return(ret);
 
132
  }
 
133
 
 
134
  /**
 
135
   * Read an int value.
 
136
   *
 
137
   *
 
138
   * @return int or 0
 
139
   * @param rootKey root key
 
140
   * @param subKeyName key name
 
141
   * @param name value name
 
142
   */
 
143
  public static int getIntValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) {
 
144
    Advapi32 advapi32;
 
145
    IntByReference pType, lpcbData;
 
146
    byte[] lpData = new byte[1];
 
147
    int handle = 0;
 
148
    int ret = 0;
 
149
 
 
150
    advapi32 = Advapi32.INSTANCE;
 
151
    pType = new IntByReference();
 
152
    lpcbData = new IntByReference();
 
153
    handle = openKey(rootKey, subKeyName, WINNT.KEY_READ);
 
154
 
 
155
    if(handle != 0) {
 
156
 
 
157
      if(advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) == WINERROR.ERROR_MORE_DATA) {
 
158
        lpData = new byte[lpcbData.getValue()];
 
159
 
 
160
        if(advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) == WINERROR.ERROR_SUCCESS) {
 
161
          ret = convertBufferToInt(lpData);
 
162
        }
 
163
      }
 
164
      advapi32.RegCloseKey(handle);
 
165
    }
 
166
    return(ret);
 
167
  }
 
168
 
 
169
  /**
 
170
   * Delete a value.
 
171
   *
 
172
   * @param rootKey root key
 
173
   * @param subKeyName key name
 
174
   * @param name value name
 
175
   * @return true on success
 
176
   */
 
177
  public static boolean deleteValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) {
 
178
    Advapi32 advapi32;
 
179
    int handle;
 
180
    boolean ret = true;
 
181
 
 
182
    advapi32 = Advapi32.INSTANCE;
 
183
 
 
184
    handle = openKey(rootKey, subKeyName, WINNT.KEY_READ | WINNT.KEY_WRITE);
 
185
 
 
186
    if(handle != 0) {
 
187
      if(advapi32.RegDeleteValue(handle, name) == WINERROR.ERROR_SUCCESS) {
 
188
        ret = true;
 
189
      }
 
190
      advapi32.RegCloseKey(handle);
 
191
    }
 
192
    return(ret);
 
193
  }
 
194
 
 
195
  /**
 
196
   * Writes a String value.
 
197
   *
 
198
   * @param rootKey root key
 
199
   * @param subKeyName key name
 
200
   * @param name value name
 
201
   * @param value value
 
202
   * @throws java.io.UnsupportedEncodingException on error
 
203
   * @return true on success
 
204
   */
 
205
  public static boolean setStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name, String value) throws UnsupportedEncodingException {
 
206
    Advapi32 advapi32;
 
207
    int handle;
 
208
    byte[] data;
 
209
    boolean ret = false;
 
210
 
 
211
    // appears to be Java 1.6 syntax, removing [fry]
 
212
    //data = Arrays.copyOf(value.getBytes("UTF-16LE"), value.length() * 2 + 2);
 
213
    data = new byte[value.length() * 2 + 2];
 
214
    byte[] src = value.getBytes("UTF-16LE");
 
215
    System.arraycopy(src, 0, data, 0, src.length);
 
216
 
 
217
    advapi32 = Advapi32.INSTANCE;
 
218
    handle = openKey(rootKey, subKeyName, WINNT.KEY_READ | WINNT.KEY_WRITE);
 
219
 
 
220
    if(handle != 0) {
 
221
      if(advapi32.RegSetValueEx(handle, name, 0, WINNT.REG_SZ, data, data.length) == WINERROR.ERROR_SUCCESS) {
 
222
        ret = true;
 
223
      }
 
224
      advapi32.RegCloseKey(handle);
 
225
    }
 
226
    return(ret);
 
227
  }
 
228
 
 
229
  /**
 
230
   * Writes an int value.
 
231
   *
 
232
   *
 
233
   * @return true on success
 
234
   * @param rootKey root key
 
235
   * @param subKeyName key name
 
236
   * @param name value name
 
237
   * @param value value
 
238
   */
 
239
  public static boolean setIntValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name, int value) {
 
240
    Advapi32 advapi32;
 
241
    int handle;
 
242
    byte[] data;
 
243
    boolean ret = false;
 
244
 
 
245
    data = new byte[4];
 
246
    data[0] = (byte)(value & 0xff);
 
247
    data[1] = (byte)((value >> 8) & 0xff);
 
248
    data[2] = (byte)((value >> 16) & 0xff);
 
249
    data[3] = (byte)((value >> 24) & 0xff);
 
250
    advapi32 = Advapi32.INSTANCE;
 
251
    handle = openKey(rootKey, subKeyName, WINNT.KEY_READ | WINNT.KEY_WRITE);
 
252
 
 
253
    if(handle != 0) {
 
254
 
 
255
      if(advapi32.RegSetValueEx(handle, name, 0, WINNT.REG_DWORD, data, data.length) == WINERROR.ERROR_SUCCESS) {
 
256
        ret = true;
 
257
      }
 
258
      advapi32.RegCloseKey(handle);
 
259
    }
 
260
    return(ret);
 
261
  }
 
262
 
 
263
  /**
 
264
   * Check for existence of a value.
 
265
   *
 
266
   * @param rootKey root key
 
267
   * @param subKeyName key name
 
268
   * @param name value name
 
269
   * @return true if exists
 
270
   */
 
271
  public static boolean valueExists(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) {
 
272
    Advapi32 advapi32;
 
273
    IntByReference pType, lpcbData;
 
274
    byte[] lpData = new byte[1];
 
275
    int handle = 0;
 
276
    boolean ret = false;
 
277
 
 
278
    advapi32 = Advapi32.INSTANCE;
 
279
    pType = new IntByReference();
 
280
    lpcbData = new IntByReference();
 
281
    handle = openKey(rootKey, subKeyName, WINNT.KEY_READ);
 
282
 
 
283
    if(handle != 0) {
 
284
 
 
285
      if(advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) != WINERROR.ERROR_FILE_NOT_FOUND) {
 
286
        ret = true;
 
287
 
 
288
      } else {
 
289
        ret = false;
 
290
      }
 
291
      advapi32.RegCloseKey(handle);
 
292
    }
 
293
    return(ret);
 
294
  }
 
295
 
 
296
  /**
 
297
   * Create a new key.
 
298
   *
 
299
   * @param rootKey root key
 
300
   * @param parent name of parent key
 
301
   * @param name key name
 
302
   * @return true on success
 
303
   */
 
304
  public static boolean createKey(REGISTRY_ROOT_KEY rootKey, String parent, String name) {
 
305
    Advapi32 advapi32;
 
306
    IntByReference hkResult, dwDisposition;
 
307
    int handle = 0;
 
308
    boolean ret = false;
 
309
 
 
310
    advapi32 = Advapi32.INSTANCE;
 
311
    hkResult = new IntByReference();
 
312
    dwDisposition = new IntByReference();
 
313
    handle = openKey(rootKey, parent, WINNT.KEY_READ);
 
314
 
 
315
    if(handle != 0) {
 
316
 
 
317
      if(advapi32.RegCreateKeyEx(handle, name, 0, null, WINNT.REG_OPTION_NON_VOLATILE, WINNT.KEY_READ, null,
 
318
         hkResult, dwDisposition) == WINERROR.ERROR_SUCCESS) {
 
319
        ret = true;
 
320
        advapi32.RegCloseKey(hkResult.getValue());
 
321
 
 
322
      } else {
 
323
        ret = false;
 
324
      }
 
325
      advapi32.RegCloseKey(handle);
 
326
    }
 
327
    return(ret);
 
328
  }
 
329
 
 
330
  /**
 
331
   * Delete a key.
 
332
   *
 
333
   * @param rootKey root key
 
334
   * @param parent name of parent key
 
335
   * @param name key name
 
336
   * @return true on success
 
337
   */
 
338
  public static boolean deleteKey(REGISTRY_ROOT_KEY rootKey, String parent, String name) {
 
339
    Advapi32 advapi32;
 
340
    int handle = 0;
 
341
    boolean ret = false;
 
342
 
 
343
    advapi32 = Advapi32.INSTANCE;
 
344
    handle = openKey(rootKey, parent, WINNT.KEY_READ);
 
345
 
 
346
    if(handle != 0) {
 
347
 
 
348
      if(advapi32.RegDeleteKey(handle, name) == WINERROR.ERROR_SUCCESS) {
 
349
        ret = true;
 
350
 
 
351
      } else {
 
352
        ret = false;
 
353
      }
 
354
      advapi32.RegCloseKey(handle);
 
355
    }
 
356
    return(ret);
 
357
  }
 
358
 
 
359
  /**
 
360
   * Get all sub keys of a key.
 
361
   *
 
362
   * @param rootKey root key
 
363
   * @param parent key name
 
364
   * @return array with all sub key names
 
365
   */
 
366
  public static String[] getSubKeys(REGISTRY_ROOT_KEY rootKey, String parent) {
 
367
    Advapi32 advapi32;
 
368
    int handle = 0, dwIndex;
 
369
    char[] lpName;
 
370
    IntByReference lpcName;
 
371
    WINBASE.FILETIME lpftLastWriteTime;
 
372
    TreeSet<String> subKeys = new TreeSet<String>();
 
373
 
 
374
    advapi32 = Advapi32.INSTANCE;
 
375
    handle = openKey(rootKey, parent, WINNT.KEY_READ);
 
376
    lpName = new char[256];
 
377
    lpcName = new IntByReference(256);
 
378
    lpftLastWriteTime = new WINBASE.FILETIME();
 
379
 
 
380
    if(handle != 0) {
 
381
      dwIndex = 0;
 
382
 
 
383
      while(advapi32.RegEnumKeyEx(handle, dwIndex, lpName, lpcName, null,
 
384
            null, null, lpftLastWriteTime) == WINERROR.ERROR_SUCCESS) {
 
385
        subKeys.add(new String(lpName, 0, lpcName.getValue()));
 
386
        lpcName.setValue(256);
 
387
        dwIndex++;
 
388
      }
 
389
      advapi32.RegCloseKey(handle);
 
390
    }
 
391
 
 
392
    return(subKeys.toArray(new String[]{}));
 
393
  }
 
394
 
 
395
  /**
 
396
   * Get all values under a key.
 
397
   *
 
398
   * @param rootKey root key
 
399
   * @param key jey name
 
400
   * @throws java.io.UnsupportedEncodingException on error
 
401
   * @return TreeMap with name and value pairs
 
402
   */
 
403
  public static TreeMap<String, Object> getValues(REGISTRY_ROOT_KEY rootKey, String key) throws UnsupportedEncodingException {
 
404
    Advapi32 advapi32;
 
405
    int handle = 0, dwIndex, result = 0;
 
406
    char[] lpValueName;
 
407
    byte[] lpData;
 
408
    IntByReference lpcchValueName, lpType, lpcbData;
 
409
    String name;
 
410
    TreeMap<String, Object> values = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
 
411
 
 
412
    advapi32 = Advapi32.INSTANCE;
 
413
    handle = openKey(rootKey, key, WINNT.KEY_READ);
 
414
    lpValueName = new char[16384];
 
415
    lpcchValueName = new IntByReference(16384);
 
416
    lpType = new IntByReference();
 
417
    lpData = new byte[1];
 
418
    lpcbData = new IntByReference();
 
419
 
 
420
    if(handle != 0) {
 
421
      dwIndex = 0;
 
422
 
 
423
      do {
 
424
        lpcbData.setValue(0);
 
425
        result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null,
 
426
          lpType, lpData, lpcbData);
 
427
 
 
428
        if(result == WINERROR.ERROR_MORE_DATA) {
 
429
          lpData = new byte[lpcbData.getValue()];
 
430
          lpcchValueName =  new IntByReference(16384);
 
431
          result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null,
 
432
            lpType, lpData, lpcbData);
 
433
 
 
434
          if(result == WINERROR.ERROR_SUCCESS) {
 
435
            name = new String(lpValueName, 0, lpcchValueName.getValue());
 
436
 
 
437
            switch(lpType.getValue()) {
 
438
              case WINNT.REG_SZ:
 
439
                values.put(name, convertBufferToString(lpData));
 
440
                break;
 
441
              case WINNT.REG_DWORD:
 
442
                values.put(name, convertBufferToInt(lpData));
 
443
                break;
 
444
              default:
 
445
                break;
 
446
            }
 
447
          }
 
448
        }
 
449
        dwIndex++;
 
450
      } while(result == WINERROR.ERROR_SUCCESS);
 
451
 
 
452
      advapi32.RegCloseKey(handle);
 
453
    }
 
454
    return(values);
 
455
  }
 
456
}