由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - how to read registry key value using java (64-bit system)
相关主题
这段程序的输出是什么? 为什么interesting
问个简单的Java技术问题问个题
出个简单题,看你Java APi熟悉到什么程度问个primitive type的问题
a fun coding question一道java面试题 (转载)
what is your opinion in this case?菜鸟问个简单的问题
折腾了一天,实在是绝望了,请教请教初学者code请教 (大牛莫取笑)
请教一个简单的问题JAVA 求解
简单问题问一个java基础的初始化的问题,一直搞不明白 (转载)
相关话题的讨论汇总
话题: hkey话题: int话题: string话题: static话题: key
进入Java版参与讨论
1 (共1页)
x*****g
发帖数: 764
1
快折腾一天了,不得不上来问问了。
公司新买一个机器,装的是64-bit的Window Server 2008。放了一个program上去跑,
一开始就停了。问题出在用一个函数去获取java registry 的一些信息,比如版本,
home location 等等。这个程序在32-bit Windows XP上从来没问题,现在也没问题。
然后作了一些研究,大概是64位引起的, 但是那个获取java registry 信息的函数又
不能改,于是只能转而用Java程序来替代,找了一个别人开发的JAR。还是一样,在
Windows XP (32位)上挺好,一到那个新机器上又不行了。 我用的eclipse是64位的。
大家能不能教教我到底怎们用java搞这个啊。 谢谢。
u****s
发帖数: 2186
2
It has nothing to do with Java.
Windows API to retrieve/update registry is different on 64-bit system.
If you write it in Java, it will end up with JNI to Windows API any way.
x*****g
发帖数: 764
3
You are absolutely right. 所有的读/写windoes registry key value 都是通过
Windows API 来实现的,Just want to get some ready to use codes to plug in my
program. 我基础实在太差。

【在 u****s 的大作中提到】
: It has nothing to do with Java.
: Windows API to retrieve/update registry is different on 64-bit system.
: If you write it in Java, it will end up with JNI to Windows API any way.

r*****l
发帖数: 2859
4
I never did such thing before. However, do a google you will find some
candidates, like Jawin, J/Invoke, JWinAPI etc. You can also write your own
native code and use JNI to call it.

my

【在 x*****g 的大作中提到】
: You are absolutely right. 所有的读/写windoes registry key value 都是通过
: Windows API 来实现的,Just want to get some ready to use codes to plug in my
: program. 我基础实在太差。

x*****g
发帖数: 764
5
Thanks! I will find a time to give it a try.

【在 r*****l 的大作中提到】
: I never did such thing before. However, do a google you will find some
: candidates, like Jawin, J/Invoke, JWinAPI etc. You can also write your own
: native code and use JNI to call it.
:
: my

