Reputation: 31
How can I read DWORD value data from windows registry using java.util.prefs.Preferences. I can read REG_SZ type data but when reading REG_DWORD type, null is returned.
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?
Upvotes: 3
Views: 2963
Reputation: 1063
The simplest method I have found to read REG_DWORD(Dword) and REG_SZ(String) registry both by using the following code
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();
}
}
}
you can use trim to use only values and remove the other spaces character
How to use this class
code is below
String outputValue = ReadRegistry.readRegistry(REGISTRY_PATH, REGISTRY_KEY);
Note: REGISTRY_PATH and REGISTRY_KEY are constants. Its value depends upon your query path and query key.
Upvotes: 1
Reputation: 21
I have the same problem. It seems that you can NOT read dwords with the Java-Preferences methods. (you can only read Strings, and i don't think that this will ever be changed)
I found a project that deals with that issue by calling Runtime.exec() and the regedit.exe witch is pretty dirty, but might by help full for somebody else with the same problem. This project uses the same approach as you for Strings. http://sourceforge.net/projects/java-registry/
You can also use jRegistryKey.jar and dll (which i want to get rid off) or other native interfaces like http://www.trustice.com/java/jnireg/ (which i haven't tried)
Upvotes: 2