纯java访问注册表
来源:互联网 发布:linux中vi修改文件 编辑:程序博客网 时间:2024/05/17 04:56
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;
}
}
- 纯java访问注册表
- 纯java访问注册表
- java访问WIN32注册表
- JAVA访问注册表
- java访问注册表剖析
- Java 访问注册表
- JAVA访问注册表(转贴)
- 用java访问windows注册表
- 使用 Java 访问 Windows 注册表
- java访问window的注册表
- JAVA访问注册表(转贴)
- 访问注册表
- 访问注册表
- 纯java项目使用hibernateSessionFactory访问数据库
- 纯html跨域访问java接口
- 【Java学习】java程序访问注册表(win10)
- 通过本地协议纯Java驱动程序访问SQL Server数据库
- 纯JAVA HTTPURL 访问制定url地址返回页面数据
- 预防和治疗黑眼圈方法
- 各种排序算法java实现
- 超好用的XML工具
- SQL指南 - UNION and UNION ALL
- uml基础知识结构图.
- 纯java访问注册表
- 冻疮治疗方法收集
- 做项目
- 说说大型高并发高负载网站的系统架构
- 面向对象编程,我的思想
- IT管理“十大失误”及其对策
- 什么是MVC
- 在Google Talk上与QQ、MSN好友聊天(转)
- JAVA访问注册表