/** * Get a registry key, the caller is responsible to close the key after use. * * @param root * Root key. * @param keyPath * Path to a registry key. * * @param samDesired * Access level (e.g. WinNT.KEY_READ) * * @return HKEYByReference. */ public static HKEYByReference registryGetKey(HKEY root, String keyPath, int samDesired) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, samDesired, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } return phkKey; }
/** * Get a registry key, the caller is responsible to close the key after use. * * @param root * Root key. * @param keyPath * Path to a registry key. * * @param samDesired * Access level (e.g. WinNT.KEY_READ) * * @return HKEYByReference. */ public static HKEYByReference registryGetKey(HKEY root, String keyPath, int samDesired) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, samDesired, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } return phkKey; }
/** * 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, 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 key exists. * * @param root * HKEY_LOCAL_MACHINE, etc. * @param key * Path to the registry key. * @param samDesiredExtra * Registry key security and access rights to be requested in addition to WinNT.KEY_READ. * (e.g WinNT.KEY_WOW64_32KEY or WinNT.KEY_WOW64_64KEY to force 32bit or 64bit registry access.) * @return True if the key exists. */ public static boolean registryKeyExists(HKEY root, String key, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, 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); } }
private static TreeMap<String, Object> visualStudioVS7SxS(com.sun.jna.platform.win32.WinReg.HKEY root, String key) { com.sun.jna.platform.win32.WinReg.HKEYByReference phkKey = new com.sun.jna.platform.win32.WinReg.HKEYByReference(); int rc = com.sun.jna.platform.win32.Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, com.sun.jna.platform.win32.WinNT.KEY_READ | com.sun.jna.platform.win32.WinNT.KEY_WOW64_32KEY, phkKey); if (rc != com.sun.jna.platform.win32.W32Errors.ERROR_SUCCESS) { throw new com.sun.jna.platform.win32.Win32Exception(rc); } try { return com.sun.jna.platform.win32.Advapi32Util.registryGetValues(phkKey.getValue()); } finally { rc = com.sun.jna.platform.win32.Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != com.sun.jna.platform.win32.W32Errors.ERROR_SUCCESS) { throw new com.sun.jna.platform.win32.Win32Exception(rc); } } }
/** * Get a table of registry values. * * @param root * Registry root. * @param keyPath * Regitry key path. * @param samDesiredExtra * Registry key security and access rights to be requested in addition to WinNT.KEY_READ. * (e.g WinNT.KEY_WOW64_32KEY or WinNT.KEY_WOW64_64KEY to force 32bit or 64bit registry access.) * @return Table of values. */ public static TreeMap<String, Object> registryGetValues(HKEY root, String keyPath, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | samDesiredExtra, 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); } } }
/** * Delete a registry value. * * @param root * Root key. * @param keyPath * Path to an existing registry key. * @param valueName * Name of the value to delete. * @param samDesiredExtra * Registry key security and access rights to be requested in addition to WinNT.KEY_READ and WinNT.KEY_WRITE. * (e.g WinNT.KEY_WOW64_32KEY or WinNT.KEY_WOW64_64KEY to force 32bit or 64bit registry access.) */ public static void registryDeleteValue(HKEY root, String keyPath, String valueName, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, 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); } } }
/** * 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, 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); } } }
/** * 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, 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); } } }
String keyName, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, parentPath, 0, WinNT.KEY_CREATE_SUB_KEY | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) {
/** * 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. */ private static String registryGet32StringValue(com.sun.jna.platform.win32.WinReg.HKEY root, String key, String value) throws com.sun.jna.platform.win32.Win32Exception { com.sun.jna.platform.win32.WinReg.HKEYByReference phkKey = new com.sun.jna.platform.win32.WinReg.HKEYByReference(); int rc = com.sun.jna.platform.win32.Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, com.sun.jna.platform.win32.WinNT.KEY_READ | com.sun.jna.platform.win32.WinNT.KEY_WOW64_32KEY, phkKey); if (rc != com.sun.jna.platform.win32.W32Errors.ERROR_SUCCESS) { throw new com.sun.jna.platform.win32.Win32Exception(rc); } try { return com.sun.jna.platform.win32.Advapi32Util.registryGetStringValue(phkKey.getValue(), value); } finally { rc = com.sun.jna.platform.win32.Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != com.sun.jna.platform.win32.W32Errors.ERROR_SUCCESS) { throw new com.sun.jna.platform.win32.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, 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); } } }
String name, long value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) {
/** * 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, 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); } } }
String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) {
/** * Get names of the registry key's sub-keys. * * @param root * Root key. * @param keyPath * Path to a registry key. * @param samDesiredExtra * Registry key security and access rights to be requested in addition to WinNT.KEY_READ. * (e.g WinNT.KEY_WOW64_32KEY or WinNT.KEY_WOW64_64KEY to force 32bit or 64bit registry access.) * @return Array of registry key names. */ public static String[] registryGetKeys(HKEY root, String keyPath, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | samDesiredExtra, 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); } } }
String name, int value, int samDesiredExtra) { WinReg.HKEYByReference phkKey = new WinReg.HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) {
/** * 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, 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 a registry DWORD value. * * @param root * Root key. * @param key * Registry key path. * @param value * Name of the value to retrieve. * @param samDesiredExtra * Registry key security and access rights to be requested in addition to WinNT.KEY_READ. * (e.g WinNT.KEY_WOW64_32KEY or WinNT.KEY_WOW64_64KEY to force 32bit or 64bit registry access.) * @return Integer value. */ public static int registryGetIntValue(WinReg.HKEY root, String key, String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { return registryGetIntValue(phkKey.getValue(), 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, 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); } } }