Advapi32Util registryKeyExists() and registryValueExists() throws exception on access denied

123 views
Skip to first unread message

Daniel B. Widdis

unread,
Feb 5, 2019, 3:19:51 PM2/5/19
to Java Native Access
This looks like a similar situation to https://github.com/java-native-access/jna/issues/1036

Trying to iterate audio device drivers using this code:

private static final String REGISTRY_SOUNDCARDS = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e96c-e325-11ce-bfc1-08002be10318}\\";

        String[] keys = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE, REGISTRY_SOUNDCARDS);
        for (String key : keys) {
            String fullKey = REGISTRY_SOUNDCARDS + key;
            if (Advapi32Util.registryValueExists(WinReg.HKEY_LOCAL_MACHINE, fullKey, "MatchingDeviceId")) {
            String id = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, REGISTRY_SOUNDCARDS + key,
                    "MatchingDeviceId");
                System.out.println("Key: " + key + ", id:" + id);
            }
        }


A Win32 Exception "Access is Denied" is thrown on the registryValueExists() check.

Exception in thread "main" com.sun.jna.platform.win32.Win32Exception: Access is denied.
    at com.sun.jna.platform.win32.Advapi32Util.registryKeyExists(Advapi32Util.java:595)
    at com.sun.jna.platform.win32.Advapi32Util.registryKeyExists(Advapi32Util.java:569)


Unlike the other issue, checking for existence of they key first doesn't work, and throws the same exception:

Advapi32Util.registryKeyExists(WinReg.HKEY_LOCAL_MACHINE, fullKey)

The offending key is:
HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e96c-e325-11ce-bfc1-08002be10318}\Properties

I don't see any way to check for access using Advapi32Util, so my only fallback is to catch this exception.  Should this just result in a "false" like the earlier issue?
--
Dan Widdis

Amritanshu

unread,
Feb 5, 2019, 3:55:21 PM2/5/19
to jna-...@googlegroups.com
>  Should this just result in a "false" like the earlier issue?  

No, because the caller of the function doesn't have sufficient privileges to tell if the key exists or not so it shouldn't return false. I think throwing the exception is correct and expected.   

Daniel D.

unread,
Feb 6, 2019, 7:48:47 AM2/6/19
to jna-...@googlegroups.com
This is indeed by design, and different than #1036 which was a bug in which a key that didn't exist resulted in an invalid handle exception from some cleanup.

On Tue, Feb 5, 2019 at 3:55 PM Amritanshu <amrit...@gmail.com> wrote:
>  Should this just result in a "false" like the earlier issue?  

No, because the caller of the function doesn't have sufficient privileges to tell if the key exists or not so it shouldn't return false. I think throwing the exception is correct and expected.   

--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Reply all
Reply to author
Forward
0 new messages