循序渐进DLL编程(三)

 在静态可以加载库文件的同时也可以进行动态加载库文件,可以通过loadlibrary和GetProcessAdd等方式来获取函数的地址

#include<stdio.h>
#include<windows.h>
#include"test.h"


typedef int (_stdcall * AddProc)(int , int );//定义函数指针类型
HINSTANCE hInst;

int main()
{
int a=1;
int b=5;
 
hInst=::LoadLibrary(TEXT("DLL2.dll"));//动态加载Dll
 
 AddProc Add=(AddProc)GetProcAddress(hInst,"Add");//获取Dll的导出函数
 
 if(!Add)
 {
  printf("获取Add函数地址失败!");
 }
 
printf("the result is %d\n",Add(a,b));
 
 ::FreeLibrary(hInst);//释放Dll函数



    getchar();
    return 0;
}

通过上述的方式可以动态加载dll文件

原文地址:https://www.cnblogs.com/OneDream/p/2878895.html