Skip to content

Commit f99b613

Browse files
cxcorpmatthiasblaesing
authored andcommitted
Add RegConnectRegistry to Advapi32 mappings (#935)
1 parent d1bb591 commit f99b613

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Features
1414
* [#913](https://github.com/java-native-access/jna/issues/913): Add `@ComInterface` annotation to `com.sun.jna.platform.win32.COM.util.IConnectionPoint` to make it possible to retrieve it via `IUnknown#queryInterface` - [@matthiasblaesing](https://github.com/matthiasblaesing).
1515
* [#797](https://github.com/java-native-access/jna/issues/797): Binding `Advapi32#EnumDependendServices`, `Advapi32#EnumServicesStatusEx` and `Advapi32#QueryServiceStatus`. `W32Service#stopService` was modified to be more resilent when stopping service - [@matthiasblaesing](https://github.com/matthiasblaesing).
1616
* Bind `com.sun.jna.platform.win32.Kernel32.ExpandEnvironmentStrings` and add helper method for it as `Kernel32Util#expandEnvironmentStrings` - [@matthiasblaesing](https://github.com/matthiasblaesing).
17+
* [#935](https://github.com/java-native-access/jna/pull/935): Add RegConnectRegistry to Advapi32 mappings. - [@cxcorp](https://github.com/cxcorp).
1718

1819
Bug Fixes
1920
---------

contrib/platform/src/com/sun/jna/platform/win32/Advapi32.java

+40
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,46 @@ boolean GetTokenInformation(HANDLE tokenHandle,
854854
int RegOpenKeyEx(HKEY hKey, String lpSubKey, int ulOptions,
855855
int samDesired, HKEYByReference phkResult);
856856

857+
/**
858+
* Establishes a connection to a predefined registry key on another
859+
* computer.
860+
* @param lpMachineName
861+
* The name of the remote computer. The string has
862+
* the following form:<br />
863+
* <pre><code>\\computername</code></pre>
864+
* The caller must have access to the remote computer or the
865+
* function fails.<br />
866+
* If this parameter is <c>null</c>, the local computer name
867+
* is used.
868+
* @param hKey
869+
* A predefined registry handle. This parameter can be one of
870+
* the following predefined keys on the remote computer.<br />
871+
* <ul>
872+
* <li>{@link WinReg#HKEY_LOCAL_MACHINE}</li>
873+
* <li>{@link WinReg#HKEY_PERFORMANCE_DATA}</li>
874+
* <li>{@link WinReg#HKEY_USERS}</li>
875+
* </ul>
876+
* @param phkResult
877+
* A pointer to a variable that receives a key handle
878+
* identifying the predefined handle on the remote computer.
879+
* @return If the function succeeds, the return value is
880+
* {@link WinError#ERROR_SUCCESS}.<br />
881+
* If the remote computer cannot be found or if its Remote Registry
882+
* service is disabled, the function fails and returns
883+
* {@link WinError#ERROR_BAD_NETPATH}.<br />
884+
* If attempting to use a registry handle other than one of the
885+
* three predefined handles, the function fails and returns
886+
* {@link WinError#ERROR_INVALID_HANDLE}.<br />
887+
* If access to the registry is denied, the function fails and
888+
* returns {@link WinError#ERROR_ACCESS_DENIED}. <br />
889+
* If the function fails for some other reason, you can use the
890+
* {@link Native#getLastError} method to get a generic description
891+
* of the error.
892+
* @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724840.aspx">RegConnectRegistry function (Windows)</a>
893+
* @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724836.aspx">Predefined Keys (Windows)</a>
894+
*/
895+
int RegConnectRegistry(String lpMachineName, HKEY hKey, HKEYByReference phkResult);
896+
857897
/**
858898
* The RegQueryValueEx function retrieves the type and data for a specified
859899
* value name associated with an open registry key.

contrib/platform/test/com/sun/jna/platform/win32/Advapi32Test.java

+20
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,26 @@ public void testRegOpenKeyEx() {
493493
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
494494
}
495495

496+
public void testRegConnectRegistry() {
497+
HKEYByReference phkResult = new HKEYByReference();
498+
int connectResult = Advapi32.INSTANCE.RegConnectRegistry(
499+
"\\\\localhost", WinReg.HKEY_LOCAL_MACHINE, phkResult);
500+
501+
if (connectResult == W32Errors.ERROR_BAD_NETPATH) {
502+
System.err.println("Failed to connect to registry with RegConnectRegistry, remote registry service is probably disabled");
503+
return;
504+
}
505+
506+
if (connectResult == W32Errors.ERROR_ACCESS_DENIED) {
507+
System.err.println("Access was denied when connecting to registry with RegConnectRegistry");
508+
return;
509+
}
510+
511+
assertEquals(W32Errors.ERROR_SUCCESS, connectResult);
512+
assertTrue(WinBase.INVALID_HANDLE_VALUE != phkResult.getValue());
513+
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phkResult.getValue()));
514+
}
515+
496516
public void testRegQueryValueEx() {
497517
HKEYByReference phKey = new HKEYByReference();
498518
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(

0 commit comments

Comments
 (0)