/* 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;
}
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");
}
/* 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);
}
}
//--------------------------Win32RegKeyNameEnumeration.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Win32RegKeyNameEnumeration */
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);
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;