当前位置: 技术问答>java相关
在Java中如何读取注册表信息?
来源: 互联网 发布时间:2015-11-08
本文导语: 在Java中如何读取注册表信息? | //usage: C:registrytest> c:jdk1.4binjava Main "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main" "start page" http://www.google.com/ //java Main "HKEY_CURRENT_USERSoftwareMicrosoftInternet...
在Java中如何读取注册表信息?
|
//usage: C:registrytest> c:jdk1.4binjava Main "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main" "start page" http://www.google.com/
//java Main "HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMain" "start page" http://www.google.com/
//http://www.esus.com/javaindex/j2se/jdk1.2/javautil/preferences/preferences.html
import java.lang.reflect.*;
import java.util.prefs.*;
import java.util.*;
import java.io.*;
public class Main
{
/* Windows security masks */
private final static int KEY_QUERY_VALUE = 1;
/* Constants used to interpret returns of native functions */
private final static int NATIVE_HANDLE = 0;
private final static int ERROR_CODE = 1;
/* Windows error codes. */
private final static int ERROR_SUCCESS = 0;
public static void main(String[] args)
{
System.out.println("java.version:"+System.getProperty("java.version"));
args = new String[2];
args[0]= "HKEY_LOCAL_MACHINE\\SOFTWARE\Rogan";
args[1]="root";
if (args.length != 2) {
System.out.println("Usage: java Main path key");
System.out.print(" eg.: java Main "HKEY_CURRENT_USER\");
System.out.print("\Software\\Microsoft\\Internet Explorer\\Main"");
System.out.println(" "start page"");
System.exit(1);
}
try {
int hkey = getHKEY(args[0]);
System.out.println(Integer.toHexString(hkey));
byte[] WINDOWS_ROOT_PATH = stripHKEY(args[0]);
String key = args[1];
System.out.println(getValue(hkey, WINDOWS_ROOT_PATH, key));
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static int getHKEY(String path) throws Exception {
if (path.startsWith("HKEY_CURRENT_USER")) {
return 0x80000001;
}
else if (path.startsWith("HKEY_LOCAL_MACHINE")) {
return 0x80000002;
}
else {
throw new Exception("Path should start with HKEY_CURRENT_USER " +
"or HKEY_LOCAL_MACHINE");
}
}
public static byte[] stripHKEY(String path)
{
int beginIndex = path.indexOf("\\");
String subtmp = path.substring(beginIndex+2);
System.out.println(subtmp);
return stringToByteArray(subtmp);
}
public static String getValue(int hkey, byte[] WINDOWS_ROOT_PATH, String key)
throws Exception
{
Class theClass = Class.forName("java.util.prefs.WindowsPreferences");
int[] result = openKey1(hkey, windowsAbsolutePath(WINDOWS_ROOT_PATH),
KEY_QUERY_VALUE);
if (result[ERROR_CODE] != ERROR_SUCCESS)
{
throw new Exception("Path not found!");
}
int nativeHandle = result[NATIVE_HANDLE];
Method m = theClass.getDeclaredMethod("WindowsRegQueryValueEx",
new Class[]{int.class, byte[].class});
m.setAccessible(true);
byte[] windowsName = toWindowsName(key);
Object value = m.invoke(null, new Object[]{new Integer(nativeHandle), windowsName});
WindowsRegCloseKey(nativeHandle);
if (value == null) {
throw new Exception("Path found. Key not found.");
}
byte[] origBuffer = (byte[]) value;
byte[] destBuffer = new byte[origBuffer.length - 1];
System.arraycopy(origBuffer, 0, destBuffer, 0, origBuffer.length - 1);
return new String(destBuffer);
}
public static int WindowsRegCloseKey(int nativeHandle)
throws Exception {
Class theClass = Class.forName("java.util.prefs.WindowsPreferences");
Method m = theClass.getDeclaredMethod("WindowsRegCloseKey", new Class[]{int.class});
m.setAccessible(true);
Object ret = m.invoke(null, new Object[]{new Integer(nativeHandle)});
return ((Integer) ret).intValue();
}
public static int openKey(byte[] windowsAbsolutePath, int securityMask)
throws Exception {
Class theClass = Class.forName("java.util.prefs.WindowsPreferences");
Method m = theClass.getDeclaredMethod("openKey", new Class[]{byte[].class, int.class});
m.setAccessible(true);
Object ret = m.invoke(null, new Object[]{windowsAbsolutePath, new Integer(securityMask)});
return ((Integer) ret).intValue();
}
public static int[] openKey1(int hkey, byte[] windowsAbsolutePath, int securityMask)
throws Exception {
Class theClass = Class.forName("java.util.prefs.WindowsPreferences");
Method m = theClass.getDeclaredMethod("WindowsRegOpenKey", new Class[]{int.class,
byte[].class,
int.class});
m.setAccessible(true);
Object ret = m.invoke(null, new Object[]{new Integer(hkey),
windowsAbsolutePath,
new Integer(securityMask)});
return (int[]) ret;
}
private static byte[] stringToByteArray(String str) {
byte[] result = new byte[str.length() + 1];
for (int i = 0; i
//java Main "HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMain" "start page" http://www.google.com/
//http://www.esus.com/javaindex/j2se/jdk1.2/javautil/preferences/preferences.html
import java.lang.reflect.*;
import java.util.prefs.*;
import java.util.*;
import java.io.*;
public class Main
{
/* Windows security masks */
private final static int KEY_QUERY_VALUE = 1;
/* Constants used to interpret returns of native functions */
private final static int NATIVE_HANDLE = 0;
private final static int ERROR_CODE = 1;
/* Windows error codes. */
private final static int ERROR_SUCCESS = 0;
public static void main(String[] args)
{
System.out.println("java.version:"+System.getProperty("java.version"));
args = new String[2];
args[0]= "HKEY_LOCAL_MACHINE\\SOFTWARE\Rogan";
args[1]="root";
if (args.length != 2) {
System.out.println("Usage: java Main path key");
System.out.print(" eg.: java Main "HKEY_CURRENT_USER\");
System.out.print("\Software\\Microsoft\\Internet Explorer\\Main"");
System.out.println(" "start page"");
System.exit(1);
}
try {
int hkey = getHKEY(args[0]);
System.out.println(Integer.toHexString(hkey));
byte[] WINDOWS_ROOT_PATH = stripHKEY(args[0]);
String key = args[1];
System.out.println(getValue(hkey, WINDOWS_ROOT_PATH, key));
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static int getHKEY(String path) throws Exception {
if (path.startsWith("HKEY_CURRENT_USER")) {
return 0x80000001;
}
else if (path.startsWith("HKEY_LOCAL_MACHINE")) {
return 0x80000002;
}
else {
throw new Exception("Path should start with HKEY_CURRENT_USER " +
"or HKEY_LOCAL_MACHINE");
}
}
public static byte[] stripHKEY(String path)
{
int beginIndex = path.indexOf("\\");
String subtmp = path.substring(beginIndex+2);
System.out.println(subtmp);
return stringToByteArray(subtmp);
}
public static String getValue(int hkey, byte[] WINDOWS_ROOT_PATH, String key)
throws Exception
{
Class theClass = Class.forName("java.util.prefs.WindowsPreferences");
int[] result = openKey1(hkey, windowsAbsolutePath(WINDOWS_ROOT_PATH),
KEY_QUERY_VALUE);
if (result[ERROR_CODE] != ERROR_SUCCESS)
{
throw new Exception("Path not found!");
}
int nativeHandle = result[NATIVE_HANDLE];
Method m = theClass.getDeclaredMethod("WindowsRegQueryValueEx",
new Class[]{int.class, byte[].class});
m.setAccessible(true);
byte[] windowsName = toWindowsName(key);
Object value = m.invoke(null, new Object[]{new Integer(nativeHandle), windowsName});
WindowsRegCloseKey(nativeHandle);
if (value == null) {
throw new Exception("Path found. Key not found.");
}
byte[] origBuffer = (byte[]) value;
byte[] destBuffer = new byte[origBuffer.length - 1];
System.arraycopy(origBuffer, 0, destBuffer, 0, origBuffer.length - 1);
return new String(destBuffer);
}
public static int WindowsRegCloseKey(int nativeHandle)
throws Exception {
Class theClass = Class.forName("java.util.prefs.WindowsPreferences");
Method m = theClass.getDeclaredMethod("WindowsRegCloseKey", new Class[]{int.class});
m.setAccessible(true);
Object ret = m.invoke(null, new Object[]{new Integer(nativeHandle)});
return ((Integer) ret).intValue();
}
public static int openKey(byte[] windowsAbsolutePath, int securityMask)
throws Exception {
Class theClass = Class.forName("java.util.prefs.WindowsPreferences");
Method m = theClass.getDeclaredMethod("openKey", new Class[]{byte[].class, int.class});
m.setAccessible(true);
Object ret = m.invoke(null, new Object[]{windowsAbsolutePath, new Integer(securityMask)});
return ((Integer) ret).intValue();
}
public static int[] openKey1(int hkey, byte[] windowsAbsolutePath, int securityMask)
throws Exception {
Class theClass = Class.forName("java.util.prefs.WindowsPreferences");
Method m = theClass.getDeclaredMethod("WindowsRegOpenKey", new Class[]{int.class,
byte[].class,
int.class});
m.setAccessible(true);
Object ret = m.invoke(null, new Object[]{new Integer(hkey),
windowsAbsolutePath,
new Integer(securityMask)});
return (int[]) ret;
}
private static byte[] stringToByteArray(String str) {
byte[] result = new byte[str.length() + 1];
for (int i = 0; i