创建动态库和使用动态库【VC6.0】

来源:互联网 发布:经济学专业 知乎 编辑:程序博客网 时间:2024/06/11 04:31

要在MFC上面使用动态库,新建MFC AppWizard (dll),选择中间的dll选项。

首先在.h文件中声明函数

extern "C" _declspec(dllexport) int TestFunction(int arg1,int arg2);  

然后在.cpp文件中写明函数主体

intTestFunction(int arg1,int arg2){return 1;}

进行编译,生成.dll文件和.h文件。

那么在要调用的时候,把.dll拷贝到所需要使用该文件的工程目录下。

HINSTANCEhDll;hDll=LoadLibrary("CourseTwoFunc.dll");typedefint(*Func)(int arg1,int arg2);Func TestFunction=(Func)GetProcAddress(hDll,"TestFunction");if(1==TestFunction(1,2))MessageBox("hello dll");FreeLibrary(hDll);

按照上述结构使用函数即可。记住加载之后必须要释放。