Creating a Dynamic Link Library (DLL) in LabWindows/CVI 8.5 for use with LabVIEW 8.5.
Primary Software: LabWindows/CVI Development Systems>>Base Package
Primary Software Version: 8.5
Primary Software Fixed Version: N/A
Secondary Software: LabVIEW Development Systems>>Base Package
Problem: I want to build a DLL in LabWindows/CVI and use it in LabVIEW. What is the process and what are the settings I should apply to my LabWindows/CVI project?
Solution: The solution is divided into four steps:
Code the DLL in LabWindows/CVI,
Create the DLL in LabWindows/CVI,
Configure a Call Library Function Node, and
Use the DLL in LabVIEW.
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)...

Save the source file.
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.
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. See Knowledge Base 2RCKK3TL: Calling Conventions in LabWindows/CVI for more information.

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 the DLL files.
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: Configure a Call Library Function Node
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 file 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.
Step 4: Use the DLL in LabVIEW
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.

Related Links: Knowledge Base 2RCKK3TL: Calling Conventions in LabWindows/CVI
Attachments:
Report Date: 05/08/2008
Last Updated: 05/16/2008
Document ID: 4L7AJ7YP