b**l
发帖数: 51
6
No Need JNI. try this:
===
/*
* @(#)WinRegistry.java 1.4 03/12/19
*
* Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
met:
*
* -Redistribution of source code must retain the above copyright notice,
this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors
may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE
THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE
,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
package jnlp.sample.JreInstaller;
/**
* A simple cover class for the Windows registry. The API is
* similar to the Windows C registry API.
*/
public class WinRegistry
{
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;
public static final int HKEY_DYN_DATA = 0x80000006;
static final int KEY_QUERY_VALUE = 0x1;
static final int KEY_SET_VALUE = 0x2;
static final int KEY_CREATE_SUB_KEY = 0x4;
static final int KEY_ENUMERATE_SUB_KEYS = 0x8;
static final int KEY_NOTIFY = 0x10;
static final int KEY_CREATE_LINK = 0x20;
static final int KEY_READ = 0x20019;
static final int KEY_WRITE = 0x20006;
static final int KEY_EXECUTE = 0x20019;
static final int KEY_ALL_ACCESS = 0xf003f;
static final int REG_OPTION_RESERVED = 0x0;
static final int REG_OPTION_NON_VOLATILE = 0x0;
static final int REG_OPTION_VOLATILE = 0x1;
static final int REG_OPTION_CREATE_LINK = 0x2;
static final int REG_OPTION_BACKUP_RESTORE = 0x4;
static final int REG_OPTION_OPEN_LINK = 0x8;
static final int REG_LEGAL_OPTION = 0xf;
static final int REG_CREATED_NEW_KEY = 0x1;
static final int REG_OPENED_EXISTING_KEY = 0x2;
static final int REG_WHOLE_HIVE_VOLATILE = 0x1;
static final int REG_REFRESH_HIVE = 0x2;
static final int REG_NO_LAZY_FLUSH = 0x4;
static final int REG_NOTIFY_CHANGE_NAME = 0x1;
static final int REG_NOTIFY_CHANGE_ATTRIBUTES = 0x2;
static final int REG_NOTIFY_CHANGE_LAST_SET = 0x4;
static final int REG_NOTIFY_CHANGE_SECURITY = 0x8;
static final int REG_LEGAL_CHANGE_FILTER = 0xf;
static final int REG_NONE = 0x0;
static final int REG_SZ = 0x1;
static final int REG_EXPAND_SZ = 0x2;
static final int REG_BINARY = 0x3;
static final int REG_DWORD = 0x4;
static final int REG_DWORD_LITTLE_ENDIAN = 0x4;
static final int REG_DWORD_BIG_ENDIAN = 0x5;
static final int REG_LINK = 0x6;
static final int REG_MULTI_SZ = 0x7;
static final int REG_RESOURCE_LIST = 0x8;
static final int REG_FULL_RESOURCE_DESCRIPTOR = 0x9;
static final int REG_RESOURCE_REQUIREMENTS_LIST = 0xa;
/**
* Registry values are represented by an integer type code, e.g.
* REG_SZ for a string, and an array of bytes. This class combines
* the two and provides a method, getValue(), that converts the array
* of bytes into an appropriate java obejct.
*


* KeyValue objects are created by native methods, see
*/
static class KeyValue {
private final int type;
private final byte[] data;
private Object value = null;
KeyValue(int type, byte[] data) {
this.type = type;
this.data = data;
}

public int getType() {
return type;
}

public byte[] getData() {
return data;
}
public Object getValue() {
switch (type) {
case REG_SZ:
// fix for 4634008, 4506366
// make sure (data.length - 1) is a positive number
if (data.length - 1 < 0) return null;
return new String(data, 0, data.length - 1);
case REG_DWORD:
{
int n = 0;
for(int i = 0; (i < 4) && (i < data.length); i++) {
n += data[i] << (i * 8);
}
return new Integer(n);
}
default:
return getData();
}
}
}
static native void initIDs();
static native int sysOpenKey(int hKey, String subKey, int sam);
static native int sysCloseKey(int hKey);
static native KeyValue sysQueryKey(int hKey, String name);
static native int sysCreateKey(int hKey, String name, int sam);
static native boolean sysSetStringValue(int hKey, String name,
String value);
static native boolean sysDeleteKey(int hKey, String subKey);
static native boolean sysReboot();
/**
* [TBD] This should move to another class.
*/
public static native String getWindowsDirectory();
static {
// Library should have already been loaded by Main
initIDs();
}

public static Object get(int hKey, String path, String name) {
int key = sysOpenKey(hKey, path, KEY_READ);
if (key != 0) {
KeyValue kv = sysQueryKey(key, name);
sysCloseKey(key);
if (kv != null) {
return kv.getValue();
}
}
return null;
}
public static String getString(int hKey, String path, String name) {
Object rv = get(hKey, path, name);
return (rv instanceof String) ? (String)rv : null;
}
public static Integer getInteger(int hKey, String path, String name) {
Object rv = get(hKey, path, name);
if (rv instanceof Integer) {
return (Integer)rv;
} else if (rv instanceof byte[]){
byte[] b = (byte[])rv;
if (b.length == 4 && b[1] == 0 && b[2] == 0 && b[3] == 0) {
return new Integer(b[0]);
} else {
return null;
}
}
return null;
}
public static boolean setStringValue(int hKey, String path, String name,
String value) {
boolean retValue = false;
int key = sysCreateKey(hKey, path, KEY_WRITE);
if (key != 0) {
retValue = sysSetStringValue(key, name, value);
sysCloseKey(key);
}
return retValue;
}
/**
* Returns true if the subkey path exists in
* hkey.
*/
public static boolean doesSubKeyExist(int hKey, String path) {
int key = sysOpenKey(hKey, path, KEY_READ);
if (key != 0) {
sysCloseKey(key);
return true;
}
return false;
}
public static boolean deleteKey(int hKey, String path) {
return sysDeleteKey(hKey, path);
}

public static boolean doReboot() {
return sysReboot();
}
}

