Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Creating and Calling LabVIEW DLLs with Various Data Types from Microsoft Visual Basic

11 ratings | 3.73 out of 5
Print

Overview

This topic describes how to use several different data types to create LabVIEW shared libraries (DLLs) and how to call DLLs from Microsoft Visual Basic. Refer to the examples linked below for more information about creating DLLs in LabVIEW.

Accessing LabVIEW Numbers from Microsoft Visual Basic

In LabVIEW, you can use several different methods to pass numbers in VIs or functions. You can use similar methods in Microsoft Visual Basic. The following function prototype includes three numeric inputs and three numeric outputs. In/Out 1 are 32-bit integers, In/Out 2 are double-precision, floating-point numbers, and In/Out 3 are unsigned 8-bit integers. In1 and In3 are passed by value, and In2 is passed as a pointer to value. The three outputs must be passed as pointers to values.

Note: Use standard calling conventions when you create DLLs for Microsoft Visual Basic.


[+] Enlarge Image


The first line of the Microsoft Visual Basic code that calls numtest.dll should be written as follows:

Private Declare Sub Numtest Lib "C:\numtest.dll" (ByVal in1 As Integer, ByRef in2 As Double, ByVal in3 As Long, ByRef out1 As Integer, ByRef out2 As Double, ByRef out3 As Long)

In2 and the three outputs are passed by ref because they are set as pointers to values.

The code creates the following array variables:

Dim i1 As Integer
Dim i2 As Double
Dim i3 As Long
Dim o1 As Integer
Dim o2 As Double
Dim o3 As Long

The actual call to the function should be written as follows:

Call Numtest(i1, i2, i3, o1, o2, o3)

Refer to the Using Microsoft Visual Basic to Call LabVIEW DLLs That Pass Numeric Data Types topic linked below for an example of using Microsoft Visual Basic to send numbers to a LabVIEW DLL.

Accessing LabVIEW Booleans from Microsoft Visual Basic


When you create DLLs in LabVIEW 6.0 or later, you can pass only Boolean parameters as pointers to values. The following function prototype has one Boolean input and one Boolean output.

Note: Use standard calling conventions when you create DLLs for Microsoft Visual Basic.


[+] Enlarge Image


The first line of the Microsoft Visual Basic code that calls booltest.dll should be written as follows:

Private Declare Sub Booltest Lib "C:\booltest.dll" Alias "Booltest" (ByRef inBool As Byte, ByRef outBool As Byte)

In LabVIEW, a Boolean is one byte. In Microsoft Visual Basic, a Boolean is two bytes. In Microsoft Visual Basic, if you declare variables as Booleans, the memory can become corrupt or overwritten. Instead, you must declare each variable as one byte. The Booleans are passed by ref because they are set as pointers to values.

The code creates the following array variables:

Dim inputBool As Byte
Dim outputBool As Byte

The actual call to the function should be written as follows:

Call Booltest(inputBool, outputBool)

Refer to the Using Microsoft Visual Basic to Call LabVIEW DLLs That Pass Booleans topic linked below for an example of using Microsoft Visual Basic to pass Booleans to a LabVIEW DLL.

Accessing LabVIEW Arrays from Microsoft Visual Basic


In LabVIEW, you can pass an array of data as an array handle pointer or an array data pointer. For Microsoft Visual Basic, use the array data pointer option.

When you pass an array as an array data pointer, you also must pass a length input value that tells Microsoft Visual Basic the number of elements in the array. In LabVIEW, when you set up an array data pointer parameter within the Define VI Prototype panel, LabVIEW automatically adds a Length Input parameter to the VI or function for both input and output arrays. For output arrays, you also can pass a Length Output parameter that points to the size of the output array that the VI or function returns. The function prototype shown below is for a VI that has an input array and an output array with identical input lengths.

Note: Use standard calling conventions when you create DLLs for Microsoft Visual Basic.


[+] Enlarge Image


The first line of the Microsoft Visual Basic code that calls Pass_Array.dll should be written as follows:

Private Declare Sub PArray Lib "C:\Pass_Array.dll" Alias "PassArray" (ByRef iArray As Double, ByRef oArray as Double, ByVal len2 As Long)

The arrays are passed by ref, and the 0th element of the array is passed when the function is called, as shown in the code below. You cannot pass an array of doubles by value.

The code creates array variables, such as Dim inp(128) As Double.

The actual call to the function should look be written as follows:

Call PArray(inp(0), outp(0), 128)

You can place values into the input array before calling the array. Both the input and output arrays have 128 elements.

Refer to the Using Microsoft Visual Basic to Call LabVIEW DLLs That Pass Array Data Pointers topic linked below for an example of using Microsoft Visual Basic to send arrays to a LabVIEW DLL.

Accessing LabVIEW Strings from Microsoft Visual Basic


You can use a LabVIEW String Handle Pointer, a Pascal String Pointer, or a C String Pointer to pass an array from LabVIEW. For Microsoft Visual Basic, use the C String Pointer option.

When you pass a string using a C String Pointer, you also need to pass a length input for the output strings. Passing the length input tells Microsoft Visual Basic how many characters of the input string you want to pass to the VI or function. When you set up the parameter as a C String Pointer output within the Define VI Prototype panel in LabVIEW, LabVIEW automatically adds a Length Input parameter to the VI or function. For an output string, use the Length Output parameter. This parameter returns a pointer to an integer that indicates the number of characters returned from the function into the associated output string. The following function prototype is for a VI that has an input string and output string.

Note: Use standard calling conventions when you create DLLs for Microsoft Visual Basic.


[+] Enlarge Image



The first line of the Microsoft Visual Basic code that calls basicstring.dll should be written as follows:

Private Declare Sub Strtry Lib "C:\basicstring.dll" Alias "Basicstring" (ByVal I1 As String, ByVal O1 As String, ByVal len2 As Long)

The strings are passed by value. You cannot pass the strings by ref.

The code creates the following variables:

Dim InpString As String
Dim StringOut As String
Dim Val As Long

The actual call to the function is written as follows:

Call Strtry(InpString, StringOut, Val)

Refer to the  Using Microsoft Visual Basic to Call LabVIEW DLLs That Pass String Data Types topic linked below for an example of using Microsoft Visual Basic to send strings to a LabVIEW DLL.

Related Links:
Developer Zone: Using Microsoft Visual Basic to Call LabVIEW DLLs That Pass Numeric Data Types
Developer Zone: Using Microsoft Visual Basic to Call LabVIEW DLLs That Pass Booleans
Developer Zone: Using Microsoft Visual Basic to Call LabVIEW DLLs That Pass Array Data Pointers
Developer Zone: Using Microsoft Visual Basic to Call LabVIEW DLLs That Pass String Data Types
Developer Zone: Building DLLs in LabVIEW 6.x or later
Developer Zone: Calling a DLL from Microsoft Visual Basic That Was Generated By LabVIEW 6i
Using Arrays and Strings in the Call Library Function Node

11 ratings | 3.73 out of 5
Print

Reader Comments | Submit a comment »

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).