简体   繁体   中英

JNA couldn't find the specified procedure in dll file through java

I am trying to access dll procedure through java but my java method is unable to find the procedure. The dll file is loaded successfully but the the procedure from C# code named Login I can't call.

Below is the def of Procedure in ADHelper.dll:

 public static ADHelper.LoginResult Login(string UserName, string Password)
    {
      if (!ADHelper.IsUserValid(UserName, Password))
        return ADHelper.LoginResult.LOGIN_USER_DOESNT_EXIST;
      DirectoryEntry user = ADHelper.GetUser(UserName);
      if (user == null)
        return ADHelper.LoginResult.LOGIN_USER_DOESNT_EXIST;
      int userAccountControl = Convert.ToInt32(RuntimeHelpers.GetObjectValue(user.Properties["userAccountControl"][0]));
      user.Close();
      return !ADHelper.IsAccountActive(userAccountControl) ? ADHelper.LoginResult.LOGIN_USER_ACCOUNT_INACTIVE : ADHelper.LoginResult.LOGIN_OK;
    }

The dll file name is ADHelper.dll. The LoginResult is enum type:

public enum LoginResult
    {
      LOGIN_OK,
      LOGIN_USER_DOESNT_EXIST,
      LOGIN_USER_ACCOUNT_INACTIVE,
    }

Below is my java program to normally call the procedure:

package dllTest;

import com.sun.jna.*;

public class DllTester {




         public interface ADHelper extends Library {    

             public final int LOGIN_OK=1;
             public final int LOGIN_USER_DOESNT_EXIST=2;
             public final int LOGIN_USER_ACCOUNT_INACTIVE=3;


               public int Login(String user, String pass);
           }
           public static void main(String[] args) {


            ADHelper objADH = (ADHelper) Native.loadLibrary("ADHelper", ADHelper.class);
            System.out.println(objADH.getClass().getDeclaredMethods()); 
            objADH.Login("ashish", "asdas");


           }

}

Now, it is gives following error:

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

Do tell me if any more details are needed.

The solution is based upon this methodolgy:

enums/constants handling in java for dll.

NOTE: I have included the dll file in system32 for testing purpose, as for easy access too. the dll file is loading but the Login function is not calling.

The output of SOP line in java is :

[Ljava.lang.reflect.Method;@145d068

The problem here is that your DLL is a .Net DLL, which is not a native DLL. JNA only loads and understands native DLLs, that is, DLLs built to function outside the .Net framework.

What this means is that you need a different glue between Java and .Net. I have successfully worked with Jni4net and IKVM , and there are a few others, you might want to look at those.

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