Saturday, April 30, 2011

DLL and CVI

   
Written by Tal Alon - April 2011

Knowing how to create DLL files and how to use them is a must for many programmers.

In this post we present the simple CVI procedure of .DLL creation and several simple ways of using DLLs (with examples of course).

To fully understand and implement the below instructions you need to know how to work with multiple files within your projects.

A tutorial on DLL's from National Instruments (CVI) can be found here : CVI DLL Tutorial.

To create a DLL in CVI you should:

1) Open a new dedicated CVI project.
2) Write down the DLL code in a new source file (.c)
3) In the menu bar click Build-->Target Type and Select Dynamic Link Library.
4) Compile the source file.
5) Create a header file that will contain the prototypes of the functions you want to export (allow others to use).You can do this quickly by clicking Build-->Generate Prototypes.
Tip - When you are writing the source file you can use the static keyword before a function to prevent it's inclusion in the automatically generated header file (and the use of this function).
6) Add the newly created header file to your project (Edit-->Add Files to Project).
7) Build-->Configuration-->Release (0x86) if you don't want a debug version.
8) Click Build-->Target Settings and in the window that opens click the Change button in the "Exports" area and mark the header file with the exported functions.
9) Build-->Create Release Dynamic Link Library.

The last step will build your project and create two files xxx.dll and xxx.lib (replace xxx with your project's name).

One way to use a DLL in another CVI application:

1) Transfer the xxx.h xxx.lib and xxx.dll files to the new application folder.
2) Add the xxx.lib file to your project (Edit-->Add Files to Project). (I recommend to add xxx.h and xxx.dll as well).
3) Include the header file xxx.h using the preprocessor's #include command.
4) Use the functions (prototypes can be seen in xxx.h) as you see fit.

2nd way to use a DLL
(there is actually no reason to use this method if you can use the next one)

It is possible to use a function from a DLL file without including its header file or adding the LIB file to the project before compiling - this use of the DLL file is dynamic, loading it only when using one of its functions and then unloading.

It is important to remember that in this case the LIB and DLL files should still be accessible, even if they were not added to the project before build.

Here is a snippet of code (click to enlarge) showing the callback function of a button. The application accesses the DLL (via the LIB import library) only when the button is pushed, uses one of the DLL's functions and then unloads the DLL:


The CVI functions LoadExternalModule and GetExternalModuleAddr can be found in:

Library-->Utility-->External Modules.

3rd way to use a DLL

It is also possible to address the DLL directly and dynamically, without ever using the LIB file, by using Win32 API. To use this method you first have to #include <windows.h> before all other included header files.

Here is a snippet of code (click to enlarge) showing the callback function of a button. The application accesses the DLL directly (no LIB) only when the button is pushed, uses one of the DLL's functions and then unloads the DLL:



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There are many other cool things you can do with libraries and DLL files (like running a GUI from inside a DLL, or bidirectional sharing), and I omitted some important things just for the sake of simplicity, so read about DLLs on the net or wait for the next DLL post here.

2 comments:

  1. This helped me hugely. thank you.

    ReplyDelete
  2. step 9 in the tutorial do not exist.is there a another way to create release dynamic link library?

    ReplyDelete