Using Complex Datatypes with C DLLs in LabVIEW

개요

When using DLLs written in C in LabVIEW, the datatypes from the DLL may be more complex than simple integers. In these cases, some considerations may need to be made to use the different datatypes. 

Contents

Import Shared Library Wizard

For most DLLs, using the Import Shared Library Wizard will generate code that will handle simple datatypes like integers, floats, one-dimensional arrays and strings. However, there may be issues with the generation of the LabVIEW library that are shown on the last screen of the library generation.

One of the most common errors is produced when trying to use datatypes and functions that are not supported by the Import Shared Library Wizard, such as multidimensional arrays or functions returning a value that is not a numeric or string. These issues will show up in the Import Shared Library Wizard on the Configure VIs and Controls page with a red exclamation point glyph (  ) on the parameter. In those cases, you will have to use and configure a Call Library Function Node manually. 

Complex Datatypes

Clusters (Structs)

When using structs in your DLL, in order to ensure that the data being passed between LabVIEW and the DLL is aligned correctly in memory, it may be necessary to create a wrapper DLL around the DLL you want to call.

Create a Wrapper DLL
  1. Create the LabVIEW cluster that you would like to pass into the DLL and wire it to the input of the Call Library Function Node. Ensure the input being connected to is set to Adapt to Type.
    • The input type must be set to Adapt to Type to make the input a void pointer that you can pass any data to.
  2. Right-click the Call Library Function Node and select Create .c File. This will generate a .c file that contains a structure definition that you can use and is aligned to the LabVIEW’s memory.
  3. Call the desired DLL from within the .c file by wrapping the function and using the LabVIEW-created datatypes.
    • An example of how to set this up with descriptive comments can be found installed with LabVIEW in <labview>\examples\Connectivity\Libraries and Executables\Source C  Files\CLUSTERSimpleS_AWrap.c
  4. Compile the .c file into a DLL using an appropriate toolchain for your system, then call the wrapper function from LabVIEW.

 

Multidimensional Arrays

If you have a multidimensional array of data in LabVIEW and need a one-dimensional array, you can use the Reshape Array function to change the dimension of your array. Depending on the format that the DLL expects the data to be in, whether row- or column-major, you may have to transpose the array before reshaping it. This will swap the rows for columns and change the order in which the values appear in the one-dimensional array.

Other Complex Datatypes

For more in-depth help on passing complex datatypes to and from DLLs, see External Code (DLL) Execution.vi in the LabVIEW Example Finder. It has examples on how to configure your Call Library Function Node to pass multiple complex datatypes. For example, there are examples on how to handle up to three-dimensional arrays and complex clusters. Some of these cases involve wrapping the generated DLL with custom C code as shown above.

Example C code for the DLL and requisite wrapper code are included in the <labview>\examples\Connectivity\Libraries and Executables\Source C Files directory.