From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.



在LabWindows/CVI程式中如何呼叫DLL函數,而無需將lib加入專案(Project中?



主要軟體: LabWindows/CVI Development Systems
主要軟體版本:
主要軟體修正版本:
次要軟體: N/A

問題:

我想在LabWindows/CVI中進行動態鏈結,但又不想將函式庫加入我的專案(Project)。請問應該怎麼做?



解答:

 

 

如果想在程式執行中呼叫指定的DLL函數,而不將任何DLL加入專案(Project),你需要使用Windows SDK函數 “LoadLibrary”“GetProcAddress”。以下是一段例子代碼:


// 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
#include

typedef VOID (*MYPROC)(LPTSTR);

VOID main(VOID)
{
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");
}



注意:
LabWindows/CVI中,所有的SDK函數都是以DLL的形式存在的。LabWindows/CVI與其外部編譯器都包含了部分SDK函數的DLL。大部分常用的SDK函數都包含在以下四個lib中:

kernel32.lib
gdi32.lib
user32.lib
advapi.lib

LabWindows/CVI
在啟動時會自動載入這4lib,並在鏈結時進行搜索以解析引用內容。因此,你不必將這些lib加入專案(Project)

如果 LabWindows/CVI 鏈結器報告SDK函數呼叫失敗,你就必須將相關lib加入專案(Project)。參考SDK具體函數的Help,判斷是否要將lib加入專案(Project)
更多關於使用Windows SDK函數的資訊可以到Microsoft Developer Network(見相關鏈結)尋找。



相關連結: Microsoft Developer Network

附加檔案:





報告日期: 10/19/2001
最後更新: 07/04/2007
文件偏號: 2EIBT1Y1