动态链接库编程.docVIP

  • 4
  • 0
  • 约2.25千字
  • 约 4页
  • 2020-12-04 发布于四川
  • 举报
西华大学上机实践报告 课程名称:Windows系统编程 年级:2011 上机实践成绩: 指导教师:陈克力 姓名:徐千 上机实践名称:动态链接库编程 学号:312011080605334 上机实践日期:12_09 上机实践编号:实验10 组号:1 上机实践时间:08:30--10:00 一、目的 (1)了解动态链接库概念 (2)了解导入库概念 (3)了解DLL的符号解析和生成过程 (4)学习创建动态链接库项目的方法 (5)学习在DLL中添加导出函数的方法 (6)学习在DLL中添加导出类的方法 (7)学习加载和实用DLL的方法 内容与设计思想 (1)练习在DLL中添加导出函数 (2)练习在DLL中添加导出类 (3)练习使用加载时动态链接的方法应用DLL中的导出函数 (4)练习使用DLL中的导出库 (5)练习使用运行时动态链接的方法应用DLL中的导出函数 三、使用环境   WINDOWS 7 Microsoft Visual Studio 核心代码及调试过程 (1)练习在DLL中添加导出函数 添加导出函数SUM() extern C_declspec(dllexport) int sum(int a,int b) { return a + b; } 生成mylib.lib与mylib.dll两个文件 (2)练习在DLL中添加导出类 class _declspec (dllexport)CMath { public: CMath(void); ~CMath(void); int sum(int a ,int b); }; 生成mylib.lib与mylib.dll两个文件 练习使用加载时动态链接的方法应用DLL中的导出函数 创建一个控制台程序,将mylib.lib及mylib.dll文件复制到其项目目录下; 导出函数的声明及使用: extern Cint sum(int a,int b); int _tmain(int argc, _TCHAR* argv[]) { printf(4+3=%d\n,sum(4,3)); system(pause); return 0; } 导出类的声明及使用: extern Cclass _declspec (dllexport)CMath { public: CMath(void); ~CMath(void); int sum(int a ,int b); }; int _tmain(int argc, _TCHAR* argv[]) { CMath math; printf(4+3=%d\n,math.sum(4,3)); system(pause); return 0; } (5)练习使用运行时动态链接的方法应用DLL中的导出函数 typedef int (* PROCSUM)(int,int); int _tmain(int argc, _TCHAR* argv[]) { HINSTANCE hinstLib; PROCSUM ProcSum;//**************注意书上的错误:不是MYPROC************* BOOL fFreeResult; hinstLib = LoadLibrary(mylib.dll); if(hinstLib !=NULL) { wprintf (LLoadLibrary() is OK !\n); ProcSum = (PROCSUM) GetProcAddress(hinstLib,sum); if(ProcSum!=NULL) { printf(GetProcAddress is OK !\n); printf(4+3= %d\n,(ProcSum) (4,3)); } else { printf(GetProcAddress() failed,error %d\n,GetLastError()); fFreeResult = FreeLibrary(hinstLib); } } else printf(LoadLibrary() failed ,error %d\n,GetLastError()); system (Pause); return 0; } 总结 通过这次动态链接库的实验,了解了动态链接库是微软实现的共享库,其文件扩展名为.dll,也可以是.ocx和(系统驱动程序)。并且了解到了动态链接库导入库的扩展名是.

文档评论(0)

1亿VIP精品文档

相关文档