From 0d12f2c89227b25665b995a667b03e1b63051014 Mon Sep 17 00:00:00 2001 From: Mikhail Filippov Date: Thu, 19 Aug 2021 22:36:31 +0300 Subject: [PATCH] Add RegLoadAppKeyW support --- CHANGES.md | 2 +- .../com/sun/jna/platform/win32/Advapi32.java | 23 +++++++++++++++++++ .../sun/jna/platform/win32/Advapi32Util.java | 22 ++++++++++++++++++ .../jna/platform/win32/Advapi32UtilTest.java | 12 ++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index d662b74b9c..dca66650de 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,7 +7,7 @@ Next Release (5.10.0) Features -------- - +* [#1377](https://github.com/java-native-access/jna/pull/1377): Add `RegLoadAppKey` to `c.s.j.p.win32.Advapi32` and `registryLoadAppKey` to `c.s.j.p.win32.Advapi32Util` - [@mfilippov](https://github.com/mfilippov). Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Advapi32.java b/contrib/platform/src/com/sun/jna/platform/win32/Advapi32.java index e9c1b23f43..07e5992b7a 100755 --- a/contrib/platform/src/com/sun/jna/platform/win32/Advapi32.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Advapi32.java @@ -82,6 +82,7 @@ public interface Advapi32 extends StdCallLibrary { int RRF_RT_REG_NONE = 0x00000001; int RRF_RT_REG_QWORD = 0x00000040; int RRF_RT_REG_SZ = 0x00000002; + int REG_PROCESS_APPKEY = 0x00000001; /** * LOGON_WITH_PROFILE: 0x00000001
@@ -854,6 +855,28 @@ boolean GetTokenInformation(HANDLE tokenHandle, int RegOpenKeyEx(HKEY hKey, String lpSubKey, int ulOptions, int samDesired, HKEYByReference phkResult); + /** + * The RegLoadAppKey function loads the specified registry hive + * as an application hive. + * + * @param lpFile + * The name of the hive file. + * If the file does not exist, an empty hive file is created + * with the specified name. + * @param phkResult + * Pointer to the handle for the root key of the loaded hive. + * @param samDesired + * Access mask that specifies the desired access rights to the + * @param dwOptions + * If this parameter is REG_PROCESS_APPKEY, + * the hive cannot be loaded again while it is loaded by the caller. + * This prevents access to this registry hive by another caller. + * @param Reserved + * Reserved; must be zero. + */ + int RegLoadAppKey(String lpFile, HKEYByReference phkResult, + int samDesired, int dwOptions, int Reserved); + /** * Establishes a connection to a predefined registry key on another * computer. 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 73e773bcbc..304fe21a3c 100755 --- a/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java @@ -2076,6 +2076,28 @@ public static HKEYByReference registryGetKey(HKEY root, String keyPath, return phkKey; } + /** + * Loads the specified registry hive as an application hive. + * + * @param fileName + * Path to the file + * @param samDesired + * Access mask that specifies the desired access rights to the + * @param dwOptions + * If this parameter is REG_PROCESS_APPKEY, + * the hive cannot be loaded again while it is loaded by the caller. + * This prevents access to this registry hive by another caller. + */ + public static HKEYByReference registryLoadAppKey(String fileName, int samDesired, int dwOptions) { + HKEYByReference phkKey = new HKEYByReference(); + int rc = Advapi32.INSTANCE.RegLoadAppKey(fileName, phkKey, samDesired, dwOptions, 0); + if (rc != W32Errors.ERROR_SUCCESS) { + throw new Win32Exception(rc); + } + + return phkKey; + } + /** * Close the registry key * 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 542e314c29..2297d2c380 100755 --- a/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java @@ -29,6 +29,7 @@ import java.io.File; import java.io.FileWriter; +import java.nio.file.Files; import java.util.Map; import java.util.TreeMap; @@ -605,6 +606,17 @@ public void testRegistryGetCloseKey() { Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA"); } + public void testRegistryLoadAppKey() throws Exception { + File tempDir = Files.createTempDirectory("testRegistryLoadAppKey").toFile(); + File registryFile = new File(tempDir, "privateregistry.bin"); + HKEYByReference phkKey = Advapi32Util.registryLoadAppKey(registryFile.getAbsolutePath(), WinNT.KEY_ALL_ACCESS, 0); + Advapi32Util.registryCreateKey(phkKey.getValue(), "Test"); + Advapi32Util.registryDeleteKey(phkKey.getValue(), "Test"); + Advapi32Util.registryCloseKey(phkKey.getValue()); + registryFile.delete(); + tempDir.delete(); + } + public void testRegistryGetValues() { String uu = "A\\u00ea\\u00f1\\u00fcC"; Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA");