From 4e8dd7fe9558a356a002bec6472c076a49946ed4 Mon Sep 17 00:00:00 2001 From: Cameron Waldron Date: Sun, 26 Aug 2018 17:31:36 +0200 Subject: [PATCH] Expose 'samDesired' param to all Advapi32Util#registry* methods Added overloads to `c.s.j.platform.win32.Advapi32Util` methods which allow the caller to specify `samDesiredExtra` to request additional registry key security and access rights It also allows accessing alternative registry views by specifying: KEY_WOW64_64KEY or KEY_WOW64_32KEY Closes: #379 --- CHANGES.md | 1 + .../sun/jna/platform/win32/Advapi32Util.java | 419 +++++++++++++++++- .../jna/platform/win32/Advapi32UtilTest.java | 276 +++++++++++- 3 files changed, 664 insertions(+), 32 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 9f3b5782c5..5af6d5f5ae 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -29,6 +29,7 @@ Features * [#992](https://github.com/java-native-access/jna/pull/992): Improve stability of windows tests and add appveyor configuration for windows CI builds - [@matthiasblaesing](https://github.com/matthiasblaesing). * [#995](https://github.com/java-native-access/jna/pull/995): Added structures and methods to `c.s.j.platform.mac.SystemB` for Process, Network interface, Swapfile, Time, and Filesystem info - [@dbwiddis](https://github.com/dbwiddis). * [#997](https://github.com/java-native-access/jna/issues/997): Added `Sysinfo` structure and function to `c.s.j.platform.linux.LibC` - [@dbwiddis](https://github.com/dbwiddis). +* [#1001](https://github.com/java-native-access/jna/pull/1001): Added overloads to `c.s.j.platform.win32.Advapi32Util` methods which allow the caller to specify `samDesiredExtra` to request additional registry key security and access rights - [@camw](https://github.com/camw). Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java index 2c4eff7632..abe114ccb2 100755 --- a/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java @@ -567,8 +567,24 @@ public static Account[] getCurrentUserGroups() { * @return True if the key exists. */ public static boolean registryKeyExists(HKEY root, String key) { + return registryKeyExists(root, key, 0); + } + + /** + * 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, + int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); switch (rc) { case W32Errors.ERROR_SUCCESS: @@ -593,9 +609,28 @@ public static boolean registryKeyExists(HKEY root, String key) { * @return True if the value exists. */ public static boolean registryValueExists(HKEY root, String key, - String value) { + String value) { + return registryValueExists(root, key, value, 0); + } + + /** + * Checks whether a registry value exists. + * + * @param root + * HKEY_LOCAL_MACHINE, etc. + * @param key + * Registry key path. + * @param value + * Value name. + * @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 value exists. + */ + public static boolean registryValueExists(HKEY root, String key, + String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); - int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, + int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); try { switch (rc) { @@ -642,9 +677,28 @@ public static boolean registryValueExists(HKEY root, String key, * @return String value. */ public static String registryGetStringValue(HKEY root, String key, - String value) { + String value) { + return registryGetStringValue(root, key, value, 0); + } + + /** + * Get a registry REG_SZ value. + * + * @param root + * Root key. + * @param key + * Registry 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 String value. + */ + public static String registryGetStringValue(HKEY root, String key, + String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); - int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, + int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra , phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); @@ -715,8 +769,27 @@ public static String registryGetStringValue(HKEY hKey, String value) { */ public static String registryGetExpandableStringValue(HKEY root, String key, String value) { + return registryGetExpandableStringValue(root, key, value, 0); + } + + /** + * Get a registry REG_EXPAND_SZ value. + * + * @param root + * Root key. + * @param key + * Registry 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 String value. + */ + public static String registryGetExpandableStringValue(HKEY root, + String key, String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); - int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, + int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); @@ -785,8 +858,27 @@ public static String registryGetExpandableStringValue(HKEY hKey, String value) { */ public static String[] registryGetStringArray(HKEY root, String key, String value) { + return registryGetStringArray(root, key, value, 0); + } + + /** + * Get a registry REG_MULTI_SZ value. + * + * @param root + * Root key. + * @param key + * Registry 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 String value. + */ + public static String[] registryGetStringArray(HKEY root, String key, + String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); - int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, + int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); @@ -872,8 +964,27 @@ public static String[] registryGetStringArray(HKEY hKey, String value) { */ public static byte[] registryGetBinaryValue(HKEY root, String key, String value) { + return registryGetBinaryValue(root, key, value, 0); + } + + /** + * Get a registry REG_BINARY value. + * + * @param root + * Root key. + * @param key + * Registry 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 String value. + */ + public static byte[] registryGetBinaryValue(HKEY root, String key, + String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); - int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, + int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); @@ -932,8 +1043,26 @@ public static byte[] registryGetBinaryValue(HKEY hKey, String value) { * @return Integer value. */ public static int registryGetIntValue(HKEY root, String key, String value) { + return registryGetIntValue(root, key, value, 0); + } + + /** + * 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, + int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); @@ -992,8 +1121,26 @@ public static int registryGetIntValue(HKEY hKey, String value) { * @return Integer value. */ public static long registryGetLongValue(HKEY root, String key, String value) { + return registryGetLongValue(root, key, value, 0); + } + + /** + * Get a registry QWORD 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 long registryGetLongValue(HKEY root, String key, String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); - int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, + int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); @@ -1112,10 +1259,26 @@ public static Object registryGetValue(HKEY hkKey, String subKey, * @return True if the key was created, false otherwise. */ public static boolean registryCreateKey(HKEY hKey, String keyName) { + return registryCreateKey(hKey, keyName, 0); + } + + /** + * Create a registry key. + * + * @param hKey + * Parent key. + * @param keyName + * Key name. + * @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 was created, false otherwise. + */ + public static boolean registryCreateKey(WinReg.HKEY hKey, String keyName, int samDesiredExtra) { 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, + WinNT.REG_OPTION_NON_VOLATILE, WinNT.KEY_READ | samDesiredExtra, null, phkResult, lpdwDisposition); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); @@ -1140,9 +1303,28 @@ public static boolean registryCreateKey(HKEY hKey, String keyName) { */ public static boolean registryCreateKey(HKEY root, String parentPath, String keyName) { + return registryCreateKey(root, parentPath, keyName, 0); + } + + /** + * Create a registry key. + * + * @param root + * Root key. + * @param parentPath + * Path to an existing registry key. + * @param keyName + * Key name. + * @param samDesiredExtra + * Registry key security and access rights to be requested in addition to WinNT.KEY_CREATE_SUB_KEY. + * (e.g WinNT.KEY_WOW64_32KEY or WinNT.KEY_WOW64_64KEY to force 32bit or 64bit registry access.) + * @return True if the key was created, false otherwise. + */ + public static boolean registryCreateKey(HKEY root, String parentPath, + String keyName, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, parentPath, 0, - WinNT.KEY_CREATE_SUB_KEY, phkKey); + WinNT.KEY_CREATE_SUB_KEY | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1193,9 +1375,29 @@ public static void registrySetIntValue(HKEY hKey, String name, int value) { */ public static void registrySetIntValue(HKEY root, String keyPath, String name, int value) { - HKEYByReference phkKey = new HKEYByReference(); + registrySetIntValue(root, keyPath, name, value, 0); + } + + /** + * 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. + * @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 registrySetIntValue(WinReg.HKEY root, String keyPath, + 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, phkKey); + WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1250,9 +1452,29 @@ public static void registrySetLongValue(HKEY hKey, String name, long value) { */ public static void registrySetLongValue(HKEY root, String keyPath, String name, long value) { + registrySetLongValue(root, keyPath, name, value, 0); + } + + /** + * Set a long 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. + * @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 registrySetLongValue(HKEY root, String keyPath, + String name, long value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, - WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey); + WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1310,9 +1532,29 @@ public static void registrySetStringValue(HKEY hKey, String name, */ public static void registrySetStringValue(HKEY root, String keyPath, String name, String value) { + registrySetStringValue(root, keyPath, name, value, 0); + } + + /** + * 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. + * @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 registrySetStringValue(HKEY root, String keyPath, + String name, String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, - WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey); + WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1367,9 +1609,29 @@ public static void registrySetExpandableStringValue(HKEY hKey, String name, */ public static void registrySetExpandableStringValue(HKEY root, String keyPath, String name, String value) { + registrySetExpandableStringValue(root, keyPath, name, value, 0); + } + + /** + * 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. + * @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 registrySetExpandableStringValue(HKEY root, + String keyPath, String name, String value, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, - WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey); + WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1440,9 +1702,29 @@ public static void registrySetStringArray(HKEY hKey, String name, */ public static void registrySetStringArray(HKEY root, String keyPath, String name, String[] arr) { + registrySetStringArray(root, keyPath, name, arr, 0); + } + + /** + * 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. + * @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 registrySetStringArray(HKEY root, String keyPath, + String name, String[] arr, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, - WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey); + WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1489,9 +1771,29 @@ public static void registrySetBinaryValue(HKEY hKey, String name, */ public static void registrySetBinaryValue(HKEY root, String keyPath, String name, byte[] data) { + registrySetBinaryValue(root, keyPath, name, data, 0); + } + + /** + * 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. + * @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 registrySetBinaryValue(HKEY root, String keyPath, + String name, byte[] data, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, - WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey); + WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1531,10 +1833,28 @@ public static void registryDeleteKey(HKEY hKey, String keyName) { * Name of the key to delete. */ public static void registryDeleteKey(HKEY root, String keyPath, - String keyName) { + String keyName) { + registryDeleteKey(root, keyPath, keyName, 0); + } + + /** + * Delete a registry key. + * + * @param root + * Root key. + * @param keyPath + * Path to an existing registry key. + * @param keyName + * Name of the key 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 registryDeleteKey(HKEY root, String keyPath, + String keyName, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, - WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey); + WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1575,9 +1895,27 @@ public static void registryDeleteValue(HKEY hKey, String valueName) { */ public static void registryDeleteValue(HKEY root, String keyPath, String valueName) { + registryDeleteValue(root, keyPath, valueName, 0); + } + + /** + * 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, phkKey); + WinNT.KEY_READ | WinNT.KEY_WRITE | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1632,9 +1970,25 @@ public static String[] registryGetKeys(HKEY hKey) { * @return Array of registry key names. */ public static String[] registryGetKeys(HKEY root, String keyPath) { + return registryGetKeys(root, keyPath, 0); + } + + /** + * 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, phkKey); + WinNT.KEY_READ | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } @@ -1819,9 +2173,26 @@ public static TreeMap registryGetValues(HKEY hKey) { */ public static TreeMap registryGetValues(HKEY root, String keyPath) { + return registryGetValues(root, keyPath, 0); + } + + /** + * 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 registryGetValues(HKEY root, + String keyPath, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, - WinNT.KEY_READ, phkKey); + WinNT.KEY_READ | samDesiredExtra, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } diff --git a/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java b/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java index 6a60cdeb4c..8a3a12820a 100755 --- a/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java @@ -221,6 +221,23 @@ public void testRegistryKeyExists() { "KeyDoesNotExist\\SubKeyDoesNotExist")); } + public void testRegistryKeyExistsSamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + assertTrue(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_64KEY)); + assertFalse(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + assertFalse(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_64KEY)); + assertTrue(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistryValueExists() { assertFalse(Advapi32Util.registryValueExists(WinReg.HKEY_LOCAL_MACHINE, "Software\\Microsoft", "")); @@ -230,6 +247,23 @@ public void testRegistryValueExists() { "SYSTEM\\CurrentControlSet\\Control", "SystemBootDevice")); } + public void testRegistryValueExistsSamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", 64, WinNT.KEY_WOW64_64KEY); + Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", 64, WinNT.KEY_WOW64_32KEY); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_64KEY)); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryDeleteValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_32KEY); + assertFalse(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_64KEY)); + assertFalse(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistryCreateDeleteKey() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); assertTrue(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\JNA")); @@ -237,6 +271,24 @@ public void testRegistryCreateDeleteKey() { assertFalse(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\JNA")); } + public void testRegistryCreateDeleteKeySamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + assertTrue(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_64KEY)); + assertFalse(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + assertFalse(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_64KEY)); + assertFalse(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_32KEY)); + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + assertFalse(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_64KEY)); + assertTrue(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + assertFalse(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_64KEY)); + assertFalse(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_32KEY)); + } + public void testRegistryCreateKeyDisposition() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); @@ -246,6 +298,23 @@ public void testRegistryCreateKeyDisposition() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistryCreateKeyDispositionSamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + assertTrue(Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY)); + assertTrue(Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY)); + assertFalse(Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY)); + assertFalse(Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY)); + assertTrue(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_64KEY)); + assertTrue(Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistryDeleteValue() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "IntValue", 42); @@ -255,6 +324,23 @@ public void testRegistryDeleteValue() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistryDeleteValueSamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", 64, WinNT.KEY_WOW64_64KEY); + Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", 32, WinNT.KEY_WOW64_32KEY); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_64KEY)); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_64KEY); + assertFalse(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_64KEY)); + Advapi32Util.registryDeleteValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_32KEY); + assertFalse(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistrySetGetIntValue() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "IntValue", 42); @@ -264,6 +350,24 @@ public void testRegistrySetGetIntValue() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistrySetGetIntValueSamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + + Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", 64, WinNT.KEY_WOW64_64KEY); + Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", 32, WinNT.KEY_WOW64_32KEY); + assertEquals(64, Advapi32Util.registryGetIntValue(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_64KEY)); + assertEquals(32, Advapi32Util.registryGetIntValue(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_32KEY)); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_64KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "IntValue", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistrySetGetLongValue() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); Advapi32Util.registrySetLongValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "LongValue", 1234L); @@ -273,6 +377,23 @@ public void testRegistrySetGetLongValue() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistrySetGetLongValueSamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registrySetLongValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "LongValue", 64L, WinNT.KEY_WOW64_64KEY); + Advapi32Util.registrySetLongValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "LongValue", 32L, WinNT.KEY_WOW64_32KEY); + assertEquals(64L, Advapi32Util.registryGetLongValue(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", "LongValue", WinNT.KEY_WOW64_64KEY)); + assertEquals(32L, Advapi32Util.registryGetLongValue(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", "LongValue", WinNT.KEY_WOW64_32KEY)); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "LongValue", WinNT.KEY_WOW64_64KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "LongValue", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistrySetGetStringValue() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "StringValue", "Hello World"); @@ -282,6 +403,23 @@ public void testRegistrySetGetStringValue() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistrySetGetStringValueSamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "StringValue", "Hello World64", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "StringValue", "Hello World32", WinNT.KEY_WOW64_32KEY); + assertEquals("Hello World64", Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", "StringValue", WinNT.KEY_WOW64_64KEY)); + assertEquals("Hello World32", Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", "StringValue", WinNT.KEY_WOW64_32KEY)); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "StringValue", WinNT.KEY_WOW64_64KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "StringValue", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistrySetGetExpandableStringValue() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); Advapi32Util.registrySetExpandableStringValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "StringValue", "Temp is %TEMP%"); @@ -291,16 +429,30 @@ public void testRegistrySetGetExpandableStringValue() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistrySetGetExpandableStringValueSamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registrySetExpandableStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "StringValue", "64 Temp is %TEMP%", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registrySetExpandableStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "StringValue", "32 Temp is %TEMP%", WinNT.KEY_WOW64_32KEY); + assertEquals("64 Temp is %TEMP%", Advapi32Util.registryGetExpandableStringValue(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", "StringValue", WinNT.KEY_WOW64_64KEY)); + assertEquals("32 Temp is %TEMP%", Advapi32Util.registryGetExpandableStringValue(WinReg.HKEY_CURRENT_USER, + "Software\\Classes\\CLSID\\JNA", "StringValue", WinNT.KEY_WOW64_32KEY)); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "StringValue", WinNT.KEY_WOW64_64KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "StringValue", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistrySetGetStringArray() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); String[] dataWritten = { "Hello", "World" }; Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "MultiStringValue", dataWritten); assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "MultiStringValue")); String[] dataRead = Advapi32Util.registryGetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "MultiStringValue"); - assertEquals(dataWritten.length, dataRead.length); - for(int i = 0; i < dataRead.length; i++) { - assertEquals(dataWritten[i], dataRead[i]); - } + this.assertStringArraysEqual(dataWritten, dataRead); dataWritten = new String[0]; Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "EmptyMultiString", dataWritten); dataRead = Advapi32Util.registryGetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "EmptyMultiString"); @@ -308,19 +460,76 @@ public void testRegistrySetGetStringArray() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistrySetGetStringArraySamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + String[] dataWritten64 = { "Hello", "World", "64" }; + String[] dataWritten32 = { "Hello", "World", "32" }; + Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "MultiStringValue", dataWritten64, WinNT.KEY_WOW64_64KEY); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "MultiStringValue", WinNT.KEY_WOW64_64KEY)); + Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "MultiStringValue", dataWritten32, WinNT.KEY_WOW64_32KEY); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "MultiStringValue", WinNT.KEY_WOW64_32KEY)); + String[] dataRead64 = Advapi32Util.registryGetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "MultiStringValue", WinNT.KEY_WOW64_64KEY); + String[] dataRead32 = Advapi32Util.registryGetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "MultiStringValue", WinNT.KEY_WOW64_32KEY); + this.assertStringArraysEqual(dataWritten64, dataRead64); + this.assertStringArraysEqual(dataWritten32, dataRead32); + dataWritten64 = new String[0]; + dataWritten32 = new String[0]; + Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "EmptyMultiString", dataWritten64, WinNT.KEY_WOW64_64KEY); + Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "EmptyMultiString", dataWritten32, WinNT.KEY_WOW64_32KEY); + dataRead64 = Advapi32Util.registryGetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "EmptyMultiString", WinNT.KEY_WOW64_64KEY); + dataRead32 = Advapi32Util.registryGetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "EmptyMultiString", WinNT.KEY_WOW64_32KEY); + assertEquals(0, dataRead32.length); + assertEquals(0, dataRead64.length); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + + private void assertStringArraysEqual(String[] dataWritten, String[] dataRead) { + assertEquals(dataWritten.length, dataRead.length); + for(int i = 0; i < dataRead.length; i++) { + assertEquals(dataWritten[i], dataRead[i]); + } + } + public void testRegistrySetGetBinaryValue() { byte[] data = { 0x00, 0x01, 0x02 }; Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); Advapi32Util.registrySetBinaryValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "BinaryValue", data); byte[] read = Advapi32Util.registryGetBinaryValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "BinaryValue"); - assertEquals(data.length, read.length); - for(int i = 0; i < data.length; i++) { - assertEquals(data[i], read[i]); - } + assertBinaryArraysEqual(read, data); assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "BinaryValue")); Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistrySetGetBinaryValueSamExtra() { + if (!is64bitWindows()) return; + + byte[] data32 = { 0x00, 0x01, 0x02, 0x32 }; + byte[] data64 = { 0x00, 0x01, 0x02, 0x64 }; + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registrySetBinaryValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "BinaryValue", data64, WinNT.KEY_WOW64_64KEY); + Advapi32Util.registrySetBinaryValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "BinaryValue", data32, WinNT.KEY_WOW64_32KEY); + byte[] read64 = Advapi32Util.registryGetBinaryValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "BinaryValue", WinNT.KEY_WOW64_64KEY); + byte[] read32 = Advapi32Util.registryGetBinaryValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "BinaryValue", WinNT.KEY_WOW64_32KEY); + assertBinaryArraysEqual(read64, data64); + assertBinaryArraysEqual(read32, data32); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "BinaryValue", WinNT.KEY_WOW64_64KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + assertTrue(Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "BinaryValue", WinNT.KEY_WOW64_32KEY)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + + private void assertBinaryArraysEqual(byte[] dataWritten, byte[] dataRead) { + assertEquals(dataWritten.length, dataRead.length); + for(int i = 0; i < dataRead.length; i++) { + assertEquals(dataWritten[i], dataRead[i]); + } + } + public void testRegistryGetKeys() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "Key1"); @@ -334,6 +543,31 @@ public void testRegistryGetKeys() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistryGetKeysSamExtra() { + if (!is64bitWindows()) return; + + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Key1", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Key1", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Key2-64", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Key2-32", WinNT.KEY_WOW64_32KEY); + String[] subKeys64 = Advapi32Util.registryGetKeys(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_64KEY); + String[] subKeys32 = Advapi32Util.registryGetKeys(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_32KEY); + assertEquals(2, subKeys64.length); + assertEquals(subKeys64[0], "Key1"); + assertEquals(subKeys64[1], "Key2-64"); + assertEquals(2, subKeys32.length); + assertEquals(subKeys32[0], "Key1"); + assertEquals(subKeys32[1], "Key2-32"); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Key1", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Key1", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Key2-64", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Key2-32", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistryGetCloseKey() { Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "Key1"); @@ -382,6 +616,24 @@ public void testRegistryGetValues() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistryGetValuesSamExtra() { + if (!is64bitWindows()) return; + + String uu = new String("A" + "\\u00ea" + "\\u00f1" + "\\u00fc" + "C"); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Number" + uu, 64, WinNT.KEY_WOW64_64KEY); + Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", "Number" + uu, 32, WinNT.KEY_WOW64_32KEY); + TreeMap values64 = Advapi32Util.registryGetValues(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_64KEY); + TreeMap values32 = Advapi32Util.registryGetValues(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID\\JNA", WinNT.KEY_WOW64_32KEY); + assertEquals(1, values64.keySet().size()); + assertEquals(64, values64.get("Number" + uu)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_64KEY); + assertEquals(1, values32.keySet().size()); + assertEquals(32, values32.get("Number" + uu)); + Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software\\Classes\\CLSID", "JNA", WinNT.KEY_WOW64_32KEY); + } + public void testRegistryGetEmptyValues() { HKEY root = WinReg.HKEY_CURRENT_USER; String keyPath = "Software\\JNA"; @@ -641,4 +893,12 @@ private File createTempFile() throws Exception{ fileWriter.close(); return file; } + + private boolean is64bitWindows() { + String arch = System.getenv("PROCESSOR_ARCHITECTURE"); + String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432"); + + return arch != null && arch.endsWith("64") + || wow64Arch != null && wow64Arch.endsWith("64"); + } }