真够事儿的,帖子内容不能长还不能连续回3次,接java读写注册表

MarsZ 2003-10-15 04:16:42
static int startNameEnumeration(JNIEnv* env, jobject this_obj,
jclass this_class)
/* helper function to start enumeration of names
*/
{ jfieldID id_index;
jfieldID id_count;
jfieldID id_root;
jfieldID id_path;
jfieldID id_hkey;
jfieldID id_maxsize;

HKEY root;
jstring path;
const char* cpath;
HKEY hkey;
int maxsize = 0;
int count = 0;

/* get the field IDs */
id_root = (*env)->GetFieldID(env, this_class, "root", "I");
id_path = (*env)->GetFieldID(env, this_class, "path",
"Ljava/lang/String;");
id_hkey = (*env)->GetFieldID(env, this_class, "hkey", "I");
id_maxsize = (*env)->GetFieldID(env, this_class, "maxsize",
"I");
id_index = (*env)->GetFieldID(env, this_class, "index",
"I");
id_count = (*env)->GetFieldID(env, this_class, "count",
"I");

/* get the field values */
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 -1;
}
(*env)->ReleaseStringUTFChars(env, path, cpath);

/* query count and max length of names */
if (RegQueryInfoKey(hkey, NULL, NULL, NULL, NULL,
NULL, NULL, &count, &maxsize, NULL, NULL, NULL)
!= ERROR_SUCCESS)
{ (*env)->ThrowNew(env,
(*env)->FindClass(env, "Win32RegKeyException"),
"Query info key failed");
return -1;
}

/* set the field values */
(*env)->SetIntField(env, this_obj, id_hkey, (DWORD)hkey);
(*env)->SetIntField(env, this_obj, id_maxsize, maxsize + 1);
(*env)->SetIntField(env, this_obj, id_index, 0);
(*env)->SetIntField(env, this_obj, id_count, count);
return count;
}

JNIEXPORT jboolean JNICALL
Java_Win32RegKeyNameEnumeration_hasMoreElements
(JNIEnv* env, jobject this_obj)
{ jclass this_class;
jfieldID id_index;
jfieldID id_count;
int index;
int count;
/* get the class */
this_class = (*env)->GetObjectClass(env, this_obj);

/* get the field IDs */
id_index = (*env)->GetFieldID(env, this_class, "index",
"I");
id_count = (*env)->GetFieldID(env, this_class, "count",
"I");

index = (*env)->GetIntField(env, this_obj, id_index);
if (index == -1) /* first time */
{ count = startNameEnumeration(env, this_obj, this_class);
index = 0;
}
else
count = (*env)->GetIntField(env, this_obj, id_count);
return index < count;
}

JNIEXPORT jobject JNICALL
Java_Win32RegKeyNameEnumeration_nextElement
(JNIEnv* env, jobject this_obj)
{ jclass this_class;
jfieldID id_index;
jfieldID id_hkey;
jfieldID id_count;
jfieldID id_maxsize;

HKEY hkey;
int index;
int count;
int maxsize;

char* cret;
jstring ret;

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

/* get the field IDs */
id_index = (*env)->GetFieldID(env, this_class, "index",
"I");
id_count = (*env)->GetFieldID(env, this_class, "count",
"I");
id_hkey = (*env)->GetFieldID(env, this_class, "hkey", "I");
id_maxsize = (*env)->GetFieldID(env, this_class, "maxsize",
"I");

index = (*env)->GetIntField(env, this_obj, id_index);
if (index == -1) /* first time */
{ count = startNameEnumeration(env, this_obj, this_class);
index = 0;
}
else
count = (*env)->GetIntField(env, this_obj, id_count);

if (index >= count) /* already at end */
{ (*env)->ThrowNew(env,
(*env)->FindClass(env,
"java/util/NoSuchElementException"),
"past end of enumeration");
return NULL;
}

maxsize = (*env)->GetIntField(env, this_obj, id_maxsize);
hkey = (HKEY)(*env)->GetIntField(env, this_obj, id_hkey);
cret = (char*)malloc(maxsize);

/* find the next name */
if (RegEnumValue(hkey, index, cret, &maxsize, NULL, NULL,
NULL, NULL) != ERROR_SUCCESS)
{ (*env)->ThrowNew(env,
(*env)->FindClass(env, "Win32RegKeyException"),
"Enum value failed");
free(cret);
RegCloseKey(hkey);
(*env)->SetIntField(env, this_obj, id_index, count);
return NULL;
}

ret = (*env)->NewStringUTF(env, cret);
free(cret);

/* increment index */
index++;
(*env)->SetIntField(env, this_obj, id_index, index);

if (index == count) /* at end */
{ RegCloseKey(hkey);
}

return ret;
}
...全文
30 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,612

社区成员

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

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