如何使用java.util.prefs.Preferences从Windows注册表中读取DWORD值数据。 我可以读取REG_SZ类型的数据,但在读取REG_DWORD类型时,返回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);
有什么想法吗?