Can LabVIEW C? - Example 3: Using the Right Tools with LabVIEW

Publish Date: Jan 28, 2019 | 22 Ratings | 3.91 out of 5 | Print | Submit your review


The previous two examples have shown that LabVIEW is a programming language with many functional similarities to C, with multiple built-in functions and a graphical programming model that is well suited to creating parallel code. But the ability to reuse code written in other languages as well as being able to be called from external code are also important aspects of programming languages. This example has two parts to show both of these capabilities. These examples will show how code reuse is possible on Windows using Dynamic Link Libraries (DLLs), with similar code reuse techniques being possible on most LabVIEW-supported platforms.

Table of Contents

  1. Calling External DLLs From LabVIEW
  2. Calling LabVIEW Code Externally

1. Calling External DLLs From LabVIEW

If you have existing code in C and need to reuse it in LabVIEW, one technique is to build the code as a DLL and call it using the Call Library Function Node. In fact, based on the architecture of your C application, you could take advantage of the simpler approach to parallel programming offered by LabVIEW to run two or more existing C routines in parallel without the additional complexity of C-based multithreaded programming.

In this example, linked below, the GenAndSortC VI is identical to the VI from Example 1, with the exception that the random number generation and sorting are not implemented in LabVIEW. These operations are carried out by calling a DLL built in C. The DLL is named GenSort.dll and the functions that LabVIEW calls are named generateRand() and bubbleSort(). Figure 29 shows the LabVIEW code that calls the generateRand() function from a Call Library Function Node and Figure 30 shows the C code that actually generates the random numbers.

Figure 29. LabVIEW Code That Calls GenSortRand_dbg.dll

Figure 30. C Code for the generateRand() Function

To call a DLL from LabVIEW, place a Call Library Function Node on the block diagram and double-click to display the Call Library Function dialog box. Browse for your DLL and then choose your function name from the drop-down list. You can then add parameters, choose their data types, and choose whether to pass by value or reference. Figure 31 shows the Call Library Function dialog box, which can be used to configure DLL function calls. This example is designed to show the power and ease of calling a DLL in LabVIEW, but it is in no way designed to be a full tutorial. For more information on using DLLs and other third party code in LabVIEW 7.1 or earlier, refer to the Using External Code in LabVIEW manual (linked below). In LabVIEW 8.0 or later, refer to the Fundamentals»Calling Code Written in Text-Based Programming Languages book in the LabVIEW Help (linked below) for more information.

Figure 31. Call Library Function Dialog Box
See Also:
Using External Code in LabVIEW User Manual
Calling a C-built DLL in LabVIEW to Generate and Sort an Array of Numbers

Back to Top

2. 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, and 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 32.


Figure 32. 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 33. 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 33. 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 34, notice how the "Show front panel when called" setting is checked for "". This allows a user to view the front panel of "" 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 34. 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 35 shows the C function calls made to the LabVIEW DLL.

Figure 35. 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. For more details, refer to the Building DLLs in LabVIEW 6.x or Later tutorial.

See Also:
Calling Shared Libraries
Calling a LabVIEW-built DLL From C to Acquire, Analyze, and Present Data
Using External Code in LabVIEW


Additional Resources

Download the Can LabVIEW C - Example 3 Source Code (Calling C DLLs in LabVIEW)
Download the Can LabVIEW C - Example 3 Source Code
(Calling LabVIEW DLLs in C)
Learn how to implement standard software engineering practices in LabVIEW
Access the Can LabVIEW C overview whitepaper

Back to Top

Bookmark & Share


Rate this document

Answered Your Question?
Yes No