纯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 flect.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 = Class();
try {
final Method mOpenKey = DeclaredMethod("openKey",
byte[].class, int.class, int.class);
mOpenKey.setAccessible(true);
final Method mCloseKey = DeclaredMethod("closeKey",
int.class);
mCloseKey.setAccessible(true);
final Method mWinRegQueryValue = DeclaredMethod(
"WindowsRegQueryValueEx", int.class, byte[].class);
mWinRegQueryValue.setAccessible(true);
final Method mWinRegEnumValue = DeclaredMethod(
"WindowsRegEnumValue1", int.class, int.class, int.class);
mWinRegEnumValue.setAccessible(true);
final Method mWinRegQueryInfo = DeclaredMethod(
"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;
}
}
本文发布于:2024-02-02 01:39:50,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170681336940589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |