read/write to Windows Registry using Java -
how possible read/write windows registry using java?
i know question old, first search result on google "java read/write registry". found amazing piece of code which:
- can read/write part of registry.
- does not use jni.
- does not use 3rd party/external applications work.
- does not use windows api (directly)
this pure, java code.
it uses reflection work, accessing private methods in java.util.prefs.preferences class. internals of class complicated, class easy use.
for example, following code obtains exact windows distribution from registry:
string value = winregistry.readstring ( winregistry.hkey_local_machine, //hkey "software\\microsoft\\windows nt\\currentversion", //key "productname"); //valuename system.out.println("windows distribution = " + value);
here original class. copy paste , should work:
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() { } /** * read value key , value name * @param hkey hkey_current_user/hkey_local_machine * @param key * @param valuename * @return value * @throws illegalargumentexception * @throws illegalaccessexception * @throws invocationtargetexception */ 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); } } /** * read value(s) , value name(s) form given key * @param hkey hkey_current_user/hkey_local_machine * @param key * @return value name(s) plus value(s) * @throws illegalargumentexception * @throws illegalaccessexception * @throws invocationtargetexception */ 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); } } /** * read value name(s) given key * @param hkey hkey_current_user/hkey_local_machine * @param key * @return value name(s) * @throws illegalargumentexception * @throws illegalaccessexception * @throws invocationtargetexception */ 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); } } /** * create key * @param hkey hkey_current_user/hkey_local_machine * @param key * @throws illegalargumentexception * @throws illegalaccessexception * @throws invocationtargetexception */ 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); } } /** * write value in given key/value name * @param hkey * @param key * @param valuename * @param value * @throws illegalargumentexception * @throws illegalaccessexception * @throws invocationtargetexception */ 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); } } /** * delete given key * @param hkey * @param key * @throws illegalargumentexception * @throws illegalaccessexception * @throws invocationtargetexception */ 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); } } /** * delete value given key/value name * @param hkey * @param key * @param value * @throws illegalargumentexception * @throws illegalaccessexception * @throws invocationtargetexception */ 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 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] being used here wrong results. suggested davenpcj, confirmed 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]; (int = 0; < str.length(); i++) { result[i] = (byte) str.charat(i); } result[str.length()] = 0; return result; } }
i unable find , give credit original author of code. if find details, please add comment , add here.
Comments
Post a Comment