1 20 21 package org.jdesktop.jdic.filetypes.internal; 22 23 import java.io.DataInputStream ; 24 import java.io.IOException ; 25 import java.net.URL ; 26 27 28 31 public class WinRegistryWrapper { 32 static { 33 System.loadLibrary("jdic"); 34 } 35 36 40 public final static int HKEY_CLASSES_ROOT = 0x80000000; 41 public final static int HKEY_CURRENT_USER = 0x80000001; 42 public final static int HKEY_LOCAL_MACHINE = 0x80000002; 43 public final static int HKEY_USERS = 0x80000003; 44 public final static int HKEY_CURRENT_CONFIG = 0x80000005; 45 46 47 public static final int ERROR_SUCCESS = 0; 48 public static final int ERROR_FILE_NOT_FOUND = 2; 49 public static final int ERROR_ACCESS_DENIED = 5; 50 public static final int ERROR_ITEM_EXIST = 0; 51 public static final int ERROR_ITEM_NOTEXIST = 9; 52 53 54 public static final int MAX_KEY_LENGTH = 255; 55 public static final int MAX_VALUE_NAME_LENGTH = 255; 56 57 58 private static final int OPENED_KEY_HANDLE = 0; 59 private static final int ERROR_CODE = 1; 60 private static final int SUBKEYS_NUMBER = 0; 61 private static final int VALUES_NUMBER = 2; 62 63 64 public static final int DELETE = 0x10000; 65 public static final int KEY_QUERY_VALUE = 1; 66 public static final int KEY_SET_VALUE = 2; 67 public static final int KEY_CREATE_SUB_KEY = 4; 68 public static final int KEY_ENUMERATE_SUB_KEYS = 8; 69 public static final int KEY_READ = 0x20019; 70 public static final int KEY_WRITE = 0x20006; 71 public static final int KEY_ALL_ACCESS = 0xf003f; 72 73 79 private static native int[] RegOpenKey(int hKey, byte[] subKey, 80 int securityMask); 81 82 85 private static native int RegCloseKey(int hKey); 86 87 90 private static native int[] RegCreateKeyEx(int hKey, byte[] subKey); 91 92 95 private static native int RegDeleteKey(int hKey, byte[] subKey); 96 97 100 private static native int RegFlushKey(int hKey); 101 102 105 private static native byte[] RegQueryValueEx(int hKey, byte[] valueName); 106 107 110 private static native int RegSetValueEx(int hKey, byte[] valueName, 111 byte[] value); 112 113 116 private static native int RegDeleteValue(int hKey, byte[] valueName); 117 118 121 private static native int[] RegQueryInfoKey(int hKey); 122 123 126 private static native byte[] RegEnumKeyEx(int hKey, int subKeyIndex, 127 int maxKeyLength); 128 129 132 private static native byte[] RegEnumValue(int hKey, int valueIndex, 133 int maxValueNameLength); 134 135 138 private static native byte[] FindMimeFromData(byte[] url, byte[] data); 139 140 143 private static native byte[] ExpandEnvironmentStrings(byte[] envBytes); 144 145 148 private static byte[] stringToByteArray(String str) { 149 if (str == null) { 150 return null; 151 } 152 153 byte[] srcByte = str.getBytes(); 154 int srcLength = srcByte.length; 155 byte[] result = new byte[srcLength + 1]; 156 157 System.arraycopy(srcByte, 0, result, 0, srcLength); 158 result[srcLength] = 0; 159 160 return result; 161 } 162 163 166 private static String byteArrayToString(byte[] array) { 167 if (array != null) { 168 String temString = new String (array); 169 170 if (temString != null) { 171 return temString.substring(0, temString.length() - 1); 172 } 173 } 174 return null; 175 } 176 177 180 private WinRegistryWrapper() {} 181 182 194 public static int WinRegCreateKeyEx(int hKey, String subKey) { 195 byte[] lpSubKey = stringToByteArray(subKey); 196 int[] createResult = RegCreateKeyEx(hKey, lpSubKey); 197 198 if (createResult == null) { 199 return -1; 200 } 201 202 if (createResult[ERROR_CODE] == ERROR_SUCCESS) { 203 RegCloseKey(createResult[OPENED_KEY_HANDLE]); 204 } 205 206 return createResult[ERROR_CODE]; 207 } 208 209 216 public static int WinRegDeleteKey(int hKey, String subKey) { 217 int result; 218 byte[] lpSubKey = stringToByteArray(subKey); 219 220 result = RegDeleteKey(hKey, lpSubKey); 221 222 if (result == ERROR_SUCCESS) { 223 return result; 224 } else { 225 int res = WinRegSubKeyExist(hKey, subKey); 226 227 if (res == ERROR_ITEM_NOTEXIST) { 228 return result; 229 } else { 230 String subSubKey; 231 String [] subSubKeys = WinRegGetSubKeys(hKey, subKey, 232 MAX_KEY_LENGTH); 233 234 for (int keyIndex = 0; keyIndex < subSubKeys.length; keyIndex++) { 235 subSubKey = subKey + "\\" + subSubKeys[keyIndex]; 236 if (subSubKey != null) { 237 WinRegDeleteKey(hKey, subSubKey); 238 } 239 } 240 result = RegDeleteKey(hKey, lpSubKey); 241 return result; 242 } 243 } 244 } 245 246 252 public static int WinRegFlushKey(int hKey, String subKey) { 253 byte[] lpSubKey = stringToByteArray(subKey); 254 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_WRITE); 255 256 if (openResult == null) { 257 return -1; 258 } 259 260 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 261 return openResult[ERROR_CODE]; 262 } else { 263 int flushResult = RegFlushKey(openResult[OPENED_KEY_HANDLE]); 264 265 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 266 return flushResult; 267 } 268 } 269 270 278 public static String WinRegQueryValueEx(int hKey, String subKey, String valueName) { 279 byte[] lpSubKey = stringToByteArray(subKey); 280 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ); 281 282 if (openResult == null) { 283 return null; 284 } 285 286 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 287 return null; 288 } else { 289 byte[] valueBytes; 290 byte[] lpValueName = stringToByteArray(valueName); 291 292 valueBytes = RegQueryValueEx(openResult[OPENED_KEY_HANDLE], 293 lpValueName); 294 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 295 296 if (valueBytes != null) { 297 if ((valueBytes.length == 1) && (valueBytes[0] == 0) && (valueName.equals("")) ){ 298 return null; 299 } else { 300 return byteArrayToString(valueBytes); 301 } 302 } else { 303 return null; 304 } 305 } 306 } 307 308 316 public static int WinRegSetValueEx(int hKey, String subKey, String valueName, String value) { 317 byte[] lpSubKey = stringToByteArray(subKey); 318 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_SET_VALUE); 319 320 if (openResult == null) { 321 return -1; 322 } 323 324 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 325 return openResult[ERROR_CODE]; 326 } else { 327 byte[] lpValueName = stringToByteArray(valueName); 328 byte[] lpValue = stringToByteArray(value); 329 int setResult = RegSetValueEx(openResult[OPENED_KEY_HANDLE], 330 lpValueName, lpValue); 331 332 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 333 334 return setResult; 335 } 336 } 337 338 345 public static int WinRegDeleteValue(int hKey, String subKey, String valueName) { 346 byte[] lpSubKey = stringToByteArray(subKey); 347 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_WRITE); 348 349 if (openResult == null) { 350 return -1; 351 } 352 353 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 354 return openResult[ERROR_CODE]; 355 } else { 356 byte[] lpValueName = stringToByteArray(valueName); 357 int deleteResult = RegDeleteValue(openResult[OPENED_KEY_HANDLE], 358 lpValueName); 359 360 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 361 362 return deleteResult; 363 } 364 } 365 366 372 public static int[] WinRegQueryInfoKey(int hKey, String subKey) { 373 byte[] lpSubKey = stringToByteArray(subKey); 374 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ); 375 376 if (openResult == null) { 377 return null; 378 } 379 380 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 381 return openResult; 382 } else { 383 int[] queryResult = RegQueryInfoKey(openResult[OPENED_KEY_HANDLE]); 384 385 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 386 387 return queryResult; 388 } 389 } 390 391 400 public static String WinRegEnumKeyEx(int hKey, String subKey, int subKeyIndex, int maxKeyLength) { 401 byte[] lpSubKey = stringToByteArray(subKey); 402 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ); 403 404 if (openResult == null) { 405 return null; 406 } 407 408 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 409 return null; 410 } else { 411 byte[] keyBytes = RegEnumKeyEx(openResult[OPENED_KEY_HANDLE], 412 subKeyIndex, maxKeyLength); 413 414 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 415 416 if (keyBytes != null) { 417 return byteArrayToString(keyBytes); 418 } else { 419 return null; 420 } 421 } 422 } 423 424 433 public static String WinRegEnumValue(int hKey, String subKey, int valueIndex, int maxValueNameLength) { 434 byte[] lpSubKey = stringToByteArray(subKey); 435 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ); 436 437 if (openResult == null) { 438 return null; 439 } 440 441 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 442 return null; 443 } else { 444 byte[] valueBytes = RegEnumValue(openResult[OPENED_KEY_HANDLE], 445 valueIndex, maxValueNameLength); 446 447 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 448 449 if (valueBytes != null) { 450 return byteArrayToString(valueBytes); 451 } else { 452 return null; 453 } 454 } 455 } 456 457 464 public static String [] WinRegGetSubKeys(int hKey, String subKey, int maxKeyLength) { 465 byte[] lpSubKey = stringToByteArray(subKey); 466 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ); 467 468 if (openResult == null) { 469 return null; 470 } 471 472 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 473 return null; 474 } else { 475 int[] queryResult = RegQueryInfoKey(openResult[OPENED_KEY_HANDLE]); 476 int subKeysNum = queryResult[SUBKEYS_NUMBER]; 477 478 if (subKeysNum == 0) { 479 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 480 return null; 481 } else { 482 String [] keyStrings = new String [subKeysNum]; 483 byte[] keyBytes; 484 485 for (int subKeyIndex = 0; subKeyIndex < subKeysNum; subKeyIndex++) { 486 keyBytes = RegEnumKeyEx(openResult[OPENED_KEY_HANDLE], 487 subKeyIndex, maxKeyLength); 488 keyStrings[subKeyIndex] = byteArrayToString(keyBytes); 489 } 490 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 491 492 return keyStrings; 493 } 494 } 495 } 496 497 504 public static String [] WinRegGetValues(int hKey, String subKey, int maxValueLength) { 505 byte[] lpSubKey = stringToByteArray(subKey); 506 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ); 507 508 if (openResult == null) { 509 return null; 510 } 511 512 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 513 return null; 514 } else { 515 int[] queryResult = RegQueryInfoKey(openResult[OPENED_KEY_HANDLE]); 516 int valuesNum = queryResult[VALUES_NUMBER]; 517 518 if (valuesNum == 0) { 519 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 520 return null; 521 } else { 522 String [] valueStrings = new String [valuesNum]; 523 byte[] valueBytes; 524 525 for (int valueIndex = 0; valueIndex < valuesNum; valueIndex++) { 526 valueBytes = RegEnumValue(openResult[OPENED_KEY_HANDLE], 527 valueIndex, maxValueLength); 528 valueStrings[valueIndex] = byteArrayToString(valueBytes); 529 } 530 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 531 532 return valueStrings; 533 } 534 } 535 } 536 537 543 public static int WinRegSubKeyExist(int hKey, String subKey) { 544 byte[] lpSubKey = stringToByteArray(subKey); 545 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ); 546 547 if (openResult == null) { 548 return ERROR_ITEM_NOTEXIST; 549 } 550 551 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 552 return ERROR_ITEM_NOTEXIST; 553 } else { 554 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 555 return ERROR_ITEM_EXIST; 556 } 557 } 558 559 566 public static int WinRegValueExist(int hKey, String subKey, String valueName) { 567 if (subKey.trim().equals("")) { 568 return ERROR_ITEM_NOTEXIST; 569 } 570 571 byte[] lpSubKey = stringToByteArray(subKey); 572 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ); 573 574 if (openResult == null) { 575 return ERROR_ITEM_NOTEXIST; 576 } 577 578 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 579 return ERROR_ITEM_NOTEXIST; 580 } else { 581 byte[] lpValueName = stringToByteArray(valueName); 582 byte[] valueBytes = RegQueryValueEx(openResult[OPENED_KEY_HANDLE], 583 lpValueName); 584 585 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 586 587 if (valueBytes == null) { 588 return ERROR_ITEM_NOTEXIST; 589 } else { 590 if ((valueBytes.length == 1) && (valueBytes[0] == 0) && (valueName.equals("")) ){ 591 return ERROR_ITEM_NOTEXIST; 592 } else { 593 return ERROR_ITEM_EXIST; 594 } 595 } 596 } 597 } 598 599 605 public static String WinFindMimeFromData(URL url) { 606 byte[] urlBytes; 607 byte[] result; 608 String urlString = url.toString(); 609 610 urlBytes = stringToByteArray(urlString); 611 612 result = FindMimeFromData(urlBytes, null); 613 if (result != null) { 614 return byteArrayToString(result); 615 } else { 616 byte[] dataBytes = new byte[256]; 617 618 DataInputStream inStream = null; 619 try { 620 inStream = new DataInputStream (url.openStream()); 621 inStream.read(dataBytes, 0, 256); 623 inStream.close(); 624 } catch (IOException e) { 625 return null; 627 } finally { 628 if (inStream != null) { 630 try { 631 inStream.close(); 632 } catch (IOException e) { 633 } 634 } 635 } 636 637 result = FindMimeFromData(null, dataBytes); 638 if (result != null) { 639 return byteArrayToString(result); 640 } else { 641 return null; 642 } 643 } 644 } 645 646 654 public static String WinExpandEnvironmentStrings(String envVariable) { 655 byte[] envVariableBytes = stringToByteArray(envVariable); 656 byte[] resultBytes = ExpandEnvironmentStrings(envVariableBytes); 657 658 return (byteArrayToString(resultBytes)); 659 } 660 } 661 662 663 | Popular Tags |