Reading DWord from the Windows Registry

How can I read these DWORD values ​​from the Windows registry using java.util.prefs.Preferences. I can read data of type REG_SZ, but when reading type REG_DWORD it returns null.

Preferences userRoot = Preferences.userRoot();
Class clz = userRoot.getClass();
openKey = clz.getDeclaredMethod("openKey", byte[].class, int.class, int.class);
openKey.setAccessible(true);
final Method closeKey = clz.getDeclaredMethod("closeKey", int.class);
closeKey.setAccessible(true);

byte[] valb = null;
String key = null;
Integer handle = -1;
final Method winRegQueryValue = clz.getDeclaredMethod("WindowsRegQueryValueEx", int.class, byte[].class);
winRegQueryValue.setAccessible(true);

key = "Software\\SimonTatham\\PuTTY\\Sessions\\myMachine";
handle = (Integer) openKey.invoke(userRoot, toCstr(key), KEY_READ, KEY_READ);

//this line returns byte[] correctly
valb = (byte[]) winRegQueryValue.invoke(userRoot, handle.intValue(), toCstr("HostName"));

//but this line returns null instead of byte[] even though there is a value of type REG_DWORD
valb = (byte[]) winRegQueryValue.invoke(userRoot, handle.intValue(), toCstr("PortNumber"));

closeKey.invoke(Preferences.userRoot(), handle);

Any idea?

+3
source share
2 answers

I have the same problem. It seems that you cannot read words using Java-Preferences methods. (you can only read lines, and I don’t think it will ever be changed)

, , Runtime.exec(), regedit.exe , - . , . http://sourceforge.net/projects/java-registry/

jRegistryKey.jar dll ( ) , http://www.trustice.com/java/jnireg/ ( )

+2

, REG_DWORD (Dword) REG_SZ (String) ,

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

public class ReadRegistry
{


    public static final String readRegistry(String location, String key){
        try {
            // Run reg query, then read output with StreamReader (internal class)
            Process process = Runtime.getRuntime().exec("reg query " +
                    '"'+ location + "\" /v " + key);

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();

            // Parse out the value
            // String[] parsed = reader.getResult().split("\\s+");

            String s1[];
            try{
             s1=reader.getResult().split("REG_SZ|REG_DWORD");
            }
            catch(Exception e)
            {
                return " ";
            }
           //MK System.out.println(s1[1].trim());

            return s1[1].trim();
        } catch (Exception e) {
        }

        return null;
    }
     static class StreamReader extends Thread {
        private InputStream is;
        private StringWriter sw= new StringWriter();

        public StreamReader(InputStream is) {
            this.is = is;
        }

        public void run() {
            try {
                int c;
                while ((c = is.read()) != -1)
                    //System.out.println(c);
                    sw.write(c);
            } catch (IOException e) {
            }
        }

        public String getResult() {
            return sw.toString();
        }
    }
}

trim,

 String outputValue = ReadRegistry.readRegistry(REGISTRY_PATH, REGISTRY_KEY);

. REGISTRY_PATH REGISTRY_KEY . .

0

Source: https://habr.com/ru/post/1795044/


All Articles