There are two methods of calling a function in a DLL:
An example of calling DLLs in both methods can be found here:
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.
In order to call a DLL statically, you only need to do 3 things:
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.
In order to call a DLL dynamically, we use the Windows SDK functions. This process is very similar in other languages/environments as well.
#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; } |
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