简体   繁体   中英

Read/Write windows registry on 64bit Win 7 using JAVA with JNA

I'm trying to read/write windows registry on 64bit Win7 using JAVA.

Firstly, I tried JDK java.util.prefs.Preferences and its reflection usage . That is a good solution but it only supports reading/writing REG_SZ type (string) value.

Unfortunately, I need to read/write REG_BINARY, so give it up.

Secondly, I tried JNI Registry . Reading is ok, but writing usually fails because writing HKLM needs administrator rights. I don't know how to get administrator rights in JAVA.

Finally, I tried JNA (Java Native Access) an excellent project for working with native libraries and has support for the Windows registry in the platform library (platform.jar) through Advapi32Util and Advapi32 . It's very good and simple to use. And writing registry needs no administrator rights.

But how can I read/write 32bit Registry (under WOW6432Node node) in a 64bit JVM on Win7?

By default, 64bit nodes are read/written in 64bit JVMs, and 32bit nodes in 32bit JVMs.

But in a 64bit JVM, I want to read/write 32bit nodes(for example, HKLM->SOFTWARE->Wow6432Node->ODBC ). How can I do that?

I got the answer: Using 64 bit Windows:

  1. A 32 bit JVM, visiting HKLM->SOFTWARE->ODBC will be redirected to HKLM->SOFTWARE->Wow6432Node->ODBC
  2. A 64 bit JVM, visiting HKLM->SOFTWARE->ODBC will be HKLM->SOFTWARE->ODBC (which is 64bit registry).
  3. A 32 or 64bit JVM, visiting HKLM->SOFTWARE->Wow6432Node->ODBC is allowed. (but in C++, win32 exe can't visit HKLM->SOFTWARE->Wow6432Node->ODBC directly, should visit HKLM->SOFTWARE->ODBC with wow64 flag).

JNA 5 adds new methods to Advapi32Util which allows you to specify the samDesiredFlags including KEY_WOW64_32KEY or KEY_WOW64_64KEY.

PR here: https://github.com/java-native-access/jna/pull/1001

Registry security and access docs here: https://docs.microsoft.com/en-us/windows/win32/sysinfo/registry-key-security-and-access-rights

KEY_WOW64_32KEY (0x0200) Indicates that an application on 64-bit Windows should operate on the 32-bit registry view. This flag is ignored by 32-bit Windows. For more information, see Accessing an Alternate Registry View. This flag must be combined using the OR operator with the other flags in this table that either query or access registry values. Windows 2000: This flag is not supported.

KEY_WOW64_64KEY (0x0100) Indicates that an application on 64-bit Windows should operate on the 64-bit registry view. This flag is ignored by 32-bit Windows. For more information, see Accessing an Alternate Registry View. This flag must be combined using the OR operator with the other flags in this table that either query or access registry values. Windows 2000: This flag is not supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM