62,636
社区成员




package available.registry.test;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
import com.ice.jni.registry.RegistryValue;
// 这个类使用了 registry.jar 中 jni 提供的功能!
public class RegistryManager {
public static void main(String[] args) throws NoSuchKeyException,
RegistryException {
// 提示如何在命令行下使用 registry.jar 文件,参数到底是什么意思?
// Registry.usage("123");
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
RegistryKey registryKey = Registry.openSubkey(
Registry.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\",
RegistryKey.ACCESS_READ);
// API 上声称返回的是一个 RegistryKey 类型的枚举,结果死活是String 类型的~
Enumeration<?> enums = registryKey.keyElements();
while (enums.hasMoreElements()) {
String subRKName = (String) enums.nextElement();
System.out.println("之前 " + subRKName);
subRKName = RegistryManager.make8859toGB(subRKName);
System.out.println("之后 " + subRKName);
RegistryKey sub = null;
try {
sub = registryKey.openSubKey(subRKName, RegistryKey.ACCESS_READ);
} catch (com.ice.jni.registry.NoSuchKeyException e) {
System.err.println("找不到这个键!");
}
if (sub != null) {
System.out.println("子键的名字为:" + sub.getName());
RegistryValue rv = sub.getValue("DisplayName");
if (rv != null) {
System.err.println(sub.getName()+" 子键 DisplayName 属性的值为:"+new String(rv.getByteData()));
System.out.println();
}
}
}
}
// iso-8859-1
public static String make8859toGB(String str) {
try {
// String str8859 = URLEncoder.encode(str, "Unicode");
// str8859 = URLDecoder.decode(str, "ASCII");
// String str8859 = URLDecoder.decode(str, "GBK");
// 从第二个参数的格式转换为第一个参数的格式~
String str8859 = new String(str.getBytes("GBK"), "ASCII");
return str8859;
} catch (UnsupportedEncodingException ioe) {
ioe.printStackTrace();
}
return str;
}
}
之前 360SD
之后 360SD
子键的名字为:360SD
360SD 子键 DisplayName 属性的值为:360????
之前 360?????¦
之后 360??????W
找不到这个键!
之前 360????????
之后 360????????
之前 Adobe AIR
之后 Adobe AIR
子键的名字为:Adobe AIR
找不到这个键!
之前 Adobe Flash Player ActiveX
之后 Adobe Flash Player ActiveX
子键的名字为:Adobe Flash Player ActiveX
Adobe AIR 子键 DisplayName 属性的值为:Adobe AIR
Adobe Flash Player ActiveX 子键 DisplayName 属性的值为:Adobe Flash Player 10 ActiveX
之前 Adobe Flash Player Plugin
之后 Adobe Flash Player Plugin
子键的名字为:Adobe Flash Player Plugin
之前 Branding
之后 Branding
子键的名字为:Branding
Adobe Flash Player Plugin 子键 DisplayName 属性的值为:Adobe Flash Player 10 Plugin
Exception in thread "main" com.ice.jni.registry.NoSuchValueException: RegQueryValueEx(), value='DisplayName'
at com.ice.jni.registry.RegistryKey.getValue(Native Method)
at available.registry.test.RegistryManager.main(RegistryManager.java:44)
/**
* 将dll获取的字符串拼接回原来的形式.
* 因为dll内以前的方法只是单纯的将byte复制到java的char里
* if ( uniBuf != NULL )
{
for ( i = 0 ; i < len ; ++i )
uniBuf[i] = (jchar) buf[i];
result = (*env)->NewString( env, uniBuf, (jsize)len );
free( uniBuf );
}
return result;
* @param str 从dll获取的字符串
* @return
* @throws UnsupportedEncodingException
*/
static String decode(String str) throws UnsupportedEncodingException{
char[] charbuf = str.toCharArray();
byte[] bytebuf = new byte[charbuf.length];
for(int i=0;i<charbuf.length;i++){
bytebuf[i] = (byte)charbuf[i];
}
return new String(bytebuf,"gbk");
}
/**
* 相反要传入中文的字符来操作,需要修改中文为他所识别的乱码...即将中文按两字节一个,拆分开
* @param str
* @return
* @throws UnsupportedEncodingException
*/
static String encode(String str) throws UnsupportedEncodingException{
byte[] bytebuf = str.getBytes("gbk");
char[] charbuf = new char[bytebuf.length];
for(int i=0;i<bytebuf.length;i++){
charbuf[i] = (char)bytebuf[i];
}
return new String(charbuf,0,charbuf.length);
}
package available.registry.basic;
import java.io.UnsupportedEncodingException;
public class RegistryUtil {
/**
* 将dll获取的字符串拼接回原来的形式.
* 因为dll内以前的方法只是单纯的将byte复制到java的char里
* if ( uniBuf != NULL )
{
for ( i = 0 ; i < len ; ++i )
uniBuf[i] = (jchar) buf[i];
result = (*env)->NewString( env, uniBuf, (jsize)len );
free( uniBuf );
}
return result;
* @param str 从dll获取的字符串
* @return
* @throws UnsupportedEncodingException
*/
public static String decode(String str) {
String result = null;
char[] charbuf = str.toCharArray();
byte[] bytebuf = new byte[charbuf.length];
for(int i=0;i<charbuf.length;i++){
bytebuf[i] = (byte)charbuf[i];
}
try {
result = new String(bytebuf,"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
/**
* 相反要传入中文的字符来操作,需要修改中文为他所识别的乱码...即将中文按两字节一个,拆分开
* @param str
* @return
*/
public static String encode(String str) {
byte[] bytebuf = null;
try {
bytebuf = str.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
char[] charbuf = new char[bytebuf.length];
for(int i=0;i<bytebuf.length;i++){
charbuf[i] = (char)bytebuf[i];
}
return new String(charbuf,0,charbuf.length);
}
}
public class CopyOfRegistryManager2 {
public static void main(String[] args) throws NoSuchKeyException,
RegistryException {
RegistryKey registryKey = Registry.openSubkey(
Registry.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
RegistryKey.ACCESS_READ);
registryKey.createSubKey(RegistryUtil.decode("成功"), "");
}
}
package available.registry.test;
import java.util.Enumeration;
import available.registry.basic.RegistryUtil;
import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
import com.ice.jni.registry.RegistryValue;
// 这个类使用了 registry.jar 中 jni 提供的功能!
public class RegistryManager {
private final Object o = new Object();
public static void main(String[] args) throws NoSuchKeyException,
RegistryException {
// 提示如何在命令行下使用 registry.jar 文件,参数到底是什么意思?
// Registry.usage("123");
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
RegistryKey rootRK = Registry.openSubkey(
Registry.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\",
RegistryKey.ACCESS_READ);
RegistryManager rm = new RegistryManager();
Enumeration<?> enums = rootRK.keyElements();
while (enums.hasMoreElements()) {
synchronized(rm.o) {
String childRKName = (String)enums.nextElement();
String childRKNameDecoded = RegistryUtil.decode(childRKName);
System.err.println("之后 " + childRKNameDecoded);
rm.getDisplayAttr(rootRK, childRKNameDecoded);
}
}
}
// “根键” 和 “枚举出来的子键名”
public synchronized void getDisplayAttr(RegistryKey rootRK, String childRKNameDecoded) throws NoSuchKeyException,
RegistryException {
RegistryKey childRK = null;
try {
childRK = rootRK.openSubKey(RegistryUtil.encode(childRKNameDecoded), RegistryKey.ACCESS_READ);
} catch (com.ice.jni.registry.NoSuchKeyException e) {
System.out.println(childRKNameDecoded + " 找不到这个键!");
}
if(childRK != null) {
// 获取子键的 DisplayName 属性的值
String subKeyName = RegistryUtil.decode(childRK.getName());
System.out.println("子键的名字为:" + subKeyName);
RegistryValue rv = null;
try {
rv = childRK.getValue("DisplayName");
} catch (com.ice.jni.registry.NoSuchValueException e) {
System.err.println("该子键没有这个 DisplayName 这个属性~");
}
if (rv != null) {
System.out.print(childRK.getName()+" 子键 DisplayName 属性的值为:");
String name = RegistryUtil.decode(new String(rv.getByteData()));
if(name == null) {
System.err.println("null");
} else if(name.equals("")) {
System.err.println("equals(\"\")");
} else {
System.err.println(name);
}
System.out.println();
}
}
}
}