请问如何用java取得win2000系统注册表中的信息?谢谢!

duduma139 2003-10-15 03:04:37
请问如何用java取得win2000系统注册表中的信息?谢谢!
...全文
74 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
MarsZ 2003-10-15
  • 打赏
  • 举报
回复
最后一段太长只能分两块
//------------------------Win32RegKey.c
/**
* @version 1.00 1997-07-01
* @author Cay Horstmann
*/

#include "Win32RegKey.h"
#include "Win32RegKeyNameEnumeration.h"
#include <string.h>
#include <stdlib.h>
#include <windows.h>

JNIEXPORT jobject JNICALL Java_Win32RegKey_getValue
(JNIEnv* env, jobject this_obj, jstring name)
{ const char* cname;
jstring path;
const char* cpath;
HKEY hkey;
DWORD type;
DWORD size;
jclass this_class;
jfieldID id_root;
jfieldID id_path;
HKEY root;
jobject ret;
char* cret;

/* get the class */
this_class = (*env)->GetObjectClass(env, this_obj);

/* get the field IDs */
id_root = (*env)->GetFieldID(env, this_class, "root", "I");
id_path = (*env)->GetFieldID(env, this_class, "path",
"Ljava/lang/String;");

/* get the fields */
root = (HKEY)(*env)->GetIntField(env, this_obj, id_root);
path = (jstring)(*env)->GetObjectField(env, this_obj,
id_path);
cpath = (*env)->GetStringUTFChars(env, path, NULL);

/* open the registry key */
if (RegOpenKeyEx(root, cpath, 0, KEY_READ, &hkey)
!= ERROR_SUCCESS)
{ (*env)->ThrowNew(env,
(*env)->FindClass(env, "Win32RegKeyException"),
"Open key failed");
(*env)->ReleaseStringUTFChars(env, path, cpath);
return NULL;
}

(*env)->ReleaseStringUTFChars(env, path, cpath);
cname = (*env)->GetStringUTFChars(env, name, NULL);

/* find the type and size of the value */
if (RegQueryValueEx(hkey, cname, NULL, &type, NULL, &size) != ERROR_SUCCESS)
{ (*env)->ThrowNew(env,
(*env)->FindClass(env, "Win32RegKeyException"),
"Query value key failed");
RegCloseKey(hkey);
(*env)->ReleaseStringUTFChars(env, name, cname);
return NULL;
}

/* get memory to hold the value */
cret = (char*)malloc(size);

/* read the value */
if (RegQueryValueEx(hkey, cname, NULL, &type, cret, &size) != ERROR_SUCCESS)
{ (*env)->ThrowNew(env,
(*env)->FindClass(env, "Win32RegKeyException"),
"Query value key failed");
free(cret);
RegCloseKey(hkey);
(*env)->ReleaseStringUTFChars(env, name, cname);
return NULL;
}

/* depending on the type, store the value in a string,
integer or byte array */
if (type == REG_SZ)
{ ret = (*env)->NewStringUTF(env, cret);
}
else if (type == REG_DWORD)
{ jclass class_Integer = (*env)->FindClass(env,
"java/lang/Integer");
/* get the method ID of the constructor */
jmethodID id_Integer = (*env)->GetMethodID(env,
class_Integer, "<init>", "(I)V");
int value = *(int*)cret;
/* invoke the constructor */
ret = (*env)->NewObject(env, class_Integer, id_Integer,
value);
}
else if (type == REG_BINARY)
{ ret = (*env)->NewByteArray(env, size);
(*env)->SetByteArrayRegion(env, (jarray)ret, 0, size,
cret);
}
else
{ (*env)->ThrowNew(env,
(*env)->FindClass(env, "Win32RegKeyException"),
"Unsupported value type");
ret = NULL;
}

free(cret);
RegCloseKey(hkey);
(*env)->ReleaseStringUTFChars(env, name, cname);

return ret;
}

JNIEXPORT void JNICALL Java_Win32RegKey_setValue
(JNIEnv* env, jobject this_obj, jstring name, jobject value)
{ const char* cname;
jstring path;
const char* cpath;
HKEY hkey;
DWORD type;
DWORD size;
jclass this_class;
jclass class_value;
jclass class_Integer;
jfieldID id_root;
jfieldID id_path;
HKEY root;
const char* cvalue;
int ivalue;

/* get the class */
this_class = (*env)->GetObjectClass(env, this_obj);

/* get the field IDs */
id_root = (*env)->GetFieldID(env, this_class, "root", "I");
id_path = (*env)->GetFieldID(env, this_class, "path",
"Ljava/lang/String;");

/* get the fields */
root = (HKEY)(*env)->GetIntField(env, this_obj, id_root);
path = (jstring)(*env)->GetObjectField(env, this_obj,
id_path);
cpath = (*env)->GetStringUTFChars(env, path, NULL);

/* open the registry key */
if (RegOpenKeyEx(root, cpath, 0, KEY_WRITE, &hkey)
!= ERROR_SUCCESS)
{ (*env)->ThrowNew(env,
(*env)->FindClass(env, "Win32RegKeyException"),
"Open key failed");
(*env)->ReleaseStringUTFChars(env, path, cpath);
return;
}

(*env)->ReleaseStringUTFChars(env, path, cpath);
cname = (*env)->GetStringUTFChars(env, name, NULL);

class_value = (*env)->GetObjectClass(env, value);
class_Integer = (*env)->FindClass(env, "java/lang/Integer");
/* determine the type of the value object */
if ((*env)->IsAssignableFrom(env, class_value,
(*env)->FindClass(env, "java/lang/String")))
{ /* it is a string--get a pointer to the characters */
cvalue = (*env)->GetStringUTFChars(env, (jstring)value,
NULL);
type = REG_SZ;
size = (*env)->GetStringLength(env, (jstring)value) + 1;
}
else if ((*env)->IsAssignableFrom(env, class_value,
class_Integer))
{ /* it is an integer--call intValue to get the value */
jmethodID id_intValue = (*env)->GetMethodID(env,
class_Integer, "intValue", "()I");
ivalue = (*env)->CallIntMethod(env, value, id_intValue);
type = REG_DWORD;
cvalue = (char*)&ivalue;
size = 4;
}
else if ((*env)->IsAssignableFrom(env, class_value,
(*env)->FindClass(env, "[B")))
{ /* it is a byte array--get a pointer to the bytes */
type = REG_BINARY;
cvalue = (char*)(*env)->GetByteArrayElements(env,
(jarray)value, NULL);
size = (*env)->GetArrayLength(env, (jarray)value);
}
else
{ /* we don't know how to handle this type */
(*env)->ThrowNew(env,
(*env)->FindClass(env, "Win32RegKeyException"),
"Unsupported value type");
RegCloseKey(hkey);
(*env)->ReleaseStringUTFChars(env, name, cname);
return;
}

/* set the value */
if (RegSetValueEx(hkey, cname, 0, type, cvalue, size)
!= ERROR_SUCCESS)
{ (*env)->ThrowNew(env,
(*env)->FindClass(env, "Win32RegKeyException"),
"Query value key failed");
}

RegCloseKey(hkey);
(*env)->ReleaseStringUTFChars(env, name, cname);

/* if the value was a string or byte array, release the
pointer */
if (type == REG_SZ)
{ (*env)->ReleaseStringUTFChars(env, (jstring)value,
cvalue);
}
else if (type == REG_BINARY)
{ (*env)->ReleaseByteArrayElements(env, (jarray)value,
(byte*)cvalue, 0);
}
}
MarsZ 2003-10-15
  • 打赏
  • 举报
