Added Windows registry value names enumeration

This commit is contained in:
Michael Putters
2013-12-11 23:48:57 +01:00
parent 730e60cbaf
commit 45dc2bc7be
4 changed files with 68 additions and 0 deletions

View File

@@ -553,4 +553,44 @@ Java_net_rubygrapefruit_platform_internal_jni_WindowsRegistryFunctions_getSubkey
return true;
}
JNIEXPORT jboolean JNICALL
Java_net_rubygrapefruit_platform_internal_jni_WindowsRegistryFunctions_getValueNames(JNIEnv *env, jclass target, jint keyNum, jstring subkey, jobject names, jobject result) {
wchar_t* subkeyStr = java_to_wchar(env, subkey, result);
jclass subkeys_class = env->GetObjectClass(names);
jmethodID method = env->GetMethodID(subkeys_class, "add", "(Ljava/lang/Object;)Z");
HKEY key;
LONG retval = RegOpenKeyExW(get_key_from_ordinal(keyNum), subkeyStr, 0, KEY_READ, &key);
if (retval != ERROR_SUCCESS) {
free(subkeyStr);
if (retval != ERROR_FILE_NOT_FOUND) {
mark_failed_with_code(env, "could open registry key", retval, NULL, result);
}
return false;
}
DWORD valueCount;
DWORD maxValueNameLen;
retval = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL, &valueCount, &maxValueNameLen, NULL, NULL, NULL);
if (retval != ERROR_SUCCESS) {
mark_failed_with_code(env, "could query registry key", retval, NULL, result);
} else {
wchar_t* valueNameStr = (wchar_t*)malloc(sizeof(wchar_t) * (maxValueNameLen+1));
for (int i = 0; i < valueCount; i++) {
DWORD valueNameLen = maxValueNameLen + 1;
retval = RegEnumValueW(key, i, valueNameStr, &valueNameLen, NULL, NULL, NULL, NULL);
if (retval != ERROR_SUCCESS) {
mark_failed_with_code(env, "could enumerate registry value name", retval, NULL, result);
break;
}
env->CallVoidMethod(names, method, wchar_to_java(env, valueNameStr, wcslen(valueNameStr), result));
}
free(valueNameStr);
}
RegCloseKey(key);
free(subkeyStr);
return true;
}
#endif

View File

@@ -23,4 +23,13 @@ public interface WindowsRegistry extends NativeIntegration {
* @throws MissingRegistryEntryException When the requested key does not exist.
*/
List<String> getSubkeys(Key key, String subkey) throws NativeException;
/**
* Lists the value names of a registry key.
*
* @throws NativeException On failure.
* @throws MissingRegistryEntryException When the requested key does not exist.
*/
List<String> getValueNames(Key key, String subkey) throws NativeException;
}

View File

@@ -38,4 +38,20 @@ public class DefaultWindowsRegistry implements WindowsRegistry {
}
return subkeys;
}
public List<String> getValueNames(Key key, String subkey) throws NativeException {
FunctionResult result = new FunctionResult();
ArrayList<String> names = new ArrayList<String>();
boolean found = WindowsRegistryFunctions.getValueNames(key.ordinal(), subkey, names, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not get value names of registry key '%s\\%s': %s", key,
subkey, result.getMessage()));
}
if (!found) {
throw new MissingRegistryEntryException(String.format(
"Could not list the value names of registry key '%s\\%s' as it does not exist.", key, subkey));
}
return names;
}
}

View File

@@ -10,4 +10,7 @@ public class WindowsRegistryFunctions {
// Returns false for unknown key
public static native boolean getSubkeys(int key, String subkey, List<String> subkeys, FunctionResult result);
// Returns false for unknown key
public static native boolean getValueNames(int key, String subkey, List<String> names, FunctionResult result);
}