Call DLL from Java using JNA

天大地大妈咪最大 提交于 2019-12-17 20:31:38

问题


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.

/// Error ///

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

//DLL File Code//

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

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

回答1:


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

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

C# .dll Project:

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

Java Project:

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

use it in the code:

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

Viola!

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

  • only available for simple datatypes as parameter & return values
  • no MethodOverloading available. yourFunction(String yourParameter) and yourFunction(String yourParameter, String yourSecondParameter) does not work! you have to name them differently
  • Use arrays as parameter or return values. (JNA offers StringArray, but I am not able to use them in C#) (maybe there is a solution, but I couldn't come up with one so far!)
  • if you export a method you can't call it internally in your C# code (simple to bypass that by the following:

.

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

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



来源:https://stackoverflow.com/questions/22695829/call-dll-from-java-using-jna

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!