Skip to content

Latest commit

 

History

History
33 lines (28 loc) · 1.34 KB

2010-03-23-jna-accessing-windows-registry-from-java.markdown

File metadata and controls

33 lines (28 loc) · 1.34 KB
layout title redirect_from date tags comments dblog_post_id
post
JNA: accessing Windows registry from Java
/jna-accessing-windows-registry-from-java/
2010-03-23 11:29:30 -0700
jna
java
win32
true
90

Some things should just be easy. I've committed a bunch of registry utility functions into JNA's platform.jar, all with unit tests. You don't have to break your head with Win32 API, just use com.sun.jna.platform.win32.Advapi32Util and the following functions.

  • registryCreateKey
  • registryGetIntValue
  • registryGetStringValue
  • registryValueExists
  • registryKeyExists
  • registrySetIntValue
  • registrySetStringValue
  • registryDeleteValue
  • registryDeleteKey

Try these:

Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA");
Advapi32Util.registrySetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "IntValue", 42);
System.out.println(Advapi32Util.registryGetIntValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "IntValue"));
Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA");

I'll add REG_MULTI_SZ support and registry key and value enumerators next.

If you were using JnaContrib Registry classes for these purposes before, you should switch. The code in the latter doesn't do a very good job at consistently throwing exceptions when a key cannot be read or found.