使用 JNA 从 Java 调用 DLL [英] Call DLL from Java using JNA

查看:54
本文介绍了使用 JNA 从 Java 调用 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 JNA 从 Java 访问 DLL 的新手.我需要从 DLL(用 .net 编写)中的类访问方法.形成下面的这个示例 DLL,我正在尝试获取 AuditID 和服务器 ID.当我运行我的代码时,我以以下错误结束.任何指导都非常感谢.

I am new to accessing DLLs from Java using JNA. I need to access methods from a class within a DLL(written in .net). Form this sample DLL below, I am trying to get AuditID and Server ID. I am ending with the following error while I am running my code. Any guidance really appreciated.

///错误///

 Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function  'GetEnrollcontext': The specified procedure could not be found.

//DLL文件代码//

//DLL File Code//

SampleDLL.ProfileEnroll enrollcontext = new SampleDLL.ProfileEnroll();
enrollcontext.Url =" url";
enrollcontext.AuditIdType = SampleDLL.ProfileId;
enrollcontext.AuditId = "22222222 "; 
enrollcontext.ServerId = "server1";

///Java 代码///

/// Java Code ///

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import dllExtract.DLLExtractTest.SampleDLL.Enrollcontext;


public class SampleDLLExtract {

    public interface SampleDLL extends Library {
        SampleDLL INSTANCE = (SampleDLL) Native.loadLibrary("SampleDLL",
            SampleDLL.class);

        public static class Enrollcontext extends Structure { 

            public String auditId;
            public String serverId;
        }
            void GetEnrollcontext(Enrollcontext ec);                     // void ();
    }
    public static void main(String[] args) {
        SampleDLL sdll = SampleDLL.INSTANCE;
        SampleDLL.Enrollcontext enrollContext = new SampleDLL.Enrollcontext();
        sdll.GetEnrollcontext(enrollContext);

        System.out.println(sdll.toString(sdll.GetEnrollcontext(enrollContext))); 
    }
}

推荐答案

事实上,有一个解决方案可以让您通过 Java 中的 JNA 使用 C#、VB.NET 或 F# 代码(仅此而已)!它也很容易使用:https://www.nuget.org/packages/UnmanagedExports

in fact there is a solution for you to use C#, VB.NET or F# code via JNA in Java (and nothing else)! and it is also very easy to use: https://www.nuget.org/packages/UnmanagedExports

有了这个包,你需要做的就是将 [RGiesecke.DllExport.DllExport] 添加到你的方法中:

with this package all you need to do is, add [RGiesecke.DllExport.DllExport] to your methods like that:

C# .dll 项目:

C# .dll Project:

[RGiesecke.DllExport.DllExport]
public static String yourFunction(String yourParameter)
{
    return "CSharp String";
}

Java 项目:

public interface jna extends Library {
    jna INSTANCE = (jna) Native.loadLibrary("yourCSharpProject.dll", jna.class);
public String yourFunction(String yourParameter);
}

在代码中使用它:

System.out.println(jna.INSTANCE.yourFunction("nothingImportant"));

中提琴!

如前所述,它工作起来非常简单,但此解决方案有一些局限性:

As already mentioned it works very easy, but this solution has some limitations:

  • 仅适用于简单数据类型作为参数 &返回值
  • 没有可用的方法重载.yourFunction(String yourParameter) 和 yourFunction(String yourParameter, String yourSecondParameter) 不起作用!你必须给它们起不同的名字
  • 使用数组作为参数或返回值.(JNA 提供了 StringArray,但我无法在 C# 中使用它们)(也许有一个解决方案,但到目前为止我还想不出一个!)
  • 如果你导出一个方法,你就不能在你的 C# 代码内部调用它(很容易通过以下方式绕过它:

.

[RGiesecke.DllExport.DllExport]
public static Boolean externalAvailable(String yourParameter)
{
    return yourInternalFunction(yourParameter);
}

使用 C# 效果很好,使用 VB.NET 和 F# 我没有经验.希望这会有所帮助!

With C# it works great, with VB.NET and F# I have no experience. hope this helps!

这篇关于使用 JNA 从 Java 调用 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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