动态加载Dll

来源:互联网 发布:python快速建站 编辑:程序博客网 时间:2024/05/16 08:08

现在有一个动态链接库“UserDef.dll”,里面有个函数原形为 int GetSelect(int nItem),请你用代码实现动态调用(显式调用)(包含文件Sample.Cpp,Sample.h,UserDef.dll已经在Windows目录下)
答:先在Sample.h中定义 typedef int(*pUserDll)(int);
在需要调用此函数前写入代码:
HINSTANCE  hModule = LoadLibrary(_T(“UserDef.dll”);//加载DLL
If(NULL == hModule)
{
 AfxMessageBox(_T(“Load Error”);
 //进行错误处理
}
pUserDll pFun = (pUserDll) GetProcAddress(hMoudle,_T(“GetSelect”));
if(NULL == pFun)
{
AfxMessageBox(_T(“Load Function Error”);
 //进行错误处理
}
Else
{
 //可以进行函数调用
 Int i = 1;
 pFun(i);
}
//最后记得要释放dll
FreeLibrary(hMoudle);
原创粉丝点击