Example Code

Calling a LabVIEW-built DLL in C to Acquire, Analyze, and Present Data

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

Download All

Description

This example illustrates the concept of sharing LabVIEW code by building a shared library (DLL). Specifically, this example demonstrates that you can reuse LabVIEW code when your main program is written in C. Using this capability, new features and functionality that you create using LabVIEW can easily be added to existing legacy C code. Included in this example are C and LabWindows/CVI applications that acquire, analyze, and present data by calling LabVIEW-built DLLs.

 

Complete the following steps to run the C code.

 

  1. Open call LVDLL.c in any C compiler.
  2. Include the LV_AAP.h and LV_AAP.lib files with your project.
  3. Place the LV_AAP.dll file in the same directory as the other files in the project.
  4. Build and run the application (ready to build examples are provided for LabWindows/CVI and Microsoft Visual C++ 2008)
  5. To run the ready-to-run LabWindows/CVI executable, place the LV_AAP.dll, LV_AAP.h, LV_AAP.lib and the CallLVDllAAP.uir files in the same directory as the CallLVDllAAP.exe executable.

Also included in this example is the LabVIEW source code for the DLL and the LabVIEW Project used to create it. More details comparing C and LabVIEW's functionality appear in the "Additional Information" section below.

 

Note: When running the application from Microsoft Visual C++ 2008, you must include the directory containing the LabVIEW support files in the "directories" section of the environment. From the VC++ environment, select Tools»Options. Expand the Projects and Solutions section and select VC++ Directories. Select “Include files” in the “Show directories for:” drop down and add the \Program Files\National Instruments\LabVIEW 8.5\cintools directory to the list (replace 8.5 in this file path with your LabVIEW version). This will prevent you from receiving the "Cannot find extcode.h" error.


Requirements:

  • LabVIEW 8.0 or later or LabVIEW Evaluation Software is required to view and run the LabVIEW source code behind the DLLs. LabVIEW Professional Development System 8.0 or higher required to build the DLL.
  • Project Files have been included for Microsoft Visual C++ 2008 and LabWindows™/CVI™. Download the LabWindows/CVI Evaluation Software to run the C code and to try the premier 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 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 1. LabVIEW Code That Calls GenSortRand_dbg.dll

 



Figure 2. 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 3. Call Library Function Dialog Box

 

 

Related Links

 

 

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