相关文章推荐
空虚的领带  ·  从CPU漏洞Meltdown&Spectre ...·  1 年前    · 
欢快的乌冬面  ·  花臂少年徐雷 - 抖音·  1 年前    · 
爱运动的小虾米  ·  侨务信息·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to read some user-defined windows registry data on remote machine using java.
I am able to read my local machine registry data using jna and even update it back.
Can anybody help on how to read/write data to remote machine using java.

In Java , the function may look like the following, granted that you've added the jna-platform library as a dependency which provides ready-made API types and functions:

import com.sun.jna.Native;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.platform.win32.WinReg.HKEY;
import com.sun.jna.platform.win32.WinReg.HKEYByReference;
import com.sun.jna.platform.win32.WinReg.HKEY_LOCAL_MACHINE;
import com.sun.jna.win32.W32APIOptions;
interface MyAdvapi32 extends StdCallLibrary {
    MyAdvapi32 INSTANCE = (MyAdvapi32) Native.loadLibrary(
        "advapi32",
        MyAdvapi32.class,
        W32APIOptions.DEFAULT_OPTIONS
    int RegConnectRegistry(String machineName, HKEY hKey, HKEYByReference result);
    int RegCloseKey(HKEY key);

You might notice the W32APIOptions.DEFAULT_OPTIONS used in the library load. The Windows API provides two different implementations for functions which use strings: one for Unicode strings and one for ANSI strings. While the function is named RegConnectRegistry, the implementations which JNA finds in the DLL are named RegConnectRegistryW (Unicode) and RegConnectRegistryA (ANSI). However, these probably are not your concern since you're not writing native code.

Passing the default options in lets JNA use the correct function names, avoiding a confusing-at-best UnsatisfiedLinkError.

Usage might look like this:

HKEYByReference result = new HKEYByReference();
int returnCode = MyAdvapi32.INSTANCE.RegConnectRegistry(
    "\\\\server-name",
    HKEY_LOCAL_MACHINE,
    result
if (returnCode != 0) {
    throw new Win32Exception(returnCode);
HKEY key = result.getValue();
// ... use the key, then once done with it ...
MyAdvapi32.INSTANCE.RegCloseKey(key);

By the way, jna-platform library does provide mappings for the Advapi32 library, but RegConnectRegistry seems to be missing. Ideally, you'd probably create a pull request and add it in, but YMMV as to how quickly they roll out a new release with the addition.

EDIT: I've created a pull request to JNA to get this function added in.

hi cubrr, thanks for update. I tired as you suggested and getting java.lang.UnsatisfiedLinkError: Error looking up function 'RegConnectRegistry': The specified procedure could not be found. – Rakesh Mar 6, 2018 at 13:47 @Rao I've edited my answer's Java example and added an explanation for the error below it. Let me know if it works! – cbr Mar 6, 2018 at 18:45 thanks cubrr, it worked. need some more info on user access. how to check if user has access or not before accessing the registry and only if user has access. FYI, all keys are user-defined only not system created/application related. – Rakesh Mar 7, 2018 at 13:00

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.