943,083 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1205
  • Java RSS
Mar 16th, 2009
0

Print Registry Folder Contents

Expand Post »
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.


Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AllenB is offline Offline
13 posts
since Aug 2008
Mar 18th, 2009
1

Re: Print Registry Folder Contents

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.
Featured Poster
Reputation Points: 1895
Solved Threads: 942
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008
Mar 19th, 2009
0

Re: Print Registry Folder Contents

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AllenB is offline Offline
13 posts
since Aug 2008
Mar 19th, 2009
0

Re: Print Registry Folder Contents

Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,754 posts
since May 2007
Mar 20th, 2009
0

Re: Print Registry Folder Contents

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:
JAVA Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1895
Solved Threads: 942
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008
Jul 27th, 2010
0
Re: Print Registry Folder Contents
Reputation Points: 10
Solved Threads: 0
Newbie Poster
randyrude41 is offline Offline
1 posts
since Jul 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to extract columns of coordinate values from text file (in java) ?
Next Thread in Java Forum Timeline: Help with extensions on FileFilters...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC