1

Im currently working on a java program and I need to read/write to the registry. i've looked at several API's to do this and I found ini4j (ini4j Project Page). I also need to edit ini files so I like this solution because it does both. I'm curious if anybody has tried ini4j in this type of scenario?

skaffman
  • 398,947
  • 96
  • 818
  • 769
GEverding
  • 3,131
  • 2
  • 21
  • 23

2 Answers2

1

I found a better solution for reading/writing to the registry without the need for ini4j or passing arguments to the command line. I use JNA quite a lot in my program so I figured that it would be easier to use native library calls instead of including an additional library to do this for me. Here is an example from my project were I search through the registry looking for a specific key. The specific key is also dependent on whether or not the OS is x64 or x86.

   public static String GetUninstallerPath() {
        try {
            //if (logger.IsInfoEnabled) logger.Info("GetUninstallerPath - begin");

            String uninstallerPath = null;

            try {
                String vncDisplayName = "UltraVNC";
                String subkey32 = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
                String subkey64 = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";

                boolean is64Bit = Platform.is64Bit();
                String[] key;
                if (is64Bit) {
                    key = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE,
                            subkey64);
                } else {
                    key = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE,
                            subkey32);
                }

                if (key != null) {
                    for (String nextSubkeyName : key) {

                        TreeMap<String, Object> subKey = Advapi32Util.registryGetValues(
                                WinReg.HKEY_LOCAL_MACHINE,
                                subkey64 + "\\" + nextSubkeyName);
                        Object value = subKey.get("DisplayName");
                        Object path = null;
                        if (value != null) {
                            if (value.toString().startsWith(vncDisplayName)) {
                                path = subKey.get("UninstallString");
                                if (path != null) {
                                    uninstallerPath = path.toString().trim();
                                }
                            }
                        }

                    }

                }

            }
            catch (Exception ex) {
                System.err.println(ex.getMessage());

            }

            return uninstallerPath;
         }
    }  

I use objects to initially store the key values because I kept getting NullPointerExceptions. Feel free to provide another solution.

GEverding
  • 3,131
  • 2
  • 21
  • 23
0

Unfortunately your test for 64 bits using Platform.is64Bit() doesn't do what you think it does...

It tells you if your JVM is 32 bits or 64 bits, not if your Windows is 32 bits or 64 bits...

The only reason why your code seems to work as intended is because the Windows registry redirector takes care of the "magic" involved (accessing the right registry key) for you...

When your code runs on a 32 bits JVM on a 64 bits Windows Platform.is64Bit() returns false and you are using subkey32 (ie "Software\Microsoft\Windows\CurrentVersion\Uninstall").

I unfortunately did the same mistake as you and released a program with the same erroneous test after readings threads such as yours which is why I am now posting this even though this thread is several years old.

Puzzled
  • 29
  • 8