在显示加载中,程序会在需要的时候才去加载DLL文件,获取到DLL文件中相关的函数入口地址,然后执行,执行完之后可以立即释放掉资源。显示加载具有更好的灵活性,能更加有效的使用内存,在编写大型程序时往往使用显示加载方式。
#include <Windows.h> #include <iostream> using namespace std; typedef int (*AddFunc) (int a, int b); typedef int (*SubFunc) (int a, int b); int main(int argc,char *argv[]) { int numb1=5, numb2=3; HMODULE hDll=LoadLibrary(L"DllTest.dll"); if(hDll!=NULL) { AddFunc add=(AddFunc)GetProcAddress(hDll,"Add"); SubFunc sub=(SubFunc)GetProcAddress(hDll,"Sub"); if(add) { cout<<numb1<<"+"<<numb2<<" = "<<add(numb1,numb2)<<endl; } if(sub) { cout<<numb1<<"-"<<numb2<<" = "<<sub(numb1,numb2)<<endl; } system("pause"); } FreeLibrary(hDll); return 0; }