纯java访问注册表
One can use the private code of Sun's Preferences API to access values of type REG_SZ in the Windows Registry. Ofcourse - I completely agree that this is a VERY HORRIBLE HACK so don't lecture me!
The java.util.prefs.WindowsPreferences is the concrete implementation of AbstractPreferences in the Windows platform. This class provides methods like WindowsRegQueryValueEx, etc. Using Reflection, one can use the methods in this class to query string values under HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER. For some reason, the java wrapper for WindowsRegQueryValueEx only seems to work for string values. I get a null for values of type REG_DWORD, which I guess makes this approach pretty useless except in times of desperation.
One can use the private code of Sun's Preferences API to access values of type REG_SZ in the Windows Registry. Ofcourse - I completely agree that this is a VERY HORRIBLE HACK so don't lecture me!
The java.util.prefs.WindowsPreferences is the concrete implementation of AbstractPreferences in the Windows platform. This class provides methods like WindowsRegQueryValueEx, etc. Using Reflection, one can use the methods in this class to query string values under HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER. For some reason, the java wrapper for WindowsRegQueryValueEx only seems to work for string values. I get a null for values of type REG_DWORD, which I guess makes this approach pretty useless except in times of desperation.
Given below is a code-snippet that demonstrates how to get the ProxyServer setting on Windows (this is what IE uses/sets):
import java.lang.reflect.Method;
import java.util.prefs.Preferences;
public class RegHack {
private static final int HKEY_CURRENT_USER = 0x80000001;
private static final int KEY_QUERY_VALUE = 1;
private static final int KEY_SET_VALUE = 2;
private static final int KEY_READ = 0x20019;
public static void main(String args[]) {
final Preferences userRoot = Preferences.userRoot();
final Class clz = userRoot.getClass();
try {
final Method mOpenKey = clz.getDeclaredMethod("openKey",
byte[].class, int.class, int.class);
mOpenKey.setAccessible(true);
final Method mCloseKey = clz.getDeclaredMethod("closeKey",
int.class);
mCloseKey.setAccessible(true);
final Method mWinRegQueryValue = clz.getDeclaredMethod(
"WindowsRegQueryValueEx", int.class, byte[].class);
mWinRegQueryValue.setAccessible(true);
final Method mWinRegEnumValue = clz.getDeclaredMethod(
"WindowsRegEnumValue1", int.class, int.class, int.class);
mWinRegEnumValue.setAccessible(true);
final Method mWinRegQueryInfo = clz.getDeclaredMethod(
"WindowsRegQueryInfoKey1", int.class);
mWinRegQueryInfo.setAccessible(true);
final String subKey = "Software//Microsoft//Windows//CurrentVersion//Internet Settings";
Integer hSettings = (Integer) mOpenKey.invoke(userRoot,
toByteArray(subKey), KEY_READ, KEY_READ);
byte[] b = (byte[]) mWinRegQueryValue.invoke(userRoot, hSettings
.intValue(), toByteArray("ProxyServer"));
String s = b != null ? new String(b).trim() : null;
System.out.println(s);
mCloseKey.invoke(Preferences.userRoot(), hSettings);
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] toByteArray(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;
}
}