简体   繁体   English

使用JNA访问从Java编译为本机代码的C#方法

[英]Using JNA to access C# methods compiled to native code from Java

I have a C# class with one method compiled to native code, that I want to call from Java method using JNA . 我有一个C#类,其中的一种方法已编译为本机代码,我想使用JNA从Java方法中调用它。 That class is packed as dll in a file called DataGrabber.dll. 该类作为dll打包在一个名为DataGrabber.dll的文件中。 This is the C# code: 这是C#代码:

namespace GrabberLibrary {
    public class Grabber {
        public void GetData(String projectId, String importURI){
            //implementation code
        }
    }
}

I've implemented the interface as suggested: 我已经按照建议实现了接口:

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);
}

and then when I call this GetData method from the main method in Java like this: 然后当我从Java的main方法中调用此GetData方法时,如下所示:

System.setProperty("jna.library.path", "C:\\dlls");
Grabber sgl = Grabber.INSTANCE;
sgl.GetData("123", "http://localhost/test");

I get the following exception: 我得到以下异常:

java.lang.UnsatisfiedLinkError: Unable to load library 'Grabber': The specified module could not be found.

On the other hand, if I put the name of the dll which is the same as the namespace defined in C# code (DataGrabber) I get: 另一方面,如果我输入的dll名称与C#代码(DataGrabber)中定义的名称空间相同,则会得到:

java.lang.UnsatisfiedLinkError: Error looking up function 'GetData': The specified procedure could not be found.

How can I call GetData method? 如何调用GetData方法?

I think you are trying to load DLL file in wrong place, it should be in main method. 我认为您尝试将DLL文件加载到错误的位置,应该在main方法中。 Look at here please : http://aboulton.blogspot.com/2012/07/netbeans-and-java-native-access.html 请在这里查看: http : //aboulton.blogspot.com/2012/07/netbeans-and-java-native-access.html

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

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