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

Document Type: Tutorial
NI Supported: Yes
Publish Date: Dec 6, 2006

Calling a DLL from Microsoft Visual Basic That Was Generated By LabVIEW 6i

19 ratings | 3.95 out of 5
Print
LabVIEW can create DLLs that can be used in other programming environments. This document demonstrates how to use Visual Basic to call a simple function in a DLL that was generated by LabVIEW 6i. The first two steps in this process are to create the VI and then create a DLL from LabVIEW. The document linked at the bottom of the page shows how this is done.

1. Open a new project for Visual Basic by selecting File>>New Project.


[+] Enlarge Image

2. Select the Standard EXE project and click OK.



3. Write the following code, which corresponds to the object front panel.

Note: To view the top line in full, see Step 5.


[+] Enlarge Image

4. The boxes that contain the degrees F and degrees C are Textboxes, and the Start and Quit buttons are CommandButtons. Create them by double-clicking their respective symbols in the left front panel.


[+] Enlarge Image


5. The top line of the code is the most critical to successfully calling the LabVIEW DLL that you created. The line in its entirety reads as follows:

Private Declare Sub DegFtoDegC Lib "C:\Temp\app\Convert_Temp.dll" Alias "F_to_C" (ByVal degF As Double, ByRef degC As Double)


-DegFtoDegC is the name that you use to call your function from the Visual Basic code, which is demonstrated above. “Call DegFtoDegC(DegreeF, DegreeC)”


-"C:\Temp\app\Convert_Temp.dll" is the location of the DLL that you created using LabVIEW. Recall that you set this up as the destination directory. (See below)


[+] Enlarge Image

-"F_to_C" is the Alias name of your function. This Alias name corresponds to the function name that was on the Define VI Prototype front panel when you created the DLL in LabVIEW.

[+] Enlarge Image


-(ByVal degF As Double, ByRef degC As Double) As Double defines the parameters that correspond to the function that you are calling from the DLL. Recall that your function prototype was "float64 (float64 DegF, float64 *DegC)” (see above). Therefore, degF is specified as being passed by value (ByVal) since it is defined as a float64 value in your function prototype. DegC is specified as being passed by reference (ByRef) since it is defined as a pointer to a value in your function prototype. Remember you can also return a value, so in this case the Celsius temperature is returned from the function as a float64 or Double.

Note: The variable names in your declaration of the function in Visual Basic are simply place holders, so they do not have to match the names in the function prototype. However, they must be the same type.


6. The main code for the Visual Basic program reads as follows:


Private Sub Start_Click()
Dim DegreeF As Double
Dim DegreeC As Double
Dim ret As Double

DegreeF = FTextBox.Text
ret = DegFtoDegC(DegreeF, DegreeC)
CTextBox.Text = DegreeC
End Sub

Private Sub Quit_Click()
End
End Sub

-This simply defines two Double variables and places the value that is in the FTextBox in the DegreeF input variable. It calls the function that places the Celsius temperature in the DegreeC output variable that corresponds to the Fahrenheit temperature in the DegreeF input variable. It also returns the Celsius temperature into the variable "ret". Then the Celsius output temperature is placed in the CTextBox. It repeats this process each time you press the Start button. The program ends when you press the Quit button. The final object front panel looks like the following illustration when the program runs:




-The link below shows how the DLL called in this Microsoft Visual Basic example was created in LabVIEW 6i.

Related Links:
Creating DLLs Using LabVIEW 6i

19 ratings | 3.95 out of 5
Print

Reader Comments | Submit a comment »

yes,function should take the place of sub
- henrywang@astec-asia.com - Jul 30, 2007

Good basic article
The article takes you that step further you need when starting from scratch and having a LabView dll that works i Visual Basic. It is a litle confusing arround declring the dll function to call because it is declared as a sub procedure with no return value and the function in the dll used is build as a function with a return value therefore the proper Visul Basic declaration should be: Private Declare Function DegFtoDegC Lib "\Convert_Temp.dll" Alias "F_to_C" (ByVal degF As Double, ByRef degC As Double) As Double
- Jul 2, 2002

 

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/).