我是使用JNA从Java访问DLL的新手。我需要从DLL中的类(用.net编写)访问方法。在下面形成此示例DLL,我试图获取AuditID和服务器ID。我在运行代码时遇到以下错误。任何指导真的很感激。
///错误///
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetEnrollcontext': The specified procedure could not be found.
// DLL文件代码//
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)));
}
}
答案 0 :(得分:2)
实际上有一个解决方案,您可以通过Java中的JNA使用C#,VB.NET或F#代码(没有别的)!它也很容易使用: https://www.nuget.org/packages/UnmanagedExports
使用此软件包,您需要做的就是将[RGiesecke.DllExport.DllExport]添加到您的方法中:
C#.dll项目:
[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"));
中提琴!
如前所述,它非常简单,但此解决方案有一些限制:
[RGiesecke.DllExport.DllExport]
public static Boolean externalAvailable(String yourParameter)
{
return yourInternalFunction(yourParameter);
}
使用C#它运行得很好,使用VB.NET和F#我没有经验。 希望这有帮助!