Accessing DLL Functions in LabWindows™/CVI™ Program Without the Import Library

Updated Oct 1, 2024

Environment

Software

  • LabWindows/CVI

I wish to do run-time dynamic linking in LabWindows™/CVI, but I don't want to explicitly include the import library in my project. How can I set this up?

You must use the Windows SDK functions LoadLibrary and GetProcAddress to access specific DLL functions at run-time without including any DLL libraries in your project. The following is sample code for accessing a function within a DLL at run-time. 

// File: RUNTIME.C 
// Sample code that uses LoadLibrary and GetProcAddress to access myFunction from MYDLL.DLL
// You will need to include stdio.h and windows.h in your project to use this code 

#include <stdio.h> 
#include <windows.h> 

typedef void (*MYPROC)(LPTSTR); 

void main() 

    HINSTANCE hinstLib; 
    MYPROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

// Get a handle to the DLL module. 
 
hinstLib = LoadLibrary("mydll"); 
// If the handle is valid, try to get the function address. 

    if (hinstLib != NULL) 
    { 
       ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myFunction"); 

             // If the function address is valid, call the function. 

       if (fRunTimeLinkSuccess = (ProcAdd != NULL)) 
            (ProcAdd) ("message via DLL function\n"); 

             // Free the DLL module 

       fFreeResult = FreeLibrary(hinstLib); 
    } 

    // If unable to call the DLL function, use an alternative 

    if (! fRunTimeLinkSuccess) 
      printf("message via alternative method\n"); 
}