How to Load and Unload a DLL Dynamically in LabVIEW

Updated Oct 31, 2022

Reported In

Software

  • LabVIEW

Issue Details

I would like to only load my DLL in memory when it is called in my program and then unload it at the end of the program.

I would like to avoid including unused libraries in my code.

Solution

After LabVIEW 8.2, Dynamic Link Librarys (refered to DLLs from now on) can be dynamically loaded and unloaded. The following article Call Library Function Node describes the process. In summary, to dynamically load a DLL, the Specify path on diagram option needs to be checked in the Call Library Function Dialog Box. The dialog box can be opened by double clicking on the Call Library Function node, as shown in the figure below. 
 
 

After the Specify path on diagram parameter is checked, a terminal named path in appears on the Call Library Function. This terminal accepts the path to the DLL that you want to call.  When that Call Library Function is executed, the DLL specified in the reference is loaded into memory. See the figure below.


The following documentation Call Library Function Dialog Box states that the path in input can be used to unload a previously referenced library from memory. To do so, wire an empty or invalid path to the path in input and LabVIEW will unload any library previously referenced by the node from memory. In other words, you must wire an empty path constant or null path constant to the path in input of the same Call Library Function node that initially called your DLL. The picture below shows an example. The first call loads the DLL and the boolean unloads the DLL as well as stops this code. The false case, not shown, has timing but no code.

2.jpg

If an empty or invalid path is wired to a different Call Library Function node than the one that initially called
your DLL it will not unload the DLL from memory. The picture below shows an example of what not to do.
 

Additional Information

If multiple VI's call and load the same DLL, then the DLL will only be loaded into LabVIEW memory once. By unloading the DLL in the first VI, it will unload the DLL in all VI's that may have it loaded.