b**l
发帖数: 51
7
or this, works on both 32-nit and 64-bit:
====
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
public class WinRegistry {
// inspired by
// http://javabyexample.wisdomplug.com/java-concepts/34-core-
java/62-java-registry-wrapper.html
// http://www.snipcode.org/java/1-java/23-java-class-for-accessing-
reading-and-writing-from-windows-registry.html
// http://snipplr.com/view/6620/accessing-windows-registry-in-java/
public static final int HKEY_CURRENT_USER = 0x80000001;
public static final int HKEY_LOCAL_MACHINE = 0x80000002;
public static final int REG_SUCCESS = 0;
public static final int REG_NOTFOUND = 2;
public static final int REG_ACCESSDENIED = 5;
private static final int KEY_ALL_ACCESS = 0xf003f;
private static final int KEY_READ = 0x20019;
private static Preferences userRoot = Preferences.userRoot();
private static Preferences systemRoot = Preferences.systemRoot();
private static Class userClass =
userRoot.getClass(
);
private static Method regOpenKey = null;
private static Method regCloseKey = null;
private static Method regQueryValueEx = null;
private static Method regEnumValue = null;
private static Method regQueryInfoKey = null;
private static Method regEnumKeyEx = null;
private static Method regCreateKeyEx = null;
private static Method regSetValueEx = null;
private static Method regDeleteKey = null;
private static Method regDeleteValue = null;
static {
try {
regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey",
new Class[] { int.class, byte[].class, int.class });
regOpenKey.setAccessible(true);
regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey",
new Class[] { int.class });
regCloseKey.setAccessible(true);
regQueryValueEx =
userClass.getDeclaredMethod("WindowsRegQueryValueEx",
new Class[] { int.class, byte[].class });
regQueryValueEx.setAccessible(true);
regEnumValue =
userClass.getDeclaredMethod("WindowsRegEnumValue",
new Class[] { int.class, int.class, int.class });
regEnumValue.setAccessible(true);
regQueryInfoKey =
userClass.getDeclaredMethod("WindowsRegQueryInfoKey1
",
new Class[] { int.class });
regQueryInfoKey.setAccessible(true);
regEnumKeyEx = userClass.getDeclaredMethod(
"WindowsRegEnumKeyEx", new Class[] { int.class, int.class,
int.class });
regEnumKeyEx.setAccessible(true);
regCreateKeyEx = userClass.getDeclaredMethod(
"WindowsRegCreateKeyEx", new Class[] { int.class,
byte[].class });
regCreateKeyEx.setAccessible(true);
regSetValueEx = userClass.getDeclaredMethod(
"WindowsRegSetValueEx", new Class[] { int.class,
byte[].class, byte[].class });
regSetValueEx.setAccessible(true);
regDeleteValue = userClass.getDeclaredMethod(
"WindowsRegDeleteValue", new Class[] { int.class,
byte[].class });
regDeleteValue.setAccessible(true);
regDeleteKey = userClass.getDeclaredMethod(
"WindowsRegDeleteKey", new Class[] { int.class,
byte[].class });
regDeleteKey.setAccessible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
private WinRegistry() { }
/**
* Read a value from key and value name
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
* @param key
* @param valueName
* @return the value
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static String readString(int hkey, String key, String
valueName)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
if (hkey == HKEY_LOCAL_MACHINE) {
return readString(systemRoot, hkey, key, valueName);
}
else if (hkey == HKEY_CURRENT_USER) {
return readString(userRoot, hkey, key, valueName);
}
else {
throw new IllegalArgumentException("hkey=" + hkey);
}
}
/**
* Read value(s) and value name(s) form given key
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
* @param key
* @return the value name(s) plus the value(s)
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Map readStringValues(int hkey, String
key)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
if (hkey == HKEY_LOCAL_MACHINE) {
return readStringValues(systemRoot, hkey, key);
}
else if (hkey == HKEY_CURRENT_USER) {
return readStringValues(userRoot, hkey, key);
}
else {
throw new IllegalArgumentException("hkey=" + hkey);
}
}
/**
* Read the value name(s) from a given key
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
* @param key
* @return the value name(s)
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static List readStringSubKeys(int hkey, String key)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
if (hkey == HKEY_LOCAL_MACHINE) {
return readStringSubKeys(systemRoot, hkey, key);
}
else if (hkey == HKEY_CURRENT_USER) {
return readStringSubKeys(userRoot, hkey, key);
}
else {
throw new IllegalArgumentException("hkey=" + hkey);
}
}
/**
* Create a key
* @param hkey HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
* @param key
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void createKey(int hkey, String key)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
int [] ret;
if (hkey == HKEY_LOCAL_MACHINE) {
ret = createKey(systemRoot, hkey, key);
regCloseKey.invoke(systemRoot, new Object[] { new
Integer(ret[0]) });
}
else if (hkey == HKEY_CURRENT_USER) {
ret = createKey(userRoot, hkey, key);
regCloseKey.invoke(userRoot, new Object[] { new Integer(ret[0])
});
}
else {
throw new IllegalArgumentException("hkey=" + hkey);
}
if (ret[1] != REG_SUCCESS) {
throw new IllegalArgumentException("rc=" + ret[1] + " key=" +
key);
}
}
/**
* Write a value in a given key/value name
* @param hkey
* @param key
* @param valueName
* @param value
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void writeStringValue
(int hkey, String key, String valueName, String value)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
if (hkey == HKEY_LOCAL_MACHINE) {
writeStringValue(systemRoot, hkey, key, valueName, value);
}
else if (hkey == HKEY_CURRENT_USER) {
writeStringValue(userRoot, hkey, key, valueName, value);
}
else {
throw new IllegalArgumentException("hkey=" + hkey);
}
}
/**
* Delete a given key
* @param hkey
* @param key
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void deleteKey(int hkey, String key)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
int rc = -1;
if (hkey == HKEY_LOCAL_MACHINE) {
rc = deleteKey(systemRoot, hkey, key);
}
else if (hkey == HKEY_CURRENT_USER) {
rc = deleteKey(userRoot, hkey, key);
}
if (rc != REG_SUCCESS) {
throw new IllegalArgumentException("rc=" + rc + " key=" + key);
}
}
/**
* delete a value from a given key/value name
* @param hkey
* @param key
* @param value
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void deleteValue(int hkey, String key, String value)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
int rc = -1;
if (hkey == HKEY_LOCAL_MACHINE) {
rc = deleteValue(systemRoot, hkey, key, value);
}
else if (hkey == HKEY_CURRENT_USER) {
rc = deleteValue(userRoot, hkey, key, value);
}
if (rc != REG_SUCCESS) {
throw new IllegalArgumentException("rc=" + rc + " key=" + key +
"
value=" + value);
}
}
// =====================
private static int deleteValue
(Preferences root, int hkey, String key, String value)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS)
});
if (handles[1] != REG_SUCCESS) {
return handles[1]; // can be REG_NOTFOUND, REG_ACCESSDENIED
}
int rc =((Integer) regDeleteValue.invoke(root,
new Object[] {
new Integer(handles[0]), toCstr(value)
})).intValue();
regCloseKey.invoke(root, new Object[] { new Integer(handles[0])
});
return rc;
}
private static int deleteKey(Preferences root, int hkey, String key)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
int rc =((Integer) regDeleteKey.invoke(root,
new Object[] { new Integer(hkey), toCstr(key) })).intValue();
return rc; // can REG_NOTFOUND, REG_ACCESSDENIED, REG_SUCCESS
}
private static String readString(Preferences root, int hkey, String
key,
String value)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
if (handles[1] != REG_SUCCESS) {
return null;
}
byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[] {
new Integer(handles[0]), toCstr(value) });
regCloseKey.invoke(root, new Object[] { new Integer(handles[0])
});
return (valb != null ? new String(valb).trim() : null);
}
private static Map readStringValues
(Preferences root, int hkey, String key)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
HashMap results = new HashMap();
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
if (handles[1] != REG_SUCCESS) {
return null;
}
int[] info = (int[]) regQueryInfoKey.invoke(root,
new Object[] { new Integer(handles[0]) });
int count = info[2]; // count
int maxlen = info[3]; // value length max
for(int index=0; index byte[] name = (byte[]) regEnumValue.invoke(root, new Object[] {
new Integer
(handles[0]), new Integer(index), new Integer(maxlen +
1)});
String value = readString(hkey, key, new String(name));
results.put(new String(name).trim(), value);
}
regCloseKey.invoke(root, new Object[] { new Integer(handles[0])
});
return results;
}
private static List readStringSubKeys
(Preferences root, int hkey, String key)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
List results = new ArrayList();
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
new Integer(hkey), toCstr(key), new Integer(KEY_READ)
});
if (handles[1] != REG_SUCCESS) {
return null;
}
int[] info = (int[]) regQueryInfoKey.invoke(root,
new Object[] { new Integer(handles[0]) });
int count = info[2]; // count
int maxlen = info[3]; // value length max
for(int index=0; index byte[] name = (byte[]) regEnumKeyEx.invoke(root, new Object[] {
new Integer
(handles[0]), new Integer(index), new Integer(maxlen + 1)
});
results.add(new String(name).trim());
}
regCloseKey.invoke(root, new Object[] { new Integer(handles[0])
});
return results;
}
private static int [] createKey(Preferences root, int hkey, String
key)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
return (int[]) regCreateKeyEx.invoke(root,
new Object[] { new Integer(hkey), toCstr(key) });
}
private static void writeStringValue
(Preferences root, int hkey, String key, String valueName, String
value)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS)
});
regSetValueEx.invoke(root,
new Object[] {
new Integer(handles[0]), toCstr(valueName), toCstr(value)
});
regCloseKey.invoke(root, new Object[] { new Integer(handles[0])
});
}
// utility
private static byte[] toCstr(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;
}
}
x*****g
发帖数: 764
8
I'll find time to try them
1 (共1页)
进入Java版参与讨论
相关主题
问一个java基础的初始化的问题,一直搞不明白 (转载)what is your opinion in this case?
Urgent!折腾了一天,实在是绝望了,请教请教
ERROR!java.io.RandomAccessFile.readInt请教一个简单的问题
[转载] Java 1.5 Generic 问题简单问题
这段程序的输出是什么? 为什么interesting
问个简单的Java技术问题问个题
出个简单题,看你Java APi熟悉到什么程度问个primitive type的问题
a fun coding question一道java面试题 (转载)
相关话题的讨论汇总
话题: hkey话题: int话题: string话题: static话题: key