# Module using dll - unloading?

**URL:** <https://forum.inductiveautomation.com/t/module-using-dll-unloading/6183>\
**Category:** Module Development\
**Created:** [April 9, 2013, 3:39pm UTC](https://forum.inductiveautomation.com/t/module-using-dll-unloading/6183 "2013-04-09T15:39:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [April 9, 2013, 3:39pm UTC](https://forum.inductiveautomation.com/t/module-using-dll-unloading/6183/1 "2013-04-09T15:39:16Z")

</div>

I’ve created on windows 7, an ignition driver module based on a CAN driver with a PEAK pc-card  
The card is provided with a driver with java class which use a dll providing native fonction.

An initialisation class load the dll :

```auto
System.loadLibrary("PCANBasic_JNI");

or System.load("D:/Appli/eco2charge/Jprojects/libPeak/32-bit/PCANBasic.dll");
System.load("D:/Appli/eco2charge/Jprojects/libPeak/32-bit/PCANBasic_JNI.dll");
```

All is fine excepted this point :

When I reload my module I’ve an error message indicating that the libary is already loaded.  
I need to restart Ignition service each time I reload my module.

I’ve read that the dll is only unloaded when the JVM is restarted.  
When the class which load the dll is destroy, the dll is not unloaded.

What is the solution ?  
Can we force to unload dll in the module shutdwon

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 9, 2013, 3:47pm UTC](https://forum.inductiveautomation.com/t/module-using-dll-unloading/6183/2 "2013-04-09T15:47:07Z")

</div>

There’s no official supported way of doing this in Java, but you can try this hack that tends to work most of the time…

Do this during shutdown in your module hook:

```auto
		try {
			ClassLoader classLoader = this.getClass().getClassLoader();
			Field field = ClassLoader.class.getDeclaredField("nativeLibraries");
			field.setAccessible(true);
			Vector<Object> libs = (Vector<Object>) field.get(classLoader);
			for (Object o : libs) {
				Method finalize = o.getClass().getDeclaredMethod("finalize", new Class[0]);
				finalize.setAccessible(true);
				finalize.invoke(o, new Object[0]);
			}
		} catch (Throwable t) {
			LogManager.getLogger(this.getClass()).error("Error unloading native resources.", t);
		}
```

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [April 10, 2013, 9:48am UTC](https://forum.inductiveautomation.com/t/module-using-dll-unloading/6183/3 "2013-04-10T09:48:12Z")

</div>

Yes,  
not understan all this code but it works like a charm !  
thanks a lot kevin !  
 ![:thumb_left:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/e26afb081ddde6df016df6ee49159cf56aaca688.gif ":thumb\_left:")
