Print Registry Folder Contents

Reply

Join Date: Aug 2008
Posts: 13
Reputation: AllenB is an unknown quantity at this point 
Solved Threads: 0
AllenB AllenB is offline Offline
Newbie Poster

Print Registry Folder Contents

 
0
  #1
Mar 16th, 2009
Hello,

I need to print the contents of a Windows Registry folder (Display Names). I have posted what I have started with and would appreciate some help. It returns a null value at this point, even though there are items in this folder. Thank you.


  1. import java.lang.reflect.Method;
  2. import java.util.prefs.Preferences;
  3.  
  4. public class JavaRegistry {
  5. private static final int HKEY_CURRENT_USER = 0x80000001;
  6. private static final int KEY_QUERY_VALUE = 1;
  7. private static final int KEY_SET_VALUE = 2;
  8. private static final int KEY_READ = 0x20019;
  9.  
  10. public static void main(String args[]) {
  11. final Preferences userRoot = Preferences.userRoot();
  12. final Preferences systemRoot = Preferences.systemRoot();
  13. final Class clz = userRoot.getClass();
  14.  
  15. try {
  16. final Method openKey = clz.getDeclaredMethod("openKey",
  17. byte[].class, int.class, int.class);
  18. openKey.setAccessible(true);
  19. final Method closeKey = clz.getDeclaredMethod("closeKey",
  20. int.class);
  21. closeKey.setAccessible(true);
  22.  
  23. final Method winRegQueryValue = clz.getDeclaredMethod(
  24. "WindowsRegQueryValueEx", int.class, byte[].class);
  25. winRegQueryValue.setAccessible(true);
  26. final Method winRegEnumValue = clz.getDeclaredMethod(
  27. "WindowsRegEnumValue1", int.class, int.class, int.class);
  28. winRegEnumValue.setAccessible(true);
  29. final Method winRegQueryInfo = clz.getDeclaredMethod(
  30. "WindowsRegQueryInfoKey1", int.class);
  31. winRegQueryInfo.setAccessible(true);
  32.  
  33. byte[] valb = null;
  34. String vals = null;
  35. String key = null;
  36. Integer handle = -1;
  37.  
  38.  
  39. key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Adobe";
  40. handle = (Integer) openKey.invoke(systemRoot, toCstr(key), KEY_READ, KEY_READ);
  41. valb = (byte[]) winRegQueryValue.invoke(systemRoot, handle, toCstr("Display Name"));
  42. vals = (valb != null ? new String(valb).trim() : null);
  43. System.out.println("Adobe Products = " + vals);
  44. closeKey.invoke(Preferences.systemRoot(), handle);
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48.  
  49. }
  50. private static byte[] toCstr(String str) {
  51. byte[] result = new byte[str.length() + 1];
  52. for (int i = 0; i < str.length(); i++) {
  53. result[i] = (byte) str.charAt(i);
  54. }
  55. result[str.length()] = 0;
  56. return result;
  57. }
  58. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 971
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Print Registry Folder Contents

 
1
  #2
Mar 18th, 2009
OK, no other answers, so I'll have a go.
This is coding on wild side! You use refection to break into private methods that are not part of the public API. Even if you get this to work now, there's no guarantee that it won't break in the very next minor Java update. Seems dangerous to me.
Anyway, try it without the HKEY_LOCAL_MACHINE\\ in your key, I think that's already implied by systemRoot.
If you want a safer way to approach this, you can use ProcessBuilder to run the command line registry tool reg.exe, and stick to published public APIs. I can let you have some sample code if you want to try it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 13
Reputation: AllenB is an unknown quantity at this point 
Solved Threads: 0
AllenB AllenB is offline Offline
Newbie Poster

Re: Print Registry Folder Contents

 
0
  #3
Mar 19th, 2009
James,

I am really new to accessing the Registry, so some sample code would be very helpful. Thank you for your recommendations. I will try them out. Thanks again
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,422
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Print Registry Folder Contents

 
0
  #4
Mar 19th, 2009
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 971
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Print Registry Folder Contents

 
0
  #5
Mar 20th, 2009
Here's a small program that will query a value from the registry. It runs reg.exe with the appropriate command then parses the out pt to get the result. If you look at the help messages for reg.exe you'll see the other options, and you'll then need to add some parsing to get whatever other results you need. The only other options I've ever found are (1) a neat Java/dll package called LatteLib that works well, but seems to have vanished from the net a couple of years ago, and (2) very many variants on the code that hooks into the private methods of the Java Windows Preferences implementation (with the dangers that I mentionsed in my earlier post).
Anyway, here's some code that may be useful to you:
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5.  
  6. public class WindowsRegistry {
  7.  
  8. // just read a simple value so far...
  9. // see reg.exe help (in a command window) for other options
  10.  
  11. public static String queryValue(String key, String value) {
  12. // returns null if key/value not found (or other error)
  13.  
  14. try {
  15. Process p = new ProcessBuilder("reg.exe", "QUERY", key, "/v", value)
  16. .redirectErrorStream(true).start();
  17. // (reg.exe requires parameters to be already tokenised)
  18. InputStream is = p.getInputStream();
  19. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  20.  
  21. String line;
  22. // scan output & parse interesting lines...
  23. while ((line = br.readLine()) != null) {
  24. System.out.println("Input: " + line);
  25. if (line.contains(value)) {
  26. // this line displays the value we are looking for
  27. int i = line.indexOf("REG_SZ");
  28. if (i >= 0) {
  29. String result = line.substring(i + 7).trim();
  30. is.close();
  31. return result;
  32. }
  33. }
  34. if (line.contains("Error")) {
  35. System.err.println("reg.exe error searching for: " + key
  36. + " " + value);
  37. System.err.println(line);
  38. is.close();
  39. return null;
  40. }
  41. }
  42. System.err.println("ERROR: unable to parse output from reg.exe");
  43. System.err.println("Searching for: " + key + " " + value);
  44. is.close();
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. return null;
  49. }
  50.  
  51. public static void main(String args[]) throws IOException {
  52. // a little demo...
  53. String res;
  54. res = WindowsRegistry.queryValue("HKCU\\Control Panel\\Desktop",
  55. "Wallpaper");
  56. System.out.println("Wallpaper path: \"" + res + "\"");
  57.  
  58. res = WindowsRegistry.queryValue("HKCU\\Control Panel\\Desktop",
  59. "noSuchValue");
  60. System.out.println("Incorrect query returned: \"" + res + "\"");
  61. }
  62. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC