简体   繁体   English

JNA通过Java在dll文件中找不到指定的过程

[英]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. 我正在尝试通过Java访问dll过程,但是我的java方法无法找到该过程。 The dll file is loaded successfully but the the procedure from C# code named Login I can't call. 该dll文件已成功加载,但无法调用C#代码中名为Login的过程。

Below is the def of Procedure in ADHelper.dll: 下面是ADHelper.dll中Procedure的定义:

 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. dll文件名为ADHelper.dll。 The LoginResult is enum type: LoginResult是枚举类型:

public enum LoginResult
    {
      LOGIN_OK,
      LOGIN_USER_DOESNT_EXIST,
      LOGIN_USER_ACCOUNT_INACTIVE,
    }

Below is my java program to normally call the procedure: 下面是我的Java程序通常调用的过程:

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. 线程“主”中的异常java.lang.UnsatisfiedLinkError:查找函数“ Login”时出错:找不到指定的过程。

Do tell me if any more details are needed. 告诉我是否需要更多详细信息。

The solution is based upon this methodolgy: 该解决方案基于以下方法:

enums/constants handling in java for dll. 枚举/常量在Java中为dll处理。

NOTE: I have included the dll file in system32 for testing purpose, as for easy access too. 注意:我已在system32中包含dll文件以进行测试,以方便访问。 the dll file is loading but the Login function is not calling. dll文件正在加载,但Login函数未调用。

The output of SOP line in java is : Java中SOP行的输出为:

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

The problem here is that your DLL is a .Net DLL, which is not a native DLL. 这里的问题是您的DLL是.Net DLL,而不是本机DLL。 JNA only loads and understands native DLLs, that is, DLLs built to function outside the .Net framework. JNA仅加载和理解本机DLL,即为在.Net框架之外运行而构建的DLL。

What this means is that you need a different glue between Java and .Net. 这意味着您需要在Java和.Net之间使用不同的粘合剂。 I have successfully worked with Jni4net and IKVM , and there are a few others, you might want to look at those. 我已经成功地使用了Jni4netIKVM ,还有其他一些,您可能想看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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