Skip to content

Commit

Permalink
Add RegLoadAppKeyW support
Browse files Browse the repository at this point in the history
  • Loading branch information
mfilippov committed Aug 23, 2021
1 parent ec81f53 commit b741bbb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -15,6 +15,7 @@ Features
* [#1354](https://github.com/java-native-access/jna/pull/1352): Add `GetParent` to `c.s.j.p.win32.User32` - [@kahgoh](https://github.com/kahgoh).
* [#1360](https://github.com/java-native-access/jna/issues/1360): Add `CommandLineToArgvW` to `c.s.j.p.win32.Shell32` and corresponding util in `c.s.j.p.win32.Shell32Util` - [@dbwiddis](https://github.com/dbwiddis).
* [#1363](https://github.com/java-native-access/jna/issues/1363): Update `NUMA_NODE_RELATIONSHIP` in `c.s.j.p.win32.WinNT` to new version of the structure and improve support for future values of `c.s.j.p.win32.WinNT.LOGICAL_PROCESSOR_RELATIONSHIP` - [@dbwiddis](https://github.com/dbwiddis).
* [#1377](https://github.com/java-native-access/jna/pull/1377): Add `RegLoadAppKey` to `com.sun.jna.platform.win32.Advapi32` - [@mfilippov](https://github.com/mfilippov).

Bug Fixes
---------
Expand Down
23 changes: 23 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Advapi32.java
Expand Up @@ -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<br>
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java
Expand Up @@ -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.RegLoadAppKeyW(fileName, phkKey, samDesired, dwOptions, 0);
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}

return phkKey;
}

/**
* Close the registry key
*
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit b741bbb

Please sign in to comment.