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函数,而无需将库文件加入工程文件(project)中?



主要软件: LabWindows/CVI Development Systems
主要软件版本: N/A
主要软件修正版本: N/A
次要软件: N/A

问题: 我想在LabWindows/CVI中进行动态链接,但又不想将函数库加入我的工程文件。请问应该怎么做?

解答: 如果想在程序执行中调用指定的DLL函数,而不将任何DLL加入工程文件,你需要使用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函数都包含在以下四个引导库中:

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

LabWindows/CVI 在启动时会自动加载这四个库,并在链接时进行搜索以解析引用内容。因此,你不必将这些引导库加入工程文件。

如果 LabWindows/CVI 链接器报告SDK函数引用失败,你就必须将相关引导库加入工程文件。参考SDK具体函数的帮助,判断是否要将引导库加入工程文件。引导库文件都在cvi\sdk\lib目录下。

更多关于使用Windows SDK函数的信息可以到Microsoft Developer Network(见相关链接)寻找。

相关链接: Microsoft Developer Network


附件:





报告日期: 10/19/2001
最近更新: 05/30/2005
文档编号: 2EIBT1Y1