Posted By

narkisr on 06/07/08


Tagged

java windows Regisrty


Versions (?)

Accessing windows registry in Java


 / Published in: Java
 

A paste from a blog (http://lenkite.blogspot.com/2008/05/access-windows-registry-using-java.html) entry which demonstrates how to access windows registry in Java.

  1. import java.lang.reflect.Method;
  2. import java.util.prefs.Preferences;
  3.  
  4. public class JavaRegistryHack {
  5.  
  6. private static final int HKEY_CURRENT_USER = 0x80000001;
  7. private static final int KEY_QUERY_VALUE = 1;
  8. private static final int KEY_SET_VALUE = 2;
  9. private static final int KEY_READ = 0x20019;
  10.  
  11. public static void main(String args[]) {
  12. final Preferences userRoot = Preferences.userRoot();
  13. final Preferences systemRoot = Preferences.systemRoot();
  14. final Class clz = userRoot.getClass();
  15. try {
  16. final Method openKey = clz.getDeclaredMethod("openKey",
  17. byte[].class, int.class, int.class);
  18. openKey.setAccessible(true);
  19.  
  20. final Method closeKey = clz.getDeclaredMethod("closeKey",
  21. int.class);
  22. closeKey.setAccessible(true);
  23.  
  24. final Method winRegQueryValue = clz.getDeclaredMethod(
  25. "WindowsRegQueryValueEx", int.class, byte[].class);
  26. winRegQueryValue.setAccessible(true);
  27. final Method winRegEnumValue = clz.getDeclaredMethod(
  28. "WindowsRegEnumValue1", int.class, int.class, int.class);
  29. winRegEnumValue.setAccessible(true);
  30. final Method winRegQueryInfo = clz.getDeclaredMethod(
  31. "WindowsRegQueryInfoKey1", int.class);
  32. winRegQueryInfo.setAccessible(true);
  33.  
  34.  
  35. byte[] valb = null;
  36. String vals = null;
  37. String key = null;
  38. Integer handle = -1;
  39.  
  40. //Query Internet Settings for Proxy
  41. key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
  42. handle = (Integer) openKey.invoke(userRoot, toCstr(key), KEY_READ, KEY_READ);
  43. valb = (byte[]) winRegQueryValue.invoke(userRoot, handle.intValue(),
  44. toCstr("ProxyServer"));
  45. vals = (valb != null ? new String(valb).trim() : null);
  46. System.out.println("Proxy Server = " + vals);
  47. closeKey.invoke(Preferences.userRoot(), handle);
  48.  
  49. // Query for IE version
  50. key = "SOFTWARE\\Microsoft\\Internet Explorer";
  51. handle = (Integer) openKey.invoke(systemRoot, toCstr(key), KEY_READ, KEY_READ);
  52. valb = (byte[]) winRegQueryValue.invoke(systemRoot, handle, toCstr("Version"));
  53. vals = (valb != null ? new String(valb).trim() : null);
  54. System.out.println("Internet Explorer Version = " + vals);
  55. closeKey.invoke(Preferences.systemRoot(), handle);
  56.  
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61.  
  62.  
  63. private static byte[] toCstr(String str) {
  64. byte[] result = new byte[str.length() + 1];
  65. for (int i = 0; i < str.length(); i++) {
  66. result[i] = (byte) str.charAt(i);
  67. }
  68. result[str.length()] = 0;
  69. return result;
  70. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ShashikanthM on July 29, 2008

Hi , Very nice way to hack. I want to get the default mail client details from HKEYCLASSESROOT\mailto\shell\open\command, to invoke the mail client Kindly let me know if possible by mail , solution for above key

Posted By: ShashikanthM on July 30, 2008

Hi , Very nice way to hack. I want to get the default mail client details from HKEYCLASSESROOT\mailto\shell\open\command, to invoke the mail client Kindly let me know if possible by mail , solution for above key

You need to login to post a comment.