我试图通过java访问dll过程,但我的java方法无法找到该过程。 dll 文件已成功加载,但无法调用 C# 代码中名为 Login 的过程。
下面是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;
}
DLL 文件名为 ADHelper.dll。 LoginResult 是枚举类型:
public enum LoginResult
{
LOGIN_OK,
LOGIN_USER_DOESNT_EXIST,
LOGIN_USER_ACCOUNT_INACTIVE,
}
下面是我的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");
}
}
现在,它给出以下错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'Login': The specified procedure could not be found.
请告诉我是否需要更多详细信息。
解决方案基于以下方法:
enums/constants handling in java for dll.
注意:我已将 dll 文件包含在 system32 中用于测试目的,也方便访问。 dll 文件正在加载,但登录函数没有调用。
java中SOP行的输出是:
[Ljava.lang.reflect.Method;@145d068
最佳答案
这里的问题是您的 DLL 是 .Net DLL,它不是 native DLL。 JNA 仅加载和理解 native DLL,即为在 .Net 框架之外运行而构建的 DLL。
这意味着您需要在 Java 和 .Net 之间使用不同的粘合剂。我已成功与 Jni4net 合作和 IKVM ,还有其他一些,您可能想看看。
关于java - JNA通过java找不到dll文件中指定的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30565496/