JNA无法找到通过Java dll文件规定的程序 [英] JNA couldn't find the specified procedure in dll file through java

查看:1427
本文介绍了JNA无法找到通过Java dll文件规定的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Java访问DLL的程序,但我的Java方法是无法找到的程序。 DLL文件加载成功,但要从名为登录我不能把C#代码的程序



下面是程序的ADHelper.dll的DEF:



 公共静态ADHelper.LoginResult登录(用户名字符串,字符串密码)
{
如果(!ADHelper.IsUserValid(用户名,密码))
返回ADHelper.LoginResult.LOGIN_USER_DOESNT_EXIST;
的DirectoryEntry用户= ADHelper.GetUser(用户名);
如果(用户== NULL)
返回ADHelper.LoginResult.LOGIN_USER_DOESNT_EXIST;
INT userAccountControl的= Convert.ToInt32(RuntimeHelpers.GetObjectValue(user.Properties [userAccountControl的] [0]));
user.Close();
的回报!ADHelper.IsAccountActive(userAccountControl的)? ADHelper.LoginResult.LOGIN_USER_ACCOUNT_INACTIVE:ADHelper.LoginResult.LOGIN_OK;
}



DLL文件名是ADHelper.dll。该LoginResult是枚举类型:

 公共枚举LoginResult 
{
LOGIN_OK,
LOGIN_USER_DOESNT_EXIST,
LOGIN_USER_ACCOUNT_INACTIVE,
}

下面是我的java程序正常调用过程

 包dllTest; 

进口com.sun.jna *。

公共类DllTester {




公共接口ADHelper扩展库{

公众最终诠释LOGIN_OK = 1;
公众最终诠释LOGIN_USER_DOESNT_EXIST = 2;
公众最终诠释LOGIN_USER_ACCOUNT_INACTIVE = 3;


公众诠释登录(用户字符串,字符串传递);
}
公共静态无效的主要(字串[] args){


ADHelper objADH =(ADHelper)Native.loadLibrary(ADHelper,ADHelper.class) ;
的System.out.println(objADH.getClass()getDeclaredMethods());
objADH.Login(与Ashish,asdas);


}

}

现在,人们给出了以下错误:




异常线程mainjava.lang.UnsatisfiedLinkError中:错误
仰视功能登陆:指定的程序无法找到




不要告诉我,如果需要的更多细节。



的解决方案是基于这样methodolgy:



枚举/常量在Java中处理的DLL



请注意:我已经包含在dll文件SYSTEM32用于测试目的,如为方便了。 。dll文件加载,但登录功能并没有叫



SOP线在Java中的输出是:



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


解决方案

这里的问题是,你的DLL是一个.NET的DLL,这是不是原生的DLL。 JNA将只读取和理解本机DLL,就是建立在.NET框架之外运作的DLL。



这意味着你需要的Java之间不同的胶水。净。我已经成功地与 Jni4net 并的 IKVM ,还有其他一些,你可能想看看那些。


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.

这篇关于JNA无法找到通过Java dll文件规定的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