我有一个 C# 类,其中一个方法编译为 native 代码,我想使用 JNA 从 Java 方法调用它.该类作为 dll 打包在名为 DataGrabber.dll 的文件中。 这是 C# 代码:
namespace GrabberLibrary {
public class Grabber {
public void GetData(String projectId, String importURI){
//implementation code
}
}
}
我已经按照建议实现了接口(interface):
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface Grabber extends Library{
Grabber INSTANCE = (Grabber)Native.loadLibrary("Grabber", Grabber.class);
void GetData(String projectId, String importURI);
}
然后当我像这样从 Java 中的 main 方法调用此 GetData 方法时:
System.setProperty("jna.library.path", "C:\\dlls");
Grabber sgl = Grabber.INSTANCE;
sgl.GetData("123", "http://localhost/test");
我得到以下异常:
java.lang.UnsatisfiedLinkError: Unable to load library 'Grabber': The specified module could not be found.
另一方面,如果我输入与 C# 代码 (DataGrabber) 中定义的命名空间相同的 dll 名称,我会得到:
java.lang.UnsatisfiedLinkError: Error looking up function 'GetData': The specified procedure could not be found.
如何调用 GetData 方法?
最佳答案
我认为你试图在错误的地方加载 DLL 文件,它应该在 main 方法中。 请看这里:http://aboulton.blogspot.com/2012/07/netbeans-and-java-native-access.html
关于c# - 使用 JNA 访问从 Java 编译为 native 代码的 C# 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8296837/