0

I need to use RegLoadKey function in my java code by using jna, but I'm getting the following error message:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'RegLoadKey': The specified procedure could not be found. Blockquote

RegLoadKey syntax

LONG WINAPI RegLoadKey(
  _In_      HKEY hKey,
  _In_opt_  LPCTSTR lpSubKey,
  _In_      LPCTSTR lpFile
);

my code: Advapi32.java

import com.sun.jna.platform.win32.WinReg.HKEY;
import com.sun.jna.win32.StdCallLibrary;

public interface Advapi32 extends StdCallLibrary
{
    long RegLoadKey(HKEY hKey, String lpSubKey,String lpFile);
}

apiTest.java

import com.sun.jna.*;
import com.sun.jna.platform.win32.WinReg.HKEY;
public class apiTest
{
       public static void main (String [] args)
       {

          Advapi32 lib2 = (Advapi32) Native.loadLibrary("Advapi32", Advapi32.class);
          HKEY key1 = new HKEY();
          String filePath = "C:\\tmp\\software";
          String regName = "loadedRegKey";
          long test = lib2.RegLoadKey(key1, regName, filePath);
       }

I think there are several problems with my code. I'm new to windows api and jna.

Anthony J.
  • 375
  • 1
  • 5
  • 14

3 Answers3

0

Did you know that an Advapi32 encapsulation is already part of JNA? Have a look here. I just saw that your method RegLoadKey is not yet added there. So add it and submit that change to the jna guys. Afterwards you can use it like this (pseudo code):

public class RegistryRead{

 private Advapi32 api = null;

 public RegistryRead(){
    this.api = Advapi32.INSTANCE;
 }

 public void read() {
    long winapi = this.api.RegLoadKey(HKEY hkey, String subkey, String file);
    ...
 }
}
Lonzak
  • 9,334
  • 5
  • 57
  • 88
0

If you look at the Advapi32 library mapping that comes with JNA, you'll see that the library instantiation includes some options to the load method. Among other things, these load options automatically map things like RegLoadKey to RegLoadKeyW, which is the real name of the function you're trying to link to.

technomage
  • 9,861
  • 2
  • 26
  • 40
-1

It is a typical for JNA developing error. Just add before using it.

System.setProperty("jna.library.path","PATH_TO_LIBRARY_JNA");

PATH_TO_LIBRARY_JNA - absolute path to jna lib

bummi
  • 27,123
  • 14
  • 62
  • 101