java读取客户端注册表_JAVA获取客户端注册表键值

该博客介绍了如何使用JAVA进行客户端注册表的读取和操作,包括打开注册表键、关闭键、查询值和枚举值的方法。通过WinRegisterUtil类提供了WindowsRegOpenKeyEx、WindowsRegCloseKey、WindowsRegQueryValueEx和WindowsRegEnumValue等关键函数,实现了对注册表的读写访问。
摘要由CSDN通过智能技术生成

展开全部

public final class WinRegisterUtil {

// root key value defined by windows

public static final int HKEY_CLASSES_ROOT = 0x80000000;

public static final int HKEY_CURRENT_USER = 0x80000001;

public static final int HKEY_LOCAL_MACHINE = 0x80000002;

public static final int HKEY_USERS = 0x80000003;

public static final int HKEY_PERFORMANCE_DATA = 0x80000004;

public static final int HKEY_CURRENT_CONFIG = 0x80000005;

//Constants used to interpret returns of native functions

public static final int NATIVE_HANDLE = 0;

public static final int ERROR_CODE = 1;

//Windows security masks

public static final int DELETE = 0x10000;

public static final int KEY_QUERY_VALUE = 1;

public static final int KEY_SET_VALUE = 2;

public static final int KEY_CREATE_SUB_KEY = 4;

public static final int KEY_ENUMERATE_SUB_KEYS = 8;

public static final int KEY_READ = 0x20019;

public static final int KEY_WRITE = 0x20006;

public static final int KEY_ALL_ACCESS = 0xf003f;

//error codes

public static final int ERROR_SUCCESS = 0;

public static final int ERROR_FAILED = -1;

private static final Integer MAX_KEY_LENGHT = new Integer(100);

private static final Class theClass = getUtilClass();

private static Class getUtilClass() {

try {

return Class.forName("java.util.prefs.WindowsPreferences");

}

catch (Exception e) {

return null;

}

}

// hKey: 跟键,列在类头,e5a48de588b63231313335323631343130323136353331333332643834HKEY_CURRENT_USER

// subKey: 相对hKey的路径,例如:"Software\\ODBC"

// securityMask:读写权限,列在类头,KEY_ALL_ACCESS

public static int[] windowsRegOpenKeyEx(int hKey, String subKey,

int securityMask) {

try {

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),

stringToByteArray(subKey), new Integer(securityMask)});

return (int[]) ret;

}

catch (Exception e) {

System.out.println("Exception windowsRegOpenKeyEx!");

return new int[] {

0, ERROR_FAILED};

}

}

// 关闭注册表

public static void windowsRegCloseKey(int hKey) {

try {

Method m = theClass.getDeclaredMethod("WindowsRegCloseKey", new Class[] {int.class});

m.setAccessible(true);

m.invoke(null, new Object[] {new Integer(hKey)});

}

catch (Exception e) {

System.out.println("Exception windowsRegCloseKey!");

}

}

// hKey父键的句柄

// key子键的字符串,是中文时取不到

public static String windowsRegQueryValueEx(int hKey, String key) {

try {

Method m = theClass.getDeclaredMethod("WindowsRegQueryValueEx",

new Class[] {int.class, byte[].class});

m.setAccessible(true);

Object value = m.invoke(null, new Object[] {new Integer(hKey),

stringToByteArray(key)});

if (value == null) {

return null;

}

return winByteArrayToString( (byte[]) value);

}

catch (Exception e) {

System.out.println("Exception windowsRegQueryValueEx!");

return null;

}

}

// 枚举hKey下的值,valueIndex从0开始计数

public static String windowsRegEnumValue(int hKey, int valueIndex) {

try {

Method m = theClass.getDeclaredMethod("WindowsRegEnumValue",

new Class[] {int.class, int.class, int.class});

m.setAccessible(true);

Object value = m.invoke(null, new Object[] {new Integer(hKey),

new Integer(valueIndex), MAX_KEY_LENGHT});

return winByteArrayToString( (byte[]) value);

}

catch (Exception e) {

System.out.println("Exception windowsRegEnumValue!");

return null;

}

}

private static byte[] stringToByteArray(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;

}

private final static String winByteArrayToString(byte[] array) {

if (array == null) {

return null;

}

int pos = array.length - 1;

while (pos >= 0) {

if (array[pos] != '\0') {

break;

}

pos--;

}

if (pos >= 0) {

return new String(array, 0, pos + 1);

}

else {

return null;

}

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值