2 // 3 4 #include "stdafx.h" 5 #include <windows.h> //需要包含windows.h 6 7 typedef int(*PFUNC_MathSub)(int,int); //定义函数指针 8 9 int _tmain(int argc, _TCHAR* argv[]) 10 { 11 HMODULE hmdll = ::LoadLibrary(_T("../Debug/DynamicLibrary.dll"));//动态加载dll 12 if(!hmdll) 13 { 14 printf("LoadDll is fail"); 15 }else 16 { 17 //获取动态库中的sub函数地址,强制类型转换为函数指针 18 PFUNC_MathSub pfMathSub = (PFUNC_MathSub)::GetProcAddress(hmdll,"MathSub"); 19 int nResult = pfMathSub(5,3); //通过函数指针进行调用 20 printf("5 - 3 = %d",nResult);
21 ::FreeLibrary(hmdll); 22 } 23 getchar(); 24 return 0; 25 }
原文:https://www.cnblogs.com/fzxiaoyi/p/12055883.html