运行DLL中声明的方法的代码

发布于 2024-12-29 22:35:43 字数 1454 浏览 6 评论 0原文

我创建了一个 Java 代码,尝试访问 myAPI.dll 中定义的类 IProjectFactoryLoadProject 方法。 DLL 文件的描述如下:IProjectFactory 用于将项目文件加载到内存中。 IProjectFactory 是 myAPI.dll 程序集中的静态类。它公开 LoadProject 方法,该方法采用包含要加载的文件路径的字符串,并返回对生成的 IProject 的引用。使用 IProject 接口,您可以通过各种方式操作加载的项目。

import com.sun.jna.Library;
import com.sun.jna.Native;

public class MyClass {

public interface IProjectFactory extends Library {
    public Object LoadProject(String fileName);
}

public MyClass() {
    //System.loadLibrary("myAPI");
    load();
}

void load() {
    String fileName = "xxx.sp";
    IProjectFactory api = (IProjectFactory) Native.loadLibrary("myAPI",IProjectFactory.class);

    try {
        Object project = api.LoadProject(fileName);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

}

运行此代码后,会生成以下错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Error looking up function 'LoadProject': The specified procedure could not be found.

    at com.sun.jna.Function.<init>(Function.java:179)
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:350)
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:330)
    at com.sun.jna.Library$Handler.invoke(Library.java:203)
    at $Proxy0.LoadProject(Unknown Source)

这是否意味着 myAPI 不包含该类IProjectFactory 使用方法 LoadProject

I created a Java code that tries to access the method LoadProject of the class IProjectFactory defined in myAPI.dll. The description of the DLL file says: IProjectFactory is used to load a project file into memory. IProjectFactory is a static class in the myAPI.dll assembly. It exposes the LoadProject method that takes a string containing the path to the file to load, and returns a reference to the resulting IProject. Using the IProject interface you can then manipulate the loaded project in various ways.

import com.sun.jna.Library;
import com.sun.jna.Native;

public class MyClass {

public interface IProjectFactory extends Library {
    public Object LoadProject(String fileName);
}

public MyClass() {
    //System.loadLibrary("myAPI");
    load();
}

void load() {
    String fileName = "xxx.sp";
    IProjectFactory api = (IProjectFactory) Native.loadLibrary("myAPI",IProjectFactory.class);

    try {
        Object project = api.LoadProject(fileName);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

}

After running this code, the following error message has been generated:

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Error looking up function 'LoadProject': The specified procedure could not be found.

    at com.sun.jna.Function.<init>(Function.java:179)
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:350)
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:330)
    at com.sun.jna.Library$Handler.invoke(Library.java:203)
    at $Proxy0.LoadProject(Unknown Source)

Does it mean that myAPI does not contain the class IProjectFactory with the method LoadProject?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆 2025-01-05 22:35:43

您可以使用 JNI 或 JNA 调用本机库中的函数。原始类型被映射。某些结构也是可能的。甚至回调函数也是可能的。请参阅 JNA 的映射表。

从设计上看,不可能的事情是:获取专为不同运行时环境(如 CLR)设计的类或接口,并在 JVM 中无缝使用它。

因此,如果您有一个仅返回指针或原始类型的本机过程/函数库,那么您可以很好地使用它。

如果您需要使用返回的对象,那么您就不走运了。您需要在其本机环境中运行它们并找到某种进程间通信的方式。

You can call functions in native libraries with JNI or JNA. Primitive types are mapped. Certain structures are possible also. Even callback functions are possible. See JNA's mapping table.

What is impossible, by design: Getting a class or interface that was designed for a different runtime environment (like CLR) and use it seamless within the JVM.

So if you have a native procedural/functional library that just returns pointers or primitive types then you can use it quite well.

If you need to work with objects that are returned then you are out of luck. You need to run them in their native environment and find some way of interprocess communication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文