62,614
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}
}
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);
}
}
String s = new String(rv.getByteData(), "utf-8");
byte[] tmp = new byte[s.length()];
for(int i=0;i<s.length();i++){
tmp[i] = (byte)s.toCharArray()[i];
}
s = new String(tmp,"gbk");//这个是正常结果
package available.registry.test;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import available.registry.basic.RegistryUtil;
import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.NoSuchValueException;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
import com.ice.jni.registry.RegistryValue;
public class CatchException {
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);
Enumeration<?> enums = registryKey.keyElements();
// 360保险箱 0xB1A3 0xCFD5 0xCFE4
while(enums.hasMoreElements()) {
String childRKName = (String)enums.nextElement();
// if(childRKName.equals("360安全卫士")) {
RegistryKey rk = registryKey.openSubKey(childRKName, RegistryKey.ACCESS_READ);
RegistryValue rv = null;
try {
rv = rk.getValue("DisplayName");
} catch (NoSuchValueException e1) {
// 仅仅 catch 运行时异常的话会终止程序运行,只有具体到是哪种 Exception,才会继续执行~
e1.printStackTrace();
}
// 如果上句代码抛出异常,则这里的rv必定为null,不做处理的话必然会报空指针~
if(rv == null) continue;
String s = null;
try {
s = new String(rv.getByteData(), "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(s + " ====================== " +
RegistryUtil.decode(new String(rv.getByteData())));
// }
}
}
}
char *
jStringToNewAscii( JNIEnv *env, jstring jStr )
{
int i;
int utfLen;
jboolean isCopy;
char *asciiBuf;
const char *utfBuf;
utfLen = (*env)->GetStringUTFLength( env, jStr );
utfBuf = (*env)->GetStringUTFChars( env, jStr, &isCopy );
asciiBuf = malloc( utfLen + 2 );
if ( asciiBuf != NULL )
{
for ( i = 0 ; i < utfLen ; ++i )
asciiBuf[i] = utfBuf[i];
asciiBuf[i] = '\0';
(*env)->ReleaseStringUTFChars( env, jStr, utfBuf );
}
return asciiBuf;
}
jstring
strbufToJString( JNIEnv *env, char *buf, int len )
{
jstring encoding = (*env)->NewStringUTF(env, "gbk");
jclass strClass = (*env)->FindClass(env, "Ljava/lang/String;");
jmethodID ctorID = (*env)->GetMethodID(env, strClass, "<init>", "([BLjava/lang/String;)V");
jbyteArray bytes = (*env)->NewByteArray(env, len);
(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)buf);
return (jstring)(*env)->NewObject(env, strClass, ctorID, bytes, encoding);
}
char *
jStringToNewAscii( JNIEnv *env, jstring jstr )
{
char* rtn = NULL;
jclass clsstring = (*env)->FindClass(env, "java/lang/String");
jstring strencode = (*env)->NewStringUTF(env, "gbk");
jmethodID mid = (*env)->GetMethodID(env, clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr= (jbyteArray)(*env)->CallObjectMethod(env, jstr, mid, strencode);
jsize alen = (*env)->GetArrayLength(env, barr);
jbyte* ba = (*env)->GetByteArrayElements(env, barr, JNI_FALSE);
if (alen > 0)
{
rtn = (char*)malloc(alen + 1);
memcpy(rtn, ba, alen);
rtn[alen] = '\0';
}else{
rtn = (char*)malloc(1);
rtn[0] = '\0';
}
(*env)->ReleaseByteArrayElements(env, barr, ba, 0);
return rtn;
}