NI Home
Cart Cart | Help
Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Using Existing C Code or a DLL in LabVIEW

VERSION 5 Published

Created on: May 12, 2008 1:55 PM by Kevin_S - Last Modified:  Jun 7, 2008 8:14 PM by Kevin_S

This tutorial explains how to convert existing C code to a DLL (Dynamically Linked Library) and then how to call that DLL from LabVIEW. If you already have a DLL, start on step 10. (Note: This tutorial explains how to create a DLL that can only be used in LabVIEW. If you want to create a DLL out of your C code that will then be downloaded to the FIRST cRIO controller, see this article.)

1. Use an integrated development environment capable of creating DLLs. (This tutorial uses Microsoft Visual Studio 2005.)

2. Create a new project.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1217/clip_image002.jpg

3. Create a C++ Win32 Console Application. Specify a Name. Specify a Location.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1218/clip_image003.jpg

4. Click Next for more options.

5. Select DLL from Application type.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1219/clip_image005.jpg

6. Add your function to the default program. (See my function in bold.)
int Multiply( int a, int b )
{
return a*b;
}

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1220/clip_image006.jpg

7. Add the following prototype to the default program. (See my prototype in bold.)
extern "C" __declspec(dllexport)int Multiply( int a, int b );

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1221/clip_image007.jpg

8. Build the DLL.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1222/clip_image008.jpg

9. Note where your DLL was created.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1223/clip_image009.jpg

10. Open LabVIEW and place a Call Library Function Node on the Block Diagram.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1224/clip_image010.jpg

11. Double click on the Node and specify the DLL to load and the function to access.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1225/clip_image011.jpg

12. Under the Parameters tab, specify the Return Type (the data being passed back from the function) and press Add a parameter in order to specify each of your input parameters.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1226/clip_image012a.jpg http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1227/clip_image012b.jpg
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1228/clip_image012c.jpg

13. Right click on each of the 3 outputs and create an indicator. (Note: You have the option of viewing as outputs each of the input parameters to the DLL.)
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1229/clip_image013a.jpg

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1230/clip_image013b.jpg http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1231/clip_image013c.jpg

14. Right click on each of the 2 inputs and create controls.

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1232/clip_image014a.jpg

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1233/clip_image014b.jpg http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1234/clip_image014c.jpg

15. Go to the Front Panel. Specify values for the 2 controls (the input parameters).
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1235/clip_image015.jpg

16. Run the VI in order to execute the DLL.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1236/clip_image016.jpg


Average User Rating
(0 ratings)




May 14, 2008 9:18 PM Click to view Rob Steele's profile Rob Steele says:

Will this same method work with something like bloodshed's (http://www.bloodshed.net/) C or C++ compiler. I've used that compiler in the past and it has worked very well - and it is free.

May 15, 2008 10:12 PM Click to view Kevin_S's profile Kevin_S says:

Hi Rob,
There are many different Integrated Development Environments (IDE) that can create DLLs capable of being called from LabVIEW. I would recommend trying it out with your specific environment. Just know that if you are using an IDE like Microsoft Visual Studio 2003 or newer, be sure to create a C DLL (an unmanaged DLL) and not a .NET DLL (a managed DLL).

Regards,
Kevin S.
Applications Engineer
National Instruments