|| 3 out of 5
How Do I Call a Dynamic Link Library (DLL) from LabVIEW?
How do I call a DLL from LabVIEW?
To call a Dynamic Link Library (DLL) from LabVIEW see the help section Calling Code Written in Text-Based Programming Languages
. The help section contains additional information not included in this document. To call a DLL, you first must identify if the library is a C/C++ DLL or a Microsoft .NET Assembly. A C/C++ DLL can be generated from C, C++, or any other programming language (besides .NET languages) such as Java, Fortran, or Visual Basic 6.0. A .NET DLL is also called a .NET assembly or 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:
- Find the header file (*.h) or function prototype definitions.
- 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.
- 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.
Import Shared Library Wizard
- If the function does not contain any complex data types and you do not have a header file (*.h), first ensure that you are working with a C/C++ DLL. If you are in fact dealing with a C/C++ DLL, find the documentation for the DLL containing function prototypes. You will need to identify the function parameters any of the functions you wish to call inside of the DLL. Once you have identified the parameters, place the Call Library Function Node on your block diagram. This is what you will use to manually call the function.
Call Library Function Node Location
- 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.
Call Library Function Node Configuration
- 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.
Call Library Function Node Parameters
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 a char * 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.
Note: The LabVIEW Example Finder contains an example that explains how to handle several different data types used with the Call Library Function Node. This example shows the LabVIEW format for complex or non-LabVIEW data types, such as structs, when passing in or reading values with the Call Library Function Node. It also contains examples of using the Call Library Function Node for a specific data type.
Call DLL VI
Using LabVIEW 2012 or earlier:
The example is named Call DLL.vi. To find this example, go to the LabVIEW Example Finder (Help»Find Examples) and select the Directory Structure radio button. In the directories list, select dll and then data passing then Call Native Code.llb. The Call DLL.vi example is under that directory.
To view the example for a data type, select the data type in the Data Type list. Then click the Open Example VI for Current Data Type button. This will open a new VI showing the use of the Call Library Function Node.
External Code (DLL) Execution VI
Using LabVIEW 2013 or later:
The example is named External Code (DLL) Execution.vi. After opening the LabVIEW Example Finder (Help»Find Examples) and selecting the Directory Structure radio button, navigate to the following directory Connectivity»Libraries and Executables. Open the External Code (DLL) Execution.vi.
Run the VI. To view an example for the data type, double-click the data type entry in the list. This will open an example VI. On the right, you can see the example function prototypes, the DLL source code, any tips for utilizing the function in LabVIEW, and a summary. If the DLL is a .NET assembly:
- In order to use a .NET assembly in LabVIEW, simply use the .NET palette (Connectivity».NET) to find all of the functions available.
- 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.
Select .NET Constructor Dialog Box
- 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.
Using LabVIEW 2013 or later:
The LabVIEW Example Finder provides an example for calling a private .NET assembly. This example, named Calling a Private .NET Assembly.vi, can be found in the Example Finder Directory structure in the following location:
Help»Find Examples»Communicating with External Applications».NET
This example calls a simple .NET Calculator assembly created by National Instruments to demonstrate how to call a private .NET Assembly.
KnowledgeBase 281A5HQO: Call Library Function Returns the Wrong Function Prototype and Function Parameters for DLLs
KnowledgeBase 1NM99D6Q: External Code for LabVIEW - CIN/DLL - Starting Point
Tutorial: Creating Wrapper VIs for C/C++ DLL functions using the Import Shared Library Wizard
Assemblies in the Common Language Runtime
LabVIEW 2015 Help: Call Library Function Node
LabVIEW 2015 Help: Calling external APIs
LabVIEW 2009 Help: Calling Code Written in Text- Based Programming Languages
NI Community: Calling External Code From LabVIEW