Overview
This Tutorial explains the different ways to call a Dynamic Link Library (DLL) from LabWindows/CVI.
There are two methods of calling a function in a DLL:
- Implicit Linking
- Explicit Linking
An example of calling DLLs in both methods can be found here:
DeveloperZone Example: Calling DLLs in CVI: Explicit Linking vs. Implicit Linking (Dynamic vs. Static)
Implicit Linking
Also known as calling a DLL Statically or Load Time Linking. This refers to the process of calling a function in a DLL and linking to it implicitly. What this means is that when the application is built, the reference to the external function call is resolved through a import library (.lib file). The function can be called from the application like any other function because you #include the header file of the DLL that contains the function prototype.
The import library does not contain the actual code for the function, rather, it links to the DLL, i.e., it contains code to load the DLL as well as hooks to call the exported functions in the DLL. Many compilers will auto-generate the import library when you build a dll, including LabWindows/CVI and LabVIEW.
Calling a DLL statically is often easier and less prone to errors, however, you are restricted in that you must have an import library (.lib) while building your application (for the linking step). If your DLL has to change in the future, you will likely have to recompile your application with the new import library.
Calling a DLL using Implicit Linking in LabWindows/CVI
In order to call a DLL statically, you only need to do 3 things:
- Include the import library (.lib) in your LabWindows/CVI project
- Include the header file that contains the function prototype in your code using #include
- Call the function in your code like any other function
Explicit Linking
Also known as calling a DLL Dynamically or Run Time Linking. This refers to the process of calling a function in a DLL and linking / loading it explicitly during run time. What this means is that when the application is built, there is no import library and the DLL is not linked during this process. It is explicitly loaded into memory during run time by calling the Windows SDK function LoadLibrary.
After calling LoadLibrary, a pointer to the function to be called is then obtained by calling another Windows SDK function, GetProcAddress. As none of this happens until runtime, this is why this method is often referred to calling the DLL dynamically.
Calling a DLL dynamically requires a little more effort and must be done carefully as there is very little compile time error-checking. On the other hand, it offers much more flexibility as there is no dependency on the actual DLL during build time, and if the code for the DLL changes in the future, DLLs can be swapped without modifying your code.
Calling a DLL using Explicit Linking in LabWindows/CVI
In order to call a DLL dynamically, we use the Windows SDK functions. This process is very similar in other languages/environments as well.
- Define the function pointer using typedef:
First, we typedef the function pointer to match the prototype (parameters and return type) of the exported function. This lets us call the function with parameters later on.
For more information on typedef, please refer to: Wikipedia: typedef - Load the DLL into memory:
Use the LoadLibrary function (part of the Windows SDK, prototyped in windows.h) to load the DLL into memory. LoadLibrary returns a handle to the DLL.
Note: Loading a DLL using LoadLibrary will cause the DLLMain function in the DLL to execute. - Get a reference to the exported function:
Use the GetProcAddress function (part of the Windows SDK, prototyped in windows.h) to get the memory address of the function in the loaded DLL. We assign this to a function pointer. - Call the function using the function pointer:
Use the function pointer that you assigned in the previous step to call the exported function. You can pass in parameters and get return values as with a regular function since we typedef'ed it earlier.
|
#include <ansi_c.h> //Typedef the pointer to the exported function so you can call it easily later int main () int number = 5; return 0; } |
Additional References
For a more in-depth reference on when to use Implicit vs Explicit Linking, please refer to the following article:
MSDN: Determining Which Linking Method to Use
To learn more about where Windows searches for DLLs and what order it searches in, please refer to the following article:
KnowledgeBase 4U4EC9PE: Where Does Windows Search for DLLs?
An example of calling DLLs in both methods can be found here:
DeveloperZone Example: Calling DLLs in CVI: Explicit Linking vs. Implicit Linking (Dynamic vs. Static)
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/).
