使用Java读写Windows注册表

使用Java读写Windows注册表,第1张

使用Java读/写Windows注册表

我知道这个问题很旧,但这是Google上第一个”java read/write to registry”的搜索结果。最近,我发现了这段惊人的代码

  • 可以读/写注册表的任何部分。
  • 不使用JNI。
  • 请勿使用任何第三方/外部应用程序进行工作。
  • 不直接使用WINDOWS API

这是纯Java代码。

通过实际访问类中的私有方法,它使用反射来工作

java.util.prefs.Preferences
。该类的内部结构很复杂,但是该类本身非常易于使用。

例如,以下代码从注册表中获取确切的Windows分发:

String value = WinRegistry.readString (    WinRegistry.HKEY_LOCAL_MACHINE,       //HKEY   "SOFTWARE\Microsoft\Windows NT\CurrentVersion",//Key   "ProductName");  //ValueName    System.out.println("Windows Distribution = " + value);  

这是原始班级。只需复制粘贴即可,它应该可以正常工作:

import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;import java.util.ArrayList;import java.util.List;import java.util.prefs.Preferences;public class WinRegistry {  public static final int HKEY_CURRENT_USER = 0x80000001;  public static final int HKEY_LOCAL_MACHINE = 0x80000002;  public static final int REG_SUCCESS = 0;  public static final int REG_NOTFOUND = 2;  public static final int REG_ACCESSDENIED = 5;  private static final int KEY_ALL_ACCESS = 0xf003f;  private static final int KEY_READ = 0x20019;  private static final Preferences userRoot = Preferences.userRoot();  private static final Preferences systemRoot = Preferences.systemRoot();  private static final Class<? extends Preferences> userClass = userRoot.getClass();  private static final Method regOpenKey;  private static final Method regCloseKey;  private static final Method regQueryValueEx;  private static final Method regEnumValue;  private static final Method regQueryInfoKey;  private static final Method regEnumKeyEx;  private static final Method regCreateKeyEx;  private static final Method regSetValueEx;  private static final Method regDeleteKey;  private static final Method regDeletevalue;  static {    try {      regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey",          new Class[] { int.class, byte[].class, int.class });      regOpenKey.setAccessible(true);      regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey",          new Class[] { int.class });      regCloseKey.setAccessible(true);      regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx",          new Class[] { int.class, byte[].class });      regQueryValueEx.setAccessible(true);      regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue",          new Class[] { int.class, int.class, int.class });      regEnumValue.setAccessible(true);      regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1",          new Class[] { int.class });      regQueryInfoKey.setAccessible(true);      regEnumKeyEx = userClass.getDeclaredMethod( "WindowsRegEnumKeyEx", new Class[] { int.class, int.class,     int.class });        regEnumKeyEx.setAccessible(true);      regCreateKeyEx = userClass.getDeclaredMethod( "WindowsRegCreateKeyEx", new Class[] { int.class,     byte[].class });        regCreateKeyEx.setAccessible(true);        regSetValueEx = userClass.getDeclaredMethod( "WindowsRegSetValueEx", new Class[] { int.class,     byte[].class, byte[].class });        regSetValueEx.setAccessible(true);       regDeletevalue = userClass.getDeclaredMethod( "WindowsRegDeletevalue", new Class[] { int.class,     byte[].class });        regDeletevalue.setAccessible(true);       regDeleteKey = userClass.getDeclaredMethod( "WindowsRegDeleteKey", new Class[] { int.class,     byte[].class });        regDeleteKey.setAccessible(true);     }    catch (Exception e) {      throw new RuntimeException(e);    }  }  private WinRegistry() {  }    public static String readString(int hkey, String key, String valueName)     throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    if (hkey == HKEY_LOCAL_MACHINE) {      return readString(systemRoot, hkey, key, valueName);    }    else if (hkey == HKEY_CURRENT_USER) {      return readString(userRoot, hkey, key, valueName);    }    else {      throw new IllegalArgumentException("hkey=" + hkey);    }  }    public static Map<String, String> readStringValues(int hkey, String key)     throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    if (hkey == HKEY_LOCAL_MACHINE) {      return readStringValues(systemRoot, hkey, key);    }    else if (hkey == HKEY_CURRENT_USER) {      return readStringValues(userRoot, hkey, key);    }    else {      throw new IllegalArgumentException("hkey=" + hkey);    }  }    public static List<String> readStringSubKeys(int hkey, String key)     throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    if (hkey == HKEY_LOCAL_MACHINE) {      return readStringSubKeys(systemRoot, hkey, key);    }    else if (hkey == HKEY_CURRENT_USER) {      return readStringSubKeys(userRoot, hkey, key);    }    else {      throw new IllegalArgumentException("hkey=" + hkey);    }  }    public static void createKey(int hkey, String key)     throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    int [] ret;    if (hkey == HKEY_LOCAL_MACHINE) {      ret = createKey(systemRoot, hkey, key);      regCloseKey.invoke(systemRoot, new Object[] { new Integer(ret[0]) });    }    else if (hkey == HKEY_CURRENT_USER) {      ret = createKey(userRoot, hkey, key);      regCloseKey.invoke(userRoot, new Object[] { new Integer(ret[0]) });    }    else {      throw new IllegalArgumentException("hkey=" + hkey);    }    if (ret[1] != REG_SUCCESS) {      throw new IllegalArgumentException("rc=" + ret[1] + "  key=" + key);    }  }    public static void writeStringValue    (int hkey, String key, String valueName, String value)     throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    if (hkey == HKEY_LOCAL_MACHINE) {      writeStringValue(systemRoot, hkey, key, valueName, value);    }    else if (hkey == HKEY_CURRENT_USER) {      writeStringValue(userRoot, hkey, key, valueName, value);    }    else {      throw new IllegalArgumentException("hkey=" + hkey);    }  }    public static void deleteKey(int hkey, String key)     throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    int rc = -1;    if (hkey == HKEY_LOCAL_MACHINE) {      rc = deleteKey(systemRoot, hkey, key);    }    else if (hkey == HKEY_CURRENT_USER) {      rc = deleteKey(userRoot, hkey, key);    }    if (rc != REG_SUCCESS) {      throw new IllegalArgumentException("rc=" + rc + "  key=" + key);    }  }    public static void deletevalue(int hkey, String key, String value)     throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    int rc = -1;    if (hkey == HKEY_LOCAL_MACHINE) {      rc = deletevalue(systemRoot, hkey, key, value);    }    else if (hkey == HKEY_CURRENT_USER) {      rc = deletevalue(userRoot, hkey, key, value);    }    if (rc != REG_SUCCESS) {      throw new IllegalArgumentException("rc=" + rc + "  key=" + key + "  value=" + value);    }  }  // =====================  private static int deletevalue    (Preferences root, int hkey, String key, String value)    throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {        new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });    if (handles[1] != REG_SUCCESS) {      return handles[1];  // can be REG_NOTFOUND, REG_ACCESSDENIED    }    int rc =((Integer) regDeletevalue.invoke(root,          new Object[] {new Integer(handles[0]), toCstr(value)})).intValue();    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });    return rc;  }  private static int deleteKey(Preferences root, int hkey, String key)     throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    int rc =((Integer) regDeleteKey.invoke(root,          new Object[] { new Integer(hkey), toCstr(key) })).intValue();    return rc;  // can REG_NOTFOUND, REG_ACCESSDENIED, REG_SUCCESS  }  private static String readString(Preferences root, int hkey, String key, String value)    throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {        new Integer(hkey), toCstr(key), new Integer(KEY_READ) });    if (handles[1] != REG_SUCCESS) {      return null;     }    byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[] {        new Integer(handles[0]), toCstr(value) });    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });    return (valb != null ? new String(valb).trim() : null);  }  private static Map<String,String> readStringValues    (Preferences root, int hkey, String key)    throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    HashMap<String, String> results = new HashMap<String,String>();    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {        new Integer(hkey), toCstr(key), new Integer(KEY_READ) });    if (handles[1] != REG_SUCCESS) {      return null;    }    int[] info = (int[]) regQueryInfoKey.invoke(root,        new Object[] { new Integer(handles[0]) });    int count = info[0]; // count      int maxlen = info[3]; // value length max    for(int index=0; index<count; index++)  {      byte[] name = (byte[]) regEnumValue.invoke(root, new Object[] {          new Integer (handles[0]), new Integer(index), new Integer(maxlen + 1)});      String value = readString(hkey, key, new String(name));      results.put(new String(name).trim(), value);    }    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });    return results;  }  private static List<String> readStringSubKeys    (Preferences root, int hkey, String key)    throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    List<String> results = new ArrayList<String>();    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {        new Integer(hkey), toCstr(key), new Integer(KEY_READ)         });    if (handles[1] != REG_SUCCESS) {      return null;    }    int[] info = (int[]) regQueryInfoKey.invoke(root,        new Object[] { new Integer(handles[0]) });    int count  = info[0]; // Fix: info[2] was being used here with wrong results. Suggested by davenpcj, confirmed by Petrucio    int maxlen = info[3]; // value length max    for(int index=0; index<count; index++)  {      byte[] name = (byte[]) regEnumKeyEx.invoke(root, new Object[] {          new Integer (handles[0]), new Integer(index), new Integer(maxlen + 1)          });      results.add(new String(name).trim());    }    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });    return results;  }  private static int [] createKey(Preferences root, int hkey, String key)    throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    return  (int[]) regCreateKeyEx.invoke(root,        new Object[] { new Integer(hkey), toCstr(key) });  }  private static void writeStringValue     (Preferences root, int hkey, String key, String valueName, String value)     throws IllegalArgumentException, IllegalAccessException,    InvocationTargetException   {    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {        new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS) });    regSetValueEx.invoke(root,          new Object[] {new Integer(handles[0]), toCstr(valueName), toCstr(value)});     regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });  }  // utility  private static byte[] toCstr(String str) {    byte[] result = new byte[str.length() + 1];    for (int i = 0; i < str.length(); i++) {      result[i] = (byte) str.charAt(i);    }    result[str.length()] = 0;    return result;  }}

原始作者:Apache。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4917297.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存