1 36 package jnlp.sample.JreInstaller; 37 38 39 43 public class WinRegistry 44 { 45 public static final int HKEY_CLASSES_ROOT = 0x80000000; 46 public static final int HKEY_CURRENT_USER = 0x80000001; 47 public static final int HKEY_LOCAL_MACHINE = 0x80000002; 48 public static final int HKEY_USERS = 0x80000003; 49 public static final int HKEY_PERFORMANCE_DATA = 0x80000004; 50 public static final int HKEY_CURRENT_CONFIG = 0x80000005; 51 public static final int HKEY_DYN_DATA = 0x80000006; 52 53 static final int KEY_QUERY_VALUE = 0x1; 54 static final int KEY_SET_VALUE = 0x2; 55 static final int KEY_CREATE_SUB_KEY = 0x4; 56 static final int KEY_ENUMERATE_SUB_KEYS = 0x8; 57 static final int KEY_NOTIFY = 0x10; 58 static final int KEY_CREATE_LINK = 0x20; 59 static final int KEY_READ = 0x20019; 60 static final int KEY_WRITE = 0x20006; 61 62 static final int KEY_EXECUTE = 0x20019; 63 static final int KEY_ALL_ACCESS = 0xf003f; 64 static final int REG_OPTION_RESERVED = 0x0; 65 static final int REG_OPTION_NON_VOLATILE = 0x0; 66 static final int REG_OPTION_VOLATILE = 0x1; 67 static final int REG_OPTION_CREATE_LINK = 0x2; 68 static final int REG_OPTION_BACKUP_RESTORE = 0x4; 69 static final int REG_OPTION_OPEN_LINK = 0x8; 70 static final int REG_LEGAL_OPTION = 0xf; 71 static final int REG_CREATED_NEW_KEY = 0x1; 72 static final int REG_OPENED_EXISTING_KEY = 0x2; 73 static final int REG_WHOLE_HIVE_VOLATILE = 0x1; 74 static final int REG_REFRESH_HIVE = 0x2; 75 static final int REG_NO_LAZY_FLUSH = 0x4; 76 static final int REG_NOTIFY_CHANGE_NAME = 0x1; 77 static final int REG_NOTIFY_CHANGE_ATTRIBUTES = 0x2; 78 static final int REG_NOTIFY_CHANGE_LAST_SET = 0x4; 79 static final int REG_NOTIFY_CHANGE_SECURITY = 0x8; 80 static final int REG_LEGAL_CHANGE_FILTER = 0xf; 81 static final int REG_NONE = 0x0; 82 static final int REG_SZ = 0x1; 83 static final int REG_EXPAND_SZ = 0x2; 84 static final int REG_BINARY = 0x3; 85 static final int REG_DWORD = 0x4; 86 static final int REG_DWORD_LITTLE_ENDIAN = 0x4; 87 static final int REG_DWORD_BIG_ENDIAN = 0x5; 88 static final int REG_LINK = 0x6; 89 static final int REG_MULTI_SZ = 0x7; 90 static final int REG_RESOURCE_LIST = 0x8; 91 static final int REG_FULL_RESOURCE_DESCRIPTOR = 0x9; 92 static final int REG_RESOURCE_REQUIREMENTS_LIST = 0xa; 93 94 102 static class KeyValue { 103 private final int type; 104 private final byte[] data; 105 private Object value = null; 106 107 KeyValue(int type, byte[] data) { 108 this.type = type; 109 this.data = data; 110 } 111 112 public int getType() { 113 return type; 114 } 115 116 public byte[] getData() { 117 return data; 118 } 119 120 public Object getValue() { 121 switch (type) { 122 case REG_SZ: 123 if (data.length - 1 < 0) return null; 126 return new String (data, 0, data.length - 1); 127 128 case REG_DWORD: 129 { 130 int n = 0; 131 for(int i = 0; (i < 4) && (i < data.length); i++) { 132 n += data[i] << (i * 8); 133 } 134 return new Integer (n); 135 } 136 137 default: 138 return getData(); 139 } 140 } 141 } 142 143 static native void initIDs(); 144 static native int sysOpenKey(int hKey, String subKey, int sam); 145 static native int sysCloseKey(int hKey); 146 static native KeyValue sysQueryKey(int hKey, String name); 147 static native int sysCreateKey(int hKey, String name, int sam); 148 static native boolean sysSetStringValue(int hKey, String name, 149 String value); 150 static native boolean sysDeleteKey(int hKey, String subKey); 151 static native boolean sysReboot(); 152 153 156 public static native String getWindowsDirectory(); 157 158 static { 159 initIDs(); 161 } 162 163 164 public static Object get(int hKey, String path, String name) { 165 int key = sysOpenKey(hKey, path, KEY_READ); 166 if (key != 0) { 167 KeyValue kv = sysQueryKey(key, name); 168 sysCloseKey(key); 169 if (kv != null) { 170 return kv.getValue(); 171 } 172 } 173 return null; 174 } 175 176 public static String getString(int hKey, String path, String name) { 177 Object rv = get(hKey, path, name); 178 return (rv instanceof String ) ? (String )rv : null; 179 } 180 181 public static Integer getInteger(int hKey, String path, String name) { 182 Object rv = get(hKey, path, name); 183 if (rv instanceof Integer ) { 184 return (Integer )rv; 185 } else if (rv instanceof byte[]){ 186 byte[] b = (byte[])rv; 187 if (b.length == 4 && b[1] == 0 && b[2] == 0 && b[3] == 0) { 188 return new Integer (b[0]); 189 } else { 190 return null; 191 } 192 } 193 return null; 194 } 195 196 public static boolean setStringValue(int hKey, String path, String name, 197 String value) { 198 boolean retValue = false; 199 int key = sysCreateKey(hKey, path, KEY_WRITE); 200 if (key != 0) { 201 retValue = sysSetStringValue(key, name, value); 202 sysCloseKey(key); 203 } 204 return retValue; 205 } 206 207 211 public static boolean doesSubKeyExist(int hKey, String path) { 212 int key = sysOpenKey(hKey, path, KEY_READ); 213 if (key != 0) { 214 sysCloseKey(key); 215 return true; 216 } 217 return false; 218 } 219 220 public static boolean deleteKey(int hKey, String path) { 221 return sysDeleteKey(hKey, path); 222 } 223 224 public static boolean doReboot() { 225 return sysReboot(); 226 } 227 } 228 | Popular Tags |