/** * Gets one of the root keys. * * @param key * key type * @return root key */ private static HKEY getRegistryRootKey(REGISTRY_ROOT_KEY key) { Advapi32 advapi32; HKEYByReference pHandle; HKEY handle = null; advapi32 = Advapi32.INSTANCE; pHandle = new WinReg.HKEYByReference(); if (advapi32.RegOpenKeyEx(rootKeyMap.get(key), null, 0, 0, pHandle) == WINERROR.ERROR_SUCCESS) { handle = pHandle.getValue(); } return (handle); }
/** * Delete a value. * * @param rootKey * root key * @param subKeyName * key name * @param name * value name * @return true on success */ public static boolean deleteValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) { Advapi32 advapi32; HKEY handle; boolean ret = true; advapi32 = Advapi32.INSTANCE; handle = openKey(rootKey, subKeyName, WinNT.KEY_READ | WinNT.KEY_WRITE); if (handle != null) { if (advapi32.RegDeleteValue(handle, name) == WINERROR.ERROR_SUCCESS) { ret = true; } advapi32.RegCloseKey(handle); } return (ret); }
/** * Writes a String value. * * @param rootKey * root key * @param subKeyName * key name * @param name * value name * @param value * value * @throws java.io.UnsupportedEncodingException * on error * @return true on success */ public static boolean setStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name, String value) throws UnsupportedEncodingException { Advapi32 advapi32; HKEY handle; byte[] data; boolean ret = false; data = Arrays.copyOf(value.getBytes("UTF-16LE"), value.length() * 2 + 2); advapi32 = Advapi32.INSTANCE; handle = openKey(rootKey, subKeyName, WinNT.KEY_READ | WinNT.KEY_WRITE); if (handle != null) { if (advapi32.RegSetValueEx(handle, name, 0, WinNT.REG_SZ, data, data.length) == WINERROR.ERROR_SUCCESS) { ret = true; } advapi32.RegCloseKey(handle); } return (ret); }
/** * Delete a key. * * @param rootKey * root key * @param parent * name of parent key * @param name * key name * @return true on success */ public static boolean deleteKey(REGISTRY_ROOT_KEY rootKey, String parent, String name) { Advapi32 advapi32; HKEY handle = null; boolean ret = false; advapi32 = Advapi32.INSTANCE; handle = openKey(rootKey, parent, WinNT.KEY_READ); if (handle != null) { if (advapi32.RegDeleteKey(handle, name) == WINERROR.ERROR_SUCCESS) { ret = true; } else { ret = false; } advapi32.RegCloseKey(handle); } return (ret); }
/** * Create a registry key. * * @param root Root key. * @param parentPath Path to an existing registry key. * @param keyName Key name. * @return True if the key was created, false otherwise. */ public static boolean registryCreateKey(HKEY root, String parentPath, String keyName) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, parentPath, 0, WinNT.KEY_CREATE_SUB_KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { return registryCreateKey(phkKey.getValue(), keyName); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set an integer value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param value Value to write to registry. */ public static void registrySetIntValue(HKEY root, String keyPath, String name, int value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetIntValue(phkKey.getValue(), name, value); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set a string value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param value Value to write to registry. */ public static void registrySetStringValue(HKEY root, String keyPath, String name, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetStringValue(phkKey.getValue(), name, value); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set a string value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param value Value to write to registry. */ public static void registrySetExpandableStringValue(HKEY root, String keyPath, String name, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetExpandableStringValue(phkKey.getValue(), name, value); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set a string array value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param arr Array of strings to write to registry. */ public static void registrySetStringArray(HKEY root, String keyPath, String name, String[] arr) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetStringArray(phkKey.getValue(), name, arr); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set a binary value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param data Data to write to registry. */ public static void registrySetBinaryValue(HKEY root, String keyPath, String name, byte[] data) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetBinaryValue(phkKey.getValue(), name, data); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Delete a registry key. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param keyName Name of the key to delete. */ public static void registryDeleteKey(HKEY root, String keyPath, String keyName) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registryDeleteKey(phkKey.getValue(), keyName); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Delete a registry value. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param valueName Name of the value to delete. */ public static void registryDeleteValue(HKEY root, String keyPath, String valueName) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registryDeleteValue(phkKey.getValue(), valueName); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get names of the registry key's sub-keys. * * @param hKey Registry key. * @return Array of registry key names. */ public static String[] registryGetKeys(HKEY hKey) { IntByReference lpcSubKeys = new IntByReference(); IntByReference lpcMaxSubKeyLen = new IntByReference(); int rc = Advapi32.INSTANCE.RegQueryInfoKey(hKey, null, null, null, lpcSubKeys, lpcMaxSubKeyLen, null, null, null, null, null, null); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } ArrayList<String> keys = new ArrayList<>(lpcSubKeys.getValue()); char[] name = new char[lpcMaxSubKeyLen.getValue() + 1]; for (int i = 0; i < lpcSubKeys.getValue(); i++) { IntByReference lpcchValueName = new IntByReference(lpcMaxSubKeyLen.getValue() + 1); rc = Advapi32.INSTANCE.RegEnumKeyEx(hKey, i, name, lpcchValueName, null, null, null, null); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } keys.add(Native.toString(name)); } return keys.toArray(new String[keys.size()]); }
/** * Get names of the registry key's sub-keys. * * @param root Root key. * @param keyPath Path to a registry key. * @return Array of registry key names. */ public static String[] registryGetKeys(HKEY root, String keyPath) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { return registryGetKeys(phkKey.getValue()); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a table of registry values. * * @param root Registry root. * @param keyPath Regitry key path. * @return Table of values. */ public static TreeMap<String, Object> registryGetValues(HKEY root, String keyPath) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { return registryGetValues(phkKey.getValue()); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Read a String value. * * @param rootKey * root key * @param subKeyName * key name * @param name * value name * @throws java.io.UnsupportedEncodingException * on error * @return String or null */ public static String getStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) throws UnsupportedEncodingException { Advapi32 advapi32; IntByReference pType, lpcbData; byte[] lpData = new byte[1]; HKEY handle = null; String ret = null; advapi32 = Advapi32.INSTANCE; pType = new IntByReference(); lpcbData = new IntByReference(); handle = openKey(rootKey, subKeyName, WinNT.KEY_READ); if (handle != null) { if (advapi32.RegQueryValueEx(handle, name, 0, pType, lpData, lpcbData) == WINERROR.ERROR_MORE_DATA) { lpData = new byte[lpcbData.getValue()]; if (advapi32.RegQueryValueEx(handle, name, 0, pType, lpData, lpcbData) == WINERROR.ERROR_SUCCESS) { ret = convertBufferToString(lpData); } } advapi32.RegCloseKey(handle); } return (ret); }
/** * Read an int value. * * * @return int or 0 * @param rootKey * root key * @param subKeyName * key name * @param name * value name */ public static int getIntValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) { Advapi32 advapi32; IntByReference pType, lpcbData; byte[] lpData = new byte[1]; HKEY handle = null; int ret = 0; advapi32 = Advapi32.INSTANCE; pType = new IntByReference(); lpcbData = new IntByReference(); handle = openKey(rootKey, subKeyName, WinNT.KEY_READ); if (handle != null) { if (advapi32.RegQueryValueEx(handle, name, 0, pType, lpData, lpcbData) == WINERROR.ERROR_MORE_DATA) { lpData = new byte[lpcbData.getValue()]; if (advapi32.RegQueryValueEx(handle, name, 0, pType, lpData, lpcbData) == WINERROR.ERROR_SUCCESS) { ret = convertBufferToInt(lpData); } } advapi32.RegCloseKey(handle); } return (ret); }
/** * Writes an int value. * * * @return true on success * @param rootKey * root key * @param subKeyName * key name * @param name * value name * @param value * value */ public static boolean setIntValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name, int value) { Advapi32 advapi32; HKEY handle; byte[] data; boolean ret = false; data = new byte[4]; data[0] = (byte) (value & 0xff); data[1] = (byte) ((value >> 8) & 0xff); data[2] = (byte) ((value >> 16) & 0xff); data[3] = (byte) ((value >> 24) & 0xff); advapi32 = Advapi32.INSTANCE; handle = openKey(rootKey, subKeyName, WinNT.KEY_READ | WinNT.KEY_WRITE); if (handle != null) { if (advapi32.RegSetValueEx(handle, name, 0, WinNT.REG_DWORD, data, data.length) == WINERROR.ERROR_SUCCESS) { ret = true; } advapi32.RegCloseKey(handle); } return (ret); }
/** * Check for existence of a value. * * @param rootKey * root key * @param subKeyName * key name * @param name * value name * @return true if exists */ public static boolean valueExists(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) { Advapi32 advapi32; IntByReference pType, lpcbData; byte[] lpData = new byte[1]; HKEY handle = null; boolean ret = false; advapi32 = Advapi32.INSTANCE; pType = new IntByReference(); lpcbData = new IntByReference(); handle = openKey(rootKey, subKeyName, WinNT.KEY_READ); if (handle != null) { if (advapi32.RegQueryValueEx(handle, name, 0, pType, lpData, lpcbData) != WINERROR.ERROR_FILE_NOT_FOUND) { ret = true; } else { ret = false; } advapi32.RegCloseKey(handle); } return (ret); }
/** * Get all sub keys of a key. * * @param rootKey * root key * @param parent * key name * @return array with all sub key names */ public static String[] getSubKeys(REGISTRY_ROOT_KEY rootKey, String parent) { Advapi32 advapi32; HKEY handle = null; int dwIndex; char[] lpName; IntByReference lpcName; WinBase.FILETIME lpftLastWriteTime; TreeSet<String> subKeys = new TreeSet<String>(); advapi32 = Advapi32.INSTANCE; handle = openKey(rootKey, parent, WinNT.KEY_READ); lpName = new char[256]; lpcName = new IntByReference(256); lpftLastWriteTime = new WinNT.FILETIME(); if (handle != null) { dwIndex = 0; while (advapi32.RegEnumKeyEx(handle, dwIndex, lpName, lpcName, null, null, null, lpftLastWriteTime) == WINERROR.ERROR_SUCCESS) { subKeys.add(new String(lpName, 0, lpcName.getValue())); lpcName.setValue(256); dwIndex++; } advapi32.RegCloseKey(handle); } return (subKeys.toArray(new String[] {})); }
/** * Checks whether a registry key exists. * * @param root HKEY_LOCAL_MACHINE, etc. * @param key Path to the registry key. * @return True if the key exists. */ public static boolean registryKeyExists(HKEY root, String key) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); switch (rc) { case W32Errors.ERROR_SUCCESS: Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); return true; case W32Errors.ERROR_FILE_NOT_FOUND: return false; default: throw new Win32Exception(rc); } }
/** * Checks whether a registry value exists. * * @param root HKEY_LOCAL_MACHINE, etc. * @param key Registry key path. * @param value Value name. * @return True if the value exists. */ public static boolean registryValueExists(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); try { switch (rc) { case W32Errors.ERROR_SUCCESS: break; case W32Errors.ERROR_FILE_NOT_FOUND: return false; default: throw new Win32Exception(rc); } IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); switch (rc) { case W32Errors.ERROR_SUCCESS: case W32Errors.ERROR_INSUFFICIENT_BUFFER: return true; case W32Errors.ERROR_FILE_NOT_FOUND: return false; default: throw new Win32Exception(rc); } } finally { if (phkKey.getValue() != null && phkKey.getValue() != WinBase.INVALID_HANDLE_VALUE) { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } } }
/** * Get a registry REG_SZ value. * * @param root Root key. * @param key Registry path. * @param value Name of the value to retrieve. * @return String value. */ public static String registryGetStringValue(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_SZ) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_SZ"); } char[] data = new char[lpcbData.getValue()]; rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } return Native.toString(data); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a registry REG_EXPAND_SZ value. * * @param root Root key. * @param key Registry path. * @param value Name of the value to retrieve. * @return String value. */ public static String registryGetExpandableStringValue(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_EXPAND_SZ) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_SZ"); } char[] data = new char[lpcbData.getValue()]; rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } return Native.toString(data); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a registry REG_MULTI_SZ value. * * @param root Root key. * @param key Registry path. * @param value Name of the value to retrieve. * @return String value. */ public static String[] registryGetStringArray(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_MULTI_SZ) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_SZ"); } Memory data = new Memory(lpcbData.getValue()); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } ArrayList<String> result = new ArrayList<>(); int offset = 0; while (offset < data.size()) { String s = data.getString(offset, true); offset += s.length() * Native.WCHAR_SIZE; offset += Native.WCHAR_SIZE; result.add(s); } return result.toArray(new String[result.size()]); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a registry REG_BINARY value. * * @param root Root key. * @param key Registry path. * @param value Name of the value to retrieve. * @return String value. */ public static byte[] registryGetBinaryValue(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_BINARY) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_BINARY"); } byte[] data = new byte[lpcbData.getValue()]; rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } return data; } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a registry DWORD value. * * @param root Root key. * @param key Registry key path. * @param value Name of the value to retrieve. * @return Integer value. */ public static int registryGetIntValue(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_DWORD) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_SZ"); } IntByReference data = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } return data.getValue(); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Create a registry key. * * @param hKey Parent key. * @param keyName Key name. * @return True if the key was created, false otherwise. */ public static boolean registryCreateKey(HKEY hKey, String keyName) { HKEYByReference phkResult = new HKEYByReference(); IntByReference lpdwDisposition = new IntByReference(); int rc = Advapi32.INSTANCE.RegCreateKeyEx(hKey, keyName, 0, null, WinNT.REG_OPTION_NON_VOLATILE, WinNT.KEY_READ, null, phkResult, lpdwDisposition); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } rc = Advapi32.INSTANCE.RegCloseKey(phkResult.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } return WinNT.REG_CREATED_NEW_KEY == lpdwDisposition.getValue(); }
/** * Set an integer value in registry. * * @param hKey Parent key. * @param name Value name. * @param value Value to write to registry. */ public static void registrySetIntValue(HKEY hKey, String name, int value) { byte[] data = new byte[4]; data[0] = (byte) (value & 0xff); data[1] = (byte) ((value >> 8) & 0xff); data[2] = (byte) ((value >> 16) & 0xff); data[3] = (byte) ((value >> 24) & 0xff); int rc = Advapi32.INSTANCE.RegSetValueEx(hKey, name, 0, WinNT.REG_DWORD, data, 4); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } }
/** * Set a string value in registry. * * @param hKey Parent key. * @param name Value name. * @param value Value to write to registry. */ public static void registrySetStringValue(HKEY hKey, String name, String value) { char[] data = Native.toCharArray(value); int rc = Advapi32.INSTANCE.RegSetValueEx(hKey, name, 0, WinNT.REG_SZ, data, data.length * Native.WCHAR_SIZE); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } }
/** * Set an expandable string value in registry. * * @param hKey Parent key. * @param name Value name. * @param value Value to write to registry. */ public static void registrySetExpandableStringValue(HKEY hKey, String name, String value) { char[] data = Native.toCharArray(value); int rc = Advapi32.INSTANCE.RegSetValueEx(hKey, name, 0, WinNT.REG_EXPAND_SZ, data, data.length * Native.WCHAR_SIZE); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } }
/** * Delete a registry key. * * @param hKey Parent key. * @param keyName Name of the key to delete. */ public static void registryDeleteKey(HKEY hKey, String keyName) { int rc = Advapi32.INSTANCE.RegDeleteKey(hKey, keyName); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } }
/** * Delete a registry value. * * @param hKey Parent key. * @param valueName Name of the value to delete. */ public static void registryDeleteValue(HKEY hKey, String valueName) { int rc = Advapi32.INSTANCE.RegDeleteValue(hKey, valueName); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } }
/** * Get all values under a key. * * @param rootKey * root key * @param key * jey name * @throws java.io.UnsupportedEncodingException * on error * @return TreeMap with name and value pairs */ public static TreeMap<String, Object> getValues(REGISTRY_ROOT_KEY rootKey, String key) throws UnsupportedEncodingException { Advapi32 advapi32; HKEY handle = null; int dwIndex, result = 0; char[] lpValueName; byte[] lpData; IntByReference lpcchValueName, lpType, lpcbData; String name; TreeMap<String, Object> values = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER); advapi32 = Advapi32.INSTANCE; handle = openKey(rootKey, key, WinNT.KEY_READ); lpValueName = new char[16384]; lpcchValueName = new IntByReference(16384); lpType = new IntByReference(); lpData = new byte[1]; lpcbData = new IntByReference(); if (handle != null) { dwIndex = 0; do { lpcbData.setValue(0); result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null, lpType, lpData, lpcbData); if (result == WINERROR.ERROR_MORE_DATA) { lpData = new byte[lpcbData.getValue()]; lpcchValueName = new IntByReference(16384); result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null, lpType, lpData, lpcbData); if (result == WINERROR.ERROR_SUCCESS) { name = new String(lpValueName, 0, lpcchValueName.getValue()); switch (lpType.getValue()) { case WinNT.REG_SZ: values.put(name, convertBufferToString(lpData)); break; case WinNT.REG_DWORD: values.put(name, convertBufferToInt(lpData)); break; default: break; } } } dwIndex++; } while (result == WINERROR.ERROR_SUCCESS); advapi32.RegCloseKey(handle); } return (values); }
public int RegQueryValueEx(HKEY hKey, String lpValueName, int lpReserved, IntByReference lpType, byte[] lpData, IntByReference lpcbData);
public int RegQueryValueEx(HKEY hKey, String lpValueName, int lpReserved, IntByReference lpType, IntByReference lpData, IntByReference lpcbData);
public int RegQueryValueEx(HKEY hKey, String lpValueName, int lpReserved, IntByReference lpType, LongByReference lpData, IntByReference lpcbData);
public int RegQueryValueEx(HKEY hKey, String lpValueName, int lpReserved, IntByReference lpType, Pointer lpData, IntByReference lpcbData);
public int RegSetValueEx(HKEY hKey, String lpValueName, int Reserved, int dwType, byte[] lpData, int cbData);