Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read windows registry REG_DWORD value with java.util.prefs.WindowsPreferences

 
sambasivarao laghuvarapu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.util.prefs.WindowsPreferences is the concrete implementation of AbstractPreferences in the Windows platform. This class provides methods like WindowsRegQueryValueEx, etc. Using Reflection, one can use the methods in this class to query string values under HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER. For some reason, the java wrapper for WindowsRegQueryValueEx only seems to work for string values. I get a null for values of type REG_DWORD, which I guess makes this approach pretty useless except in times of desperation. Please Tell me how I can retrive REG_DWORD values.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't count on classes not in the public API. They may disappear or change at any time. This includes WindowsPreferences because it's not public.

The Windows native call RegQueryValueEx returns the actual type in its fourth parameter. This call has 6 parameters. In contract, the Java method WindowsRegQueryValueEx only has 2 parameters - the key and the name (as a byte[]). So, Sun discard the actual type at this point. I've checked the source of WindowsPreferences some more, and it totally ignores the possible type - it just treats the byte[] returned by the method as a String. I recon WindowsRegQueryValueEx returns null if the actual registry value type is not a string. This makes me believe that WindowsPreferences convert any input to and from REG_SZ (stirng).

If I were you, I'd go and find a third party registry library that uses JNI. Check out http://www.google.com/search?client=opera&rls=en&q=java+windows+registry&sourceid=opera&ie=utf-8&oe=utf-8 and JNIRegistry from http://www.gjt.org/download/. The latter has a class RegistryValue with subclasses RegBinaryValue, RegDWordValue, RegMultiStringValue and RegStringValue. With those, the only types you're missing are REG_DWORD_BIG_ENDIAN, REG_LINK, REG_NONE, REG_QWORD and REG_QWORD_LITTLE_ENDIAN.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a beginner's question. Moving.
 
manjula sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Don't count on classes not in the public API. They may disappear or change at any time. This includes WindowsPreferences because it's not public.

The Windows native call RegQueryValueEx returns the actual type in its fourth parameter. This call has 6 parameters. In contract, the Java method WindowsRegQueryValueEx only has 2 parameters - the key and the name (as a byte[]). So, Sun discard the actual type at this point. I've checked the source of WindowsPreferences some more, and it totally ignores the possible type - it just treats the byte[] returned by the method as a String. I recon WindowsRegQueryValueEx returns null if the actual registry value type is not a string. This makes me believe that WindowsPreferences convert any input to and from REG_SZ (stirng).

If I were you, I'd go and find a third party registry library that uses JNI. Check out http://www.google.com/search?client=opera&rls=en&q=java+windows+registry&sourceid=opera&ie=utf-8&oe=utf-8 and JNIRegistry from http://www.gjt.org/download/. The latter has a class RegistryValue with subclasses RegBinaryValue, RegDWordValue, RegMultiStringValue and RegStringValue. With those, the only types you're missing are REG_DWORD_BIG_ENDIAN, REG_LINK, REG_NONE, REG_QWORD and REG_QWORD_LITTLE_ENDIAN.



which one download from page: http://www.gjt.org/download/ to get thired party registry library that uses JNI???
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manjula sharma wrote:which one download from page: http://www.gjt.org/download/ to get thired party registry library that uses JNI?

Are you asking how to find the string "JNIRegistry" -which Rob mentioned was the project's name- on that page?
 
manjula sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
manjula sharma wrote:which one download from page: http://www.gjt.org/download/ to get thired party registry library that uses JNI?

Are you asking how to find the string "JNIRegistry" -which Rob mentioned was the project's name- on that page?


I am using java.util.pref.WindowsPreferences to read windows registry values.
but it returns NULL for all key value data which is not of type String.
(Same problem as posted at the top of this page)
Is there anyother JNI to solve the issue, where to download it??
 
Tobias Hoppenthaler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am experiencing the same problems - guess you have found a solution by now, if not, here's a thread that might help.
http://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic