Calling a DLL in LabWindows™/CVI™: Explicit Linking vs. Implicit Linking (Dynamic vs. Static)

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:

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:

  1. Include the import library (.lib) in your LabWindows/CVI project
  2. Include the header file that contains the function prototype in your code using #include
  3. 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.

  1. 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
  2. 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. 
  3. 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.
  4. 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>
//Include windows.h to call the DLL dynamically. windows.h contains LoadLibrary and GetProcAddress .
#include <windows.h>

//Typedef the pointer to the exported function so you can call it easily later
//The function returns an int and has two inputs, an int and a string
typedef int (*MYPROC)(int, char*);

int main ()
{

int number = 5;
char* string = "Hello";
int returnValue;

HINSTANCE hinstLib; //Handle to the DLL
MYPROC ProcAddress; //Pointer to the function

hinstLib = LoadLibrary("SimpleDLL.dll");
//The the pointer to the exported function and typecast it so that we can easily call it
//MYPROC is typedef'ed above
//GetProcAddress is part of the Windows SDK and is declared in windows.h
ProcAddress = (MYPROC) GetProcAddress(hinstLib, "ShowMyNumberAndString");

//Call the function using the function pointer
returnValue = (ProcAddress)(number, string);

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

Was this information helpful?

Yes

No