全方位解读DLL文件—调用DLL
程序员文章站
2022-03-04 19:03:58
...
显式链接调用
调用之前 MSVC 生成的 mydll_vc.dll 中的函数
#include <stdio.h>
#include <windows.h>
int main(){
HINSTANCE hMyDLL = NULL; // typedef HINSTANCE HMODULE;
int (*MyAdd)(int,int);
hMyDLL = LoadLibrary("mydll_vc.dll");
if(hMyDLL == NULL){
printf("Can not open this file!\n");
}
MyAdd = (int (*)(int,int))GetProcAddress(hMyDLL,"myadd");
printf("%d\n",MyAdd(99,999));
return 0;
}
调用之前 MinGW 生成的 mydll_gcc.dll 中的函数
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
typedef int (*AddFunc)(int,int);
typedef int (*SubFunc)(int,int);
typedef int (*MulFunc)(int,int);
typedef int (*DivFunc)(int,int);
int main(){
int a=66,b=6;
HMODULE hDLL = LoadLibrary("mydll_gcc.dll");
if(hDLL != NULL){
AddFunc add = (AddFunc)GetProcAddress(hDLL, "add");
SubFunc sub = (SubFunc)GetProcAddress(hDLL, "sub");
MulFunc mul = (MulFunc)GetProcAddress(hDLL, "mul");
DivFunc div = (DivFunc)GetProcAddress(hDLL, "div");
if(add != NULL){
printf("a+b=%d\n",add(a,b));
}
if(sub != NULL){
printf("a-b=%d\n",sub(a,b));
}
if(mul != NULL){
printf("a*b=%d\n",mul(a,b));
}
if(div != NULL){
printf("a/b=%d\n",div(a,b));
}
}else{
printf("Load Failed!\n");
}
FreeLibrary(hDLL);
system("pause");
return 0;
}