回复
//--------------------------Win32RegKeyNameEnumeration.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Win32RegKeyNameEnumeration */

#ifndef _Included_Win32RegKeyNameEnumeration
#define _Included_Win32RegKeyNameEnumeration
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Win32RegKeyNameEnumeration
* Method: nextElement
* Signature: ()Ljava/lang/Object;
*/
JNIEXPORT jobject JNICALL Java_Win32RegKeyNameEnumeration_nextElement
(JNIEnv *, jobject);

/*
* Class: Win32RegKeyNameEnumeration
* Method: hasMoreElements
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_Win32RegKeyNameEnumeration_hasMoreElements
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
MarsZ 2003-10-15
  • 打赏
  • 举报
回复
corejava最后一章有写了这部分内容,给你贴上源代码吧,要用c编译一个lib

//-------------------------------Win32RegKeyTest.java
/**
* @version 1.00 1997-07-01
* @author Cay Horstmann
*/

import java.util.*;

public class Win32RegKeyTest
{ public static void main(String[] args)
{ Win32RegKey key = new Win32RegKey(
Win32RegKey.HKEY_CURRENT_USER,
"Software\\Microsoft\\MS Setup (ACME)\\User Info");

key.setValue("Default user", "Bozo the clown");
key.setValue("Lucky number", new Integer(13));
key.setValue("Small primes", new byte[]
{ 2, 3, 5, 7, 11 });

Enumeration enum = key.names();

while (enum.hasMoreElements())
{ String name = (String)enum.nextElement();
System.out.print(name + " = ");

Object value = key.getValue(name);

if (value instanceof byte[])
{ byte[] bvalue = (byte[])value;
for (int i = 0; i < bvalue.length; i++)
System.out.print((bvalue[i] & 0xFF) + " ");
}
else System.out.print(value);

System.out.println();
}
}
}


//---------------------------------Win32RegKey.java
/**
* @version 1.00 1997-07-01
* @author Cay Horstmann
*/

import java.util.*;

public class Win32RegKey
{ public Win32RegKey(int theRoot, String thePath)
{ root = theRoot;
path = thePath;
}
public Enumeration names()
{ return new Win32RegKeyNameEnumeration(root, path);
}
public native Object getValue(String name);
public native void setValue(String name, Object value);

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_CURRENT_CONFIG = 0x80000005;
public static final int HKEY_DYN_DATA = 0x80000006;

private int root;
private String path;

static
{ System.loadLibrary("Win32RegKey");
}
}

class Win32RegKeyNameEnumeration implements Enumeration
{ Win32RegKeyNameEnumeration(int theRoot, String thePath)
{ root = theRoot;
path = thePath;
}

public native Object nextElement();
public native boolean hasMoreElements();

private int root;
private String path;
private int index = -1;
private int hkey = 0;
private int maxsize;
private int count;
}

class Win32RegKeyException extends RuntimeException
{ public Win32RegKeyException() {}
public Win32RegKeyException(String why)
{ super(why);
}
}

//----------------------------Win32RegKey.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Win32RegKey */

#ifndef _Included_Win32RegKey
#define _Included_Win32RegKey
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Win32RegKey
* Method: getValue
* Signature: (Ljava/lang/String;)Ljava/lang/Object;
*/
JNIEXPORT jobject JNICALL Java_Win32RegKey_getValue
(JNIEnv *, jobject, jstring);

/*
* Class: Win32RegKey
* Method: setValue
* Signature: (Ljava/lang/String;Ljava/lang/Object;)V
*/
JNIEXPORT void JNICALL Java_Win32RegKey_setValue
(JNIEnv *, jobject, jstring, jobject);

#ifdef __cplusplus
}
#endif
#endif
LoveRose 2003-10-15
  • 打赏
  • 举报
回复
有点难度
搜索一下,你可以看看下面的文章。
http://expert.csdn.net/Expert/topic/2295/2295927.xml?temp=.9048426

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