/** * Opens the specified key path and reads the specified value. */ private static String readKeyValue(Integer keyHandle, String keyPath, String valueName) { Integer actualKeyHandle = null; try { // Open key: actualKeyHandle = openKey(keyHandle, keyPath); // If key does not exists, abort: if (actualKeyHandle == null) return null; // Read value: return queryKeyValue(actualKeyHandle, valueName); } finally { // Close key, if open: closeKey(actualKeyHandle); } }
/** * Opens the specified key path and reads the specified value from the current user settings. * * @param valueName name of value */ public static String readKeyValueCurrentUser(String keyPath, String valueName) { return readKeyValue(HKEY_CURRENT_USER, keyPath, valueName); }
/** * Queries the specified key handle for the specified value. * * @param keyHandle handle to key to query. * @param valueName name of value to query. * @return The specified value's data. If not exists, null. */ private static String queryKeyValue(Integer keyHandle, String valueName) { // Translate String arguments to raw bytes: byte[] rawValueName = toByteArray(valueName); // Invoke registry operations: byte[] rawResult = invokeRegistryMethod(_queryValueMethod, keyHandle, rawValueName); // Translate result to java string: if (rawResult == null) return null; // Translate the byte array to string (remove the null-termination): String result = new String(rawResult, 0, rawResult.length - 1); return result; }
/** * Opens a registry key. * * @param baseKey One of the predefined keys, or a key previously returned by openKey. * @param keyPath Path relative to baseKey. * @return Handle to opened key. Null if not exists. */ private static Integer openKey(Integer baseKey, String keyPath) { return openKey(baseKey, keyPath, KEY_READ); }
/** * Closes a previously opened key. * * @param keyHandle handle to opened key. */ private static void closeKey(Integer keyHandle) { if (keyHandle == null) return; // Invoke registry operation, get result: Integer result = invokeRegistryMethod(_closeKeyMethod, keyHandle); if (result != ERROR_SUCCESS) throw new WindowsRegistryException("Error. Code=" + result); }
/** * Opens a registry key. * * @param baseKey One of the predefined keys, or a key previously returned by openKey. * @param keyPath Path relative to baseKey. * @param securityToken Security modifiers for key. * @return Handle to opened key. Null if not exists. */ private static Integer openKey(Integer baseKey, String keyPath, int securityToken) { // Invoke registry operation, get result: int[] result = invokeRegistryMethod(_openKeyMethod, baseKey, toByteArray(keyPath), securityToken); // Check result: int errorCode = result[ERROR_CODE]; switch (errorCode) { case ERROR_SUCCESS: return result[NATIVE_HANDLE]; case ERROR_FILE_NOT_FOUND: return null; case ERROR_ACCESS_DENIED: throw new WindowsRegistryException("Access denied. Path=" + keyPath + ", securityToken=" + securityToken); default: throw new WindowsRegistryException("Error. Code=" + errorCode + ", Path=" + keyPath + ", securityToken=" + securityToken); } }
/** * Opens the specified key path and reads the specified value from the local machine settings. */ public static String readKeyValueLocalMachine(String keyPath, String valueName) { return readKeyValue(HKEY_LOCAL_MACHINE, keyPath, valueName); }