Developer Center Resources

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling C/C++ DLLs from LabVIEW

 

» Calling External Code » Calling C/C++ DLLs

Basics: Call Library Function Node

LabVIEW gives you the ability to call DLLs written in C and C++ using the Call Library Function Node. The Call Library Function Node allows you to specify the DLL you want to use, the function within the DLL you want to call and allows you to configure the inputs and outputs (parameters and return values) of the function.

 

LabVIEW Help: Configuring the Call Library Function Node

Tutorial: Configuring the Call Library Function Node to call a Simple DLL

Import Shared Library Wizard

The Import Shared Library Wizard can automate a lot of the effort involved in calling C/C++ DLLs by creating wrapper VIs with pre-configured Call Library Function Nodes that you can call from your VIs.

 

In order to use the wizard to create wrapper VIs, you must have a header file associated with the DLL. The wizard requires this header file in order to properly create the wrapper VIs with the correct inputs and outputs.
Tutorial: Creating Wrapper VIs for C/C++ DLL functions using the Import Shared Library Wizard

 

Note: While the Import Shared Library Wizard does very well in creating wrapper VIs for functions that use simple data types and stucts of simple data types, it has some limitations especially relating to:

  • Confusion between pointers and Arrays: Since arrays technically are simply pointers to the first element in the array, the Import Shared Library cannot tell them apart.

  • Return Types: Since the Call Library Function Node only allows void, integer and string return types, anything more complex (like structs) must be handled as pointers.

  • Pointers: Pointers to complex types and pointers returned via return value might not be dereferenced (that is, they will be represented as an integer with a value equal to the memory address pointed to).

 

These caveats are detailed in the following article. The article also includes examples of how to work around them.

Calling C/++ DLLs Containing Simple and Complex Datatypes from LabVIEW

Representing Structs as Clusters

The equivalent for representing structs in LabVIEW is a LabVIEW Cluster. As with structs in C/C++, the ordering of elements in a cluster make a difference, as this is the order in which they are stored in memory. To pass a cluster to a DLL that expects a struct, make sure to order the elements in the cluster in the same order as those in the struct.

 

For example:

Structs.png

If you create a cluster with the improper order of elements, the elements can be reordered after the fact:
LabVIEW Help: Modifying Cluster Element Order

 

For examples on calling C/C++ functions that accept or return structs, refer to:

Calling C/++ DLLs Containing Simple and Complex Datatypes from LabVIEW

Pointers and References

Several languages like C and C++ employ the concept of pointers, which is basically a variable that holds (points to) the address (in memory) of the data you are interested in. This is especially prevalent in functions that need to return multiple values. In these cases, programmers use a technique called Call By Reference where the parameters are pointers to the values rather than the actual values.

 

This can be initially challenging in LabVIEW since LabVIEW hides the concept of pointers and memory allocation from the user, as do several modern languages like C# and Java.

 

There are ways to pass pointers to C/C++ DLLs, as well as to receive and dereference pointers from C/C++ DLLs in LabVIEW. For details refer to:

Passing and Receiving Pointers with C/C++ DLLs from LabVIEW

Dereferencing Pointers from C/C++ DLLs in LabVIEW

 

Examples

To see how to call a C/C++ DLL from LabVIEW and pass around different types of data (ranging from numerics, string and arrays to pointers and structs/clusters) between LabVIEW and the DLL, refer to:
Calling C/++ DLLs Containing Simple and Complex Datatypes from LabVIEW

 

Child Documents

Configuring the Call Library Function Node to call a Simple DLL

Creating Wrapper VIs for C/C++ DLL functions using the Import Shared Library Wizard

Passing and Receiving Pointers with C/C++ DLLs from LabVIEW

Dereferencing Pointers from C/C++ DLLs in LabVIEW

Calling C/++ DLLs Containing Simple and Complex Datatypes from LabVIEW

 

Jervin Justin
NI TestStand Product Manager
Comments
rolfk
Knight of NI Knight of NI
Knight of NI
on

I find the picture under "Representing Structs as Clusters" rather unfortunately chosen. It would never suggest to the normal reader that a structure with an array and/or string pointer in it can be represented by a cluster with an according LabVIEW array or string. Almost nothing is further from the truth than that.

 

I would rather chose a struct with some skalar elements as an embedded struct and show the according LabVIEW Cluster.

Rolf Kalbermatter
My Blog
rolfk
Knight of NI Knight of NI
Knight of NI
on

Another warning. The examples about struct return values are simply wrong. They do not work in the way proposed. See for more details here: https://forums.ni.com/t5/LabVIEW/Problems-with-Calling-C-C-DLLs-from-LabVIEW-Returning-a-value/td-p/...

 

Rolf Kalbermatter
My Blog