要在MFC上面使用动态库,新建MFC AppWizard (dll),选择中间的dll选项。
首先在.h文件中声明函数
extern "C" _declspec(dllexport) int TestFunction(int arg1,int arg2);
然后在.cpp文件中写明函数主体
int TestFunction(int arg1,int arg2)
{
return 1;
}
进行编译,生成.dll文件和.h文件。
那么在要调用的时候,把.dll拷贝到所需要使用该文件的工程目录下。
HINSTANCE hDll;
hDll = LoadLibrary("CourseTwoFunc.dll");
typedef int (*Func)(int arg1,int arg2);
Func TestFunction = (Func)GetProcAddress(hDll,"TestFunction");
if(1 == TestFunction(1,2))
MessageBox("hello dll");
FreeLibrary(hDll);
按照上述结构使用函数即可。记住加载之后必须要释放。