Building a DLL with LabWindows/CVI 8.5 for use with LabVIEW 8.5
Overview
This tutorial provides step-by-step instructions for creating a Dynamic Link Library (DLL) in LabWindows/CVI 8.5 and using the DLL in LabVIEW 8.5. For demonstration purposes, a simple library is created. The demonstration library contains four functions: add, subtract, multiply and divide. Coding will vary with your actual library. This tutorial was written for software installed on a Windows XP operating system. Exact steps may vary on a different operating system.
Step 1: Code the DLL in LabWindows/CVI
- Start LabWindows/CVI.
- Start » Programs » National Instruments » LabWindows CVI 8.5 » NI LabWindows CVI 8.5.

- Create a new project.
- From the LabWindows/CVI 8.5 splash screen, click Project in the New section.
- File » Save Untitled.prj As...

- Save the project to a project folder. The project folder will contain all build files, including the DLL created by the compiler. In this example, the project name is Math and the project folder is MathDLL. Be sure to save the project with a .prj extension.

- Create a new source file.
- File » New » Source (*.c)...

- File » Save Untitled1.c As...

- Save the source file to the project folder. In this example, the source file name is Math and the project folder is MathDLL. Be sure to save the source file with a .c extension.

- Add the source file to the project. File » Add <File Name>.c to Project.
_20080428124251.png)
- Code the source file. In this example, five functions are shown. Notice that DLLHeader function has the __stdcall keyword before it. This keyword defines the calling convention for the function. Microsoft Windows contains two calling conventions: the C calling convention, denoted by __cdecl, and the standard calling convention, formerly the Pascal calling convention, denoted by __stdcall. Both keywords are prefixed by a two underscores.

- Create a new header file.
- File » New » Include (*.h)...

- File » Save Untitled1.h As...

- Save the header file to the project folder. In this example, the header file name is Math and the project folder is MathDLL. Be sure to save the header file with a .h extension.

- Add the header file to the project. File » Add <File Name>.h to Project.

- Code the header file. In this example, function prototypes for the four math functions are coded. Notice that each prototype is followed by a semicolon.

- Save the project files.
- File » Save All.

Step 2: Create the DLL in LabWindows/CVI
- Set the target type.
- Build » Target Type » Dynamic Link Library.

- Configure the target settings.
- Build » Target Settings...

- From the Target Settings configuration box...

- ...click the Import Library Choices... box. In the DLL Import Library Choices box, select the Generate import libraries for both compilers radio button and click OK.

- From the Target Settings configuration box, click the Change box in the Exports section.
- In the DLL Export Options box, highlight the header file and click OK.

- Set the build configuration.
- Build » Configuration » Release.

- Build.
- Build » Create Release Dynamic Link Library.

- Verify that the DLL built in the project directory. Also verify that .LIB files were created in the project directory, in the Microsoft Visual C (msvc) directory, and in the Borland directory.

Step 3: Use the DLL in LabVIEW
- Create a blank VI in LabVIEW.
- Start » Programs » National Instruments LabVIEW 8.5.
- Click Blank VI from the New section of the LabVIEW 8.5 splash screen.

- Place a Call Library Function Node on the block diagram.
- Right-click the block diagram » Connectivity » Libraries & Executables » Call Library Function Node. If the Connectivity pallet does not appear when you right-click the block diagram, click the search box in the top right hand corner of the Functions pallet and search for Call Library Function Node.

- After you place the Call Library Function Node, double-click its icon on the block diagram to configure it.

- Configure the Call Library Function Node.
- There are four tabs in the Call Library Function configuration box: Function, Parameters, Callbacks and Error Checking.

- From the Function tab, click the folder icon next to the Library name or path box. Use the browser to select the DLL created in Steps 1 and 2.

- Using the Function name drop-down box, select the function name. In this example, we select the multiply function. In the Thread box, select Run in UI thread. This is the correct selection for most DLL functions. If the function was coded to be thread safe, i.e. the function is reentrant, you may select Run in any thread. Select stdcall (WINAPI) for the demonstration library. For more information, click here.

- Switch to the Parameters tab. From this tab, you will define the function prototype. Recall the prototypes defined in the header file. In this example, we will configure the prototype for the multiply function. Change the Type for the return type from Void to Numeric. Change the Data type to 4-byte Single.

- Click the plus icon to add input parameters. Add two input parameters, and change the data type for each to Numeric, 4-byte single. Pass the data by Value. Click OK to close the Call Library Function configuration box.

- Wire the Call Library Function Node.
- After the Call Library Function Node is configured, you can hover the mouse over the input and output ports to see the function argument that connects to the node.

- Wire the input port(s).

- Wire the output port.

- Run the VI.

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