简体   繁体   English

Jna,动态地从java类卸载Dll

[英]Jna, Unload Dll from java class dynamically

I have googled bit , but dint find way to unload dll using JNA ,from java Class. 我有谷歌搜索,但dint找到方法从Java类使用JNA卸载DLL。 And as i am using dll to transfer data from usb device using this dll, i have to unload my dll from java class in order to re-use my usb device with same class without closing my whole program. 而且当我使用dll使用这个dll从usb设备传输数据时,我必须从java类中卸载我的dll,以便在不关闭整个程序的情况下重新使用同一类的usb设备。 here is how i load my dll using JNA 这是我如何使用JNA加载我的DLL

public interface UsbSensor extends Library {

        UsbSensor INSTANCE = (UsbSensor) Native.loadLibrary(
                (Platform.isWindows() ? "D:\\UsbDevice.dll" : "D:\\UsbDevice.dll"), UsbSensor.class);

        int SearchDevices();

        Pointer Startacquisition(String type);
}

and by 并通过

 UsbSensor sdll = UsbSensor.INSTANCE; 

Dll is loded. Dll被塞满了。 And here how i use my function 在这里我如何使用我的功能

sdll.SearchDevices();
sdll.Startacquisition();

And now after using these function I must have to unload my dll in again load dll using above code. 现在使用这些功能后,我必须使用上面的代码再次卸载我的dll加载DLL。 order reuse these function. 命令重用这些功能。

So how to unload dll dynamically Using JNA? 那么如何使用JNA动态卸载dll?

NativeLibrary.dispose() should do what you are looking for. NativeLibrary.dispose()应该做你想要的。 NativeLibrary is a 1:1 representation of the native library you are using (and is used internally by Native.loadLibrary() anyway). NativeLibrary是您正在使用的本机库的1:1表示(并且无论如何都由Native.loadLibrary()在内部使用)。 So 所以

  1. null the reference returned by Native.loadLibrary() and null Native.loadLibrary()和。返回的引用
  2. call NativeLibrary.dispose() 调用NativeLibrary.dispose()

I had the same issue but using Native.dispose() did not help. 我有同样的问题,但使用Native.dispose()没有帮助。 The solution to my problem was this question and answer . 我的问题的解决方案是这个问题和答案 It basically sets instance of the native library to null and calls the garbage collector. 它基本上将本机库的实例设置为null并调用垃圾收集器。

你可以创建一个临时的类加载器,使用本机函数,null对类加载器的引用,它将与它加载的类/ dll一起符合GC的条件

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

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