Example Code

Calling a C-built DLL in LabVIEW to Generate and Sort an Array of Numbers

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

    Programming Language

  • C++

Code and Documents

Attachment

Description

This example is designed to illustrate the concept of using external code in LabVIEW. Specifically, this example demonstrates that LabVIEW allows a developer to use the code developed external to LabVIEW. This example contains a LabVIEW program that generates and sorts an array of numbers by calling a C-built DLL using LabVIEW's Call Library Function Node. This example also contains the C source code that the DLL was built from. It contains a .c file and a .prj file that can be build in LabWindows/CVI, as well as a .cpp file and all of the project files necessary to build this in Microsoft Visual C++. If you see a broken arrow when you open the LabVIEW VI, double-click on the Call Library Function Node and relink to the exact path of the DLL.

The material in Additional Information section below compares C and LabVIEW's functionality.

Requirements:

  • LabVIEW 8.0 or later or LabVIEW Evaluation Software is required to view and run the LabVIEW examples.
  • Any C compiler is required to view and execute the C code. If you do not have a C compiler, you can download the LabWindows/CVI Evaluation Software to run the C code and to try the premium C environment for measurement and automation applications. At a minimum, the LabWindows/CVI Run Time Engine is required to run the CVI executable.

 

 

Additional Information

Calling LabVIEW Code Externally

Building a shared library allows LabVIEW code to be shared and reused in other programming languages. callLVDLL.c, linked below, features a C program that calls functions from a LabVIEW-built DLL, LV_AAP.dll, to acquire, analyze, and present data. In order to make this example hardware independent, the “acquire” function does not actually acquire data but generates a sine wave. The “analyze” function computes the FFT Power Spectrum of the data, and the “present” function displays the data on a graph.

 

LV_AAP.DLL has been built from the VIs acquire.vi, analyze.vi and present.vi. The LabVIEW project LV_AAP.lvproj references these three VIs and includes the Build Specification required to create the DLL. With the project loaded you can examine how the DLL is created by expanding the Build Specification then right click on LV_APP and select Properties to view the Build Specification Properties dialog box for the DLL as shown in Figure 1.

 



Figure 1. Build Application or Shared Library Dialog Box in LabVIEW


Click the Source Files category to add VIs to be exported for the DLL. Each VI you add corresponds to one function called from an external environment. Click the Define VI Prototype button to display the Define VI Prototype dialog box shown in Figure 2. This dialog box allows you to define the function name, return type, and parameters of the DLL function. The parameters are generated automatically by clicking the Add button. The data types will be consistent with the VI connector terminals.

 



Figure 2. Define VI Prototype Dialog Box in LabVIEW



Select the Source File Settings category to configure the settings for each VI you want to include. To configure VI properties, click the Customize VI Properties button. In Figure 3, notice how the "Show front panel when called" setting is checked for "present.vi". This allows a user to view the front panel of "present.vi" and see the LabVIEW graph when they call a corresponding DLL function from another programming language. The other two VIs included in the DLL do not have this setting checked.

 



Figure 3. VI Settings Tab of the Application Builder


Click Build to generate a .dll file, a .h file, and a .lib file. These files make it easy to call the functions in the LabVIEW DLL. From a C environment, you can add the .lib and .h files to the project, include the .h file at the top of the source code, and put the labview\cintools folder in the include paths list to directly make the LabVIEW DLL function calls. Figure 4 shows the C function calls made to the LabVIEW DLL.


Figure 4. C Calls to the LabVIEW DLL Functions


This example is designed to encourage you to use LabVIEW even if it cannot be the primary development environment. 

 

 

Related Links

 

 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.