Calling a Dynamic Link Library (DLL) from LabVIEW

Updated Nov 1, 2018

Reported In

Software

  • LabVIEW

Programming Language

  • C
  • C++
  • C# .NET

Issue Details

I have code/API written in C/C++ and I want to be able to call it in LabVIEW. Once I have made a Dynamically Linked Library (DLL) with my C code, how do I call the DLL from LabVIEW?

Solution

To call a DLL,  you first must identify if the library is a C/C++ DLL or a Microsoft .NET Assembly. A .NET DLL is also called a .NET assembly, and are useful in .NET programming. A .NET DLL uses the Common Language Runtime (CLR) and the .NET Framework to manage the functions within the DLL.


If the DLL is a C/C++ DLL:
 
  1. Find the header file (*.h) or function prototype definitions.
  2. Identify the function(s) you wish to call. Notice whether the parameters for the function contain primitive data type parameters such as int, char, double, etc or whether they contain complex data types such as a struct, array, or  vector.
  3. If the function does not contain any complex data types and you have a header file (*.h), you can use the Import Shared Library Wizard to create a LabVIEW library containing the functions in the header file. You can find this option in Tools»Import»Shared Library (.dll)...  Continue with the Wizard. 
  1. If you do not have the header file, you will need to use the Call Library Function Node along with proper documentation of the .DLL. The picture below shows the location of the Call Library Function Node in the pallet: 
  1. Double-click the Call Library Function Node to configure the node. On the Function tab of the configuration window, enter the path to the DLL and select the function you wish to call. 
  1. If the function(s) you wish to call contains complex data types, you'll need to manually define the function prototype within the configuration dialog in the Call Library Function Node (if the DLL has been compiled with a Type Library,  the parameter data and return types will be defined automatically). Navigate to the Parameters tab in the configuration window. Here you will add parameters and modify the return type until the function prototype at the bottom of the window matches the DLL's function definition.

If the DLL is a .NET assembly:
 
  1. In order to use a .NET assembly in LabVIEW, simply use the .NET palette (Connectivity».NET)to find all of the functions available.
  2. First use a Constructor Node in order to instantiate a class within the .NET assembly. This can be done by double clicking the Constructor Node to bring up the Select .NET Constructor dialog box. 
 
  1. Use Property and Invoke Nodes to access properties and methods of the class by simply wiring in the class reference from the Constructor Node to the property or Invoke Node. 
 

Additional Information

Note that if your complex data type is a struct with more than primitive data types (int, double, char), you can create a wrapper DLL from a C-based language to simplify function calls.  C/C++ constructs do not always directly correlate to LabVIEW data types, and a wrapper DLL can appropriately extract the contents of the complex structure and translate them to primitive terms.  For instance, a DLL you are using utilizes a struct in C which contains a char * (a string or character array). Your wrapper DLL can contain a wrapper function with achar * parameter that places the char * into a struct, and then in turn calls the original DLL. In LabVIEW, you can instead call the wrapper DLL functions. Alternatively, if you have the DLL source code, you can directly modify the DLL so that it takes in a char * instead of a struct.

The LabVIEW example finder has complete and functional examples of how to properly call external code in LabVIEW. See Help >> Find Examples >> Communicating with External Applications

WAS THIS ARTICLE HELPFUL?

Not Helpful