在C++中,运行时加载函数是指在程序运行过程中动态加载和调用的函数。这种方法可以在不重新编译程序的情况下,实现对程序的扩展和升级。以下是一些常用的运行时加载函数的介绍:
以下是一个简单的示例,展示了如何在C++中使用LoadLibrary和GetProcAddress函数来动态加载和调用DLL中的函数:
#include<iostream>
#include<Windows.h>
typedef int (*AddFunc)(int, int);
int main()
{
HINSTANCE hDLL = LoadLibrary(TEXT("MyDLL.dll"));
if (hDLL == NULL) {
std::cout << "Failed to load DLL"<< std::endl;
return 1;
}
AddFunc add = (AddFunc)GetProcAddress(hDLL, "Add");
if (add == NULL) {
std::cout << "Failed to get function address"<< std::endl;
return 1;
}
int result = add(3, 4);
std::cout << "3 + 4 = "<< result<< std::endl;
FreeLibrary(hDLL);
return 0;
}
在上面的示例中,我们首先使用LoadLibrary函数加载了名为MyDLL.dll的DLL文件,然后使用GetProcAddress函数获取了DLL中名为Add的函数的地址,最后调用了该函数并输出了结果。
推荐的腾讯云相关产品:
产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云