Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
85 ratings:
 2.98 out of 5   

How Do I Call a Dynamic Link Library (DLL) from LabVIEW?



Hardware: PXI/CompactPCI>>Controllers

Problem:
How do I call a DLL from LabVIEW?

Solution:
To call a Dynamic Link Library (DLL) from LabVIEW see the help section Calling Code Written in Text-Based Programming Languages.  The help section contains additional information not included in this document.  To call a DLL,  you first must identify what type of DLL it is.  There are essentially two types of DLL's to consider: C/C++ DLL and a Microsoft .NET assembly.  A C/C++ DLL can be generated from C, C++, or any other programming language (besides .NET languages) such as Java, Fortran, or Visual Basic 6.0.  A .NET DLL is also called a .NET assembly or just assembly.  The reason for this terminology difference is that a .NET DLL does not work the same way as other DLL's.  A .NET DLL uses the Common Language Runtime (CLR) and the .NET Framework to manage the functions within the DLL.

If the DLL is a C/C++ DLL:
  1. Find the header file (*.h) or function prototype definitions.
  2. Identify the function(s) you wish to call.  Notice whether the parameters for the function contain primitive data type parameters such as int, char, double, etc or whether they contain complex data types such as structs, arrays or vectors.
  3. If the function does not contain any complex data types and you have a header file (*.h), a great tool in LabVIEW can help you easily import the DLL (and it will create an entire library for the DLL).  You can find this option in Tools»Import»Shared Library (.dll)...  Continue with the wizard.
  4. If the function does not contain any complex data types and you do not have a header file (*.h), first ensure that you are dealing with a C/C++ DLL as most programming environments will generate a header file along with the DLL.  If you are in fact dealing with a C/C++ DLL, find the documentation for the function as you will need to identify the parameters for the function you wish to call inside of the DLL.  Once you have identified the parameters, use the Call Library Function Node function in LabVIEW to manually call the function.
  5. If the function(s) you wish to call contains complex data types, you need to manually use a Call Library Function Node function in LabVIEW to directly call the DLL, defining the prototype within the configuration dialog (if the DLL has been compiled with a Type Library, parameter data and return types will be defined automatically).  Note that if your complex data type is a struct with more than primitive data types (int, double, char), it is easiest to create a wrapper DLL from a C-based language. 

    Since C/C++ constructs do not always directly correlate to LabVIEW data types, a wrapper DLL can appropriately extract the contents of the complex structure and translate them to primitive terms.  For instance, if you have a struct in C which contains a char * (a string or character array), you can create a wrapper function which LabVIEW will call that takes in a char * type directly, puts that into a structure, and then in turn calls the DLL.  Note that if you have the DLL source code, you can directly modify the DLL so that it takes in a char * instead of the struct.
Note: There is a very helpful example in the Labview Example finder that explains how to handle several different data types to be used with the Call Library Function Node. The example is named Call DLL.vi. Refer to this example when dealing with complex data types such as arrays or structs. To Find this example go to the LabVIEW Example Finder (Help»Find Examples) and select the Directory Structure radio button. In the directories list, select dll and then data passing then Call Native Code.llb. The Call DLL.vi example is under that directory. 

To view the example for a data type, select the data type in the Data Type list.  Then click the Open Example VI for Current Data Type button.  This will open a new VI showing the use of the Call Library Function Node.

If the DLL is a .NET assembly:
  1. In order to use a .NET assembly in LabVIEW, simply use the .NET palette (Connectivity».NET) to find all of the functions available.
  2. First use a constructor node in order to instantiate a class within the .NET assembly.
  3. Use property nodes and invoke nodes to access properties and methods of the class by simply wiring in the class reference from the constructor node to the property or invoke node.


Related Links:

KnowledgeBase 281A5HQO: Call Library Function Returns the Wrong Function Prototype and Function Parameters for DLLs
KnowledgeBase 1NM99D6Q: External Code for LabVIEW - CIN/DLL - Starting Point
LabVIEW 8.6 Help: Calling external APIs
LabVIEW 2009 Help: Calling Code Written in Text- Based Programming Languages
Tutorial: Creating Wrapper VIs for C/C++ DLL functions using the Import Shared Library Wizard



Attachments:





Report Date: 07/20/2007
Last Updated: 04/15/2016
Document ID: 4BJGH6GJ

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit