1

I am new to registrykeyexists(root,key) method of Advapi32util which checks whether registry key "key"(key that is passed to registrykeyexists) is present in windows registry under the root.

Root that i am passing is "HKEY_LOCAL_MACHINE". Key is something that looks like "SOFTWARE\ABC\ABC DB"

And I see that this key is present in window's registry. ( by running "regedit" via cmd)

Below is the code snippet which does this work.

public static String getRegistryData(WinReg.HKEY root, String key, String value) {

        System.out.println("Registry key exists status:" + Advapi32Util.registryKeyExists(root, key));

        if (Advapi32Util.registryKeyExists(root, key)) {
            String retVal = Advapi32Util.registryGetStringValue(root, key, value);
            return retVal;
        } else {
            return null;
        }
    }

So,here when I debug, Advapi32Util.registryKeyExists(root, key) returns false.

Can anyone help me on this?? I would be grateful !!

Thanks In Advance.

Radhika KS
  • 11
  • 3

2 Answers2

2

I know it is some months old, but I had the same question as you.. And I hope this answer can help more people.

This can be a bit confusing, but the namings for these items work like this: enter image description here

The Keys would be like the Path, and the Values would be each value inside a Key. After understanding it, it should be easier to do the coding. The method Advapi32Util.registryKeyExists looks for Keys (in other words, paths), so it won't help you.

If you want to verify if the value Counter exists in the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009 for example, you should use this method:

boolean keyExists = Advapi32Util.registryValueExists(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009", "Counter");

In your case, your method would be:

public static String getRegistryData(WinReg.HKEY root, String key, String value) {

    System.out.println("Registry key exists status:" + Advapi32Util.registryValueExists(root, key, value));

    if (Advapi32Util.registryValueExists(root, key, value)) {
        String retVal = Advapi32Util.registryGetStringValue(root, key, value);
        return retVal;
    } else {
        return null;
    }
}

You can take a look in their GitHub project if you are curious in how they implemented it: Advapi32Util - JNA GitHub Project

RKrum
  • 410
  • 5
  • 15
0

Late answer, but I guess it applies to what you witnessed there. Since version 5, JNA added a parameter into their registry navigation methods that allow to switch from 32 to 64 registry "realms"

Look at that excellent answer from @CamW https://stackoverflow.com/a/57051855/2143734

Zzirconium
  • 431
  • 1
  • 10
  • 32