C++动态加载DLL调用方法
一、构建DLL路径
char szTmp[_MAX_PATH]; char* szPath = getcwd(szTmp, _MAX_PATH);//获取当前工作目录
//构建dll路径
strcat(szTmp, DLLNAME);
TCHAR wLocation[_MAX_PATH] = { 0 };
MultiByteToWideChar(0, 0, szTmp, _MAX_PATH, wLocation, _MAX_PATH * 2);
二、加载DLL
//dll资源加载 HINSTANCE hActive = LoadLibrary(wLocation);
三、调用DLL中函数
typedef void (_stdcall *pfnGetStatus)(long*); bool CLASSNAME::IsActive() { long lStatus = 0; pfnGetStatus Func = (pfnGetStatus)GetProcAddress(hActive, "GetStatus"); if(!Func) { // Error; } else { Func(&lStatus); } return lStatus; }
四、释放DLL
if(hActive != NULL) { FreeLibrary(hActive); }