Example Code

Using Existing C Code or a DLL in LabVIEW

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.

3. Create a C++ Win32 Console Application. Specify a Name. Specify a Location.

4. Click Next for more options.

5. Select DLL from Application type.

6. Add your function to the default program. (See my function in bold.)

int Multiply( int a, int b )

{

return a*b;

}

7. Add the following prototype to the default program. (See my prototype in bold.)

extern "C" __declspec(dllexport)int Multiply( int a, int b );

8. Build the DLL.

9. Note where your DLL was created.

10. Open LabVIEW and place a Call Library Function Node on the Block Diagram.

11. Double click on the Node and specify the DLL to load and the function to access.

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.

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

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

15. Go to the Front Panel. Specify values for the 2 controls (the input parameters).

16. Run the VI in order to execute the DLL.

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
parkcs
Member
Member
on

When I follow this exactly, I get "error LNK2005: _DllMain@12 already defined in dllmain.obj" error. Any help would be appreciated. I need C++ code to read data being collected by LabVIEW and write them into a new file.

sym2012go
Member
Member
on

In step 6 or 7, some lines of the code are not necessary:

123.jpg

delete the above code and it will be OK.

San_Punt
Member
Member
on

nice example, thanks

Contributors