简体   繁体   English

我无法使用JNA以Java代码访问Visual Basic DLL方法

[英]I'm not able to access Visual Basic DLL methods in Java code using JNA

I was pushed to use JNA (Java Native Access) in order to read data through Ehternet (based on UDP protocol). 我被迫使用JNA(Java Native Access)来通过Ehternet(基于UDP协议)读取数据。 I've received, plugin UDP_RT.dll, programmed in visual basic. 我收到了用Visual Basic编程的插件UDP_RT.dll。 Now I'm trying to use this library in my Java code with JNA help. 现在,我正在JNA帮助下尝试在Java代码中使用此库。 I went through a lot of tutorials, with some successful but some part of code doesn't work and now no clue what I can do next . 我经历了很多教程,都取得了一些成功,但是某些代码无法正常工作,现在不知道下一步该做什么。 I been able to load library, but can't solve puzzle how to use method from UDP_RT.dll, becouse I'm getting an error: 我已经能够加载库,但是无法解决如何使用UDP_RT.dll中的方法的难题,因为我遇到了一个错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'close': Nie można odnaleźć określonej procedury.
enter code here
at com.sun.jna.Function.<init>(Function.java:208)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:536)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:513)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:499)
at com.sun.jna.Library$Handler.invoke(Library.java:199)

I saw a lot of threads on stackoverflow like How to access DLL methods in Java code using JNA? 我在stackoverflow上看到很多线程,例如如何使用JNA访问Java代码中的DLL方法?

What I been able to figure out is that might be issue of UDP_RT.dll rather that my JAVA Code. 我能够弄清楚的可能是UDP_RT.dll的问题,而不是我的JAVA代码的问题。 I have to admit that I don't have a lot of experience with visual basic (only basic skills) also I don't have a UDP_RT.dll source code (but wheh I would be mandatory I think I may get). 我必须承认,我在视觉基础方面没有太多经验(仅是基本技能),也没有UDP_RT.dll源代码(但是我认为我可能必须这样做)。 I've tried to load these methods in Visual Basic it seems load these methods. 我试图在Visual Basic中加载这些方法,似乎加载了这些方法。 I looked to deinition of UDP_RT in visual and I can see some methods. 我在视觉上查看了UDP_RT的定义,并且可以看到一些方法。 Below I'm attaching screenshots, java code and visual basic code Thanks in advance for your help! 在下面,我附上屏幕截图,Java代码和Visual Basic代码,在此先感谢您的帮助!

  interface UDP_RT extends Library {

    public boolean   Init(int a,int b ,int c,int d);
    void close();

    }


public class Native2{

    public static void main(String[] args) {
        UDP_RT UDP = (UDP_RT)Native.loadLibrary("UDP_RT2", UDP_RT.class,W32APIOptions.ASCII_OPTIONS);
        UDP.close();
        UDP.Init(9000, 8000, 1, 2);

    }
}

udp设计师视觉基本代码

The Visual Studio Object Browser is showing you the assembly's .NET metadata. Visual Studio对象浏览器向您显示程序集的.NET元数据。 A .NET assembly does not usually have the same functions in its function export table. .NET程序集通常在其函数导出表中没有相同的函数。 You can see the export table using Dependency Walker or CFF Explorer . 您可以使用Dependency WalkerCFF Explorer查看导出表。 If you don't see them, it means they aren't callable via the technique that you're trying. 如果您没有看到它们,则表示无法通过您尝试的技术来调用它们。

You could write a JNI interface using a C++/CLI wrapper for the .NET assembly. 您可以使用.NET程序集的C ++ / CLI包装器编写JNI接口。 C++/CLI is designed to intermix C++ and .NET. C ++ / CLI旨在将C ++和.NET混合在一起。 The wrapper functions can be exported as JNI implementations and their code can call the .NET methods. 包装函数可以作为JNI实现导出,其代码可以调用.NET方法。 It's not too difficult if the methods you what to call are static or if the .NET objects you create will live only for the duration of a wrapper function call. 如果您要调用的方法是静态的,或者您创建的.NET对象仅在包装函数调用期间有效,则并不太困难。

You should also consider using an interop library for .NET (eg, Javonet, JNBridge) or, if the assembly's object are exposed through COM an interop library for COM (eg, JNIWrapper). 您还应该考虑使用用于.NET的互操作库(例如Javonet,JNBridge),或者,如果程序集的对象是通过COM公开的,则可以使用针对COM的互操作库(例如JNIWrapper)。 See Best way to call VB.NET function from Java? 请参阅从Java调用VB.NET函数的最佳方法? .

In JNI types are wrapped into adapter to be java-compatible. 在JNI中,类型被包装到适配器中以与Java兼容。 The methods must be named java-compatible too. 这些方法也必须命名为与Java兼容。 Try to build your own dll to wrap/unwrap and load the dll indirectly. 尝试构建自己的dll来包装/解包并间接加载dll。

Look for javah to create JNI compatible header-files. 查找javah以创建JNI兼容的头文件。

A example here: http://www.sahirshah.com/java/jni.html 此处的示例: http : //www.sahirshah.com/java/jni.html

There exists a package called Unmanaged Export and it is very handy and easy to use! 存在一个名为“ 非托管导出”的软件包,它非常方便且易于使用! from the link you can access the docu via "Project Site" 从链接中,您可以通过“项目站点”访问文档

With this package you can export C#, VB.NET or F# Methodes to Java via JNA (and nothing else). 使用此程序包,您可以通过JNA将C#,VB.NET或F#方法导出到Java(没有其他功能)。 I gave a little more details in the following post: Call DLL from Java using JNA - which seems somekind related/similar to me 我在以下文章中提供了更多详细信息: 使用JNA从Java调用 DLL-似乎与我有些相关/相似

it helped me alot with my own project. 它帮助我完成了自己的项目。 tbh I never tried it with VB.NET just with C#, but as Robert Giesecke says in his docu, it should work with both. 我从来没有尝试过仅使用C#在VB.NET中进行尝试,但是正如Robert Giesecke在他的文档中所说的那样,它应该同时适用于两者。 hope this helps. 希望这可以帮助。 cheers 干杯

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

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