在沒有 Import Library的情況下存取LabWindows™/ CVI™程式中的DLL函數

更新 Oct 1, 2024

我希望在LabWindows™/ CVI中進行run-time dynamic linking,但是我不想在project中明確包含import library。我可以怎麼設定呢?

如果不想在project中明確包含import library,但是又希望在LabWindows™/ CVI中進行run-time dynamic linking的方法,你必須在run-time時使用Windows SDK functions LoadLibrary和GetProcAddress去存取特定的DLL函數,以下是在run-time時存取DLL中函數的範例程式:

// 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"); 
}