




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、综合实验一 用所学的内容编写一个动态链接库的文件1 实验目的(1) (1) 了解和掌握类和指向函数的指针的使用。(2) (2) 了解和掌握宏的使用,(3) (3) 掌握动态链接库文件创建的过程。2 实验要求 熟练掌握动态链接库文件的创建3 实验步骤与内容1.启动Visual C+6.0或者C+ Builder 5.0 .在WINDOWS98或WINDOWS 2000环境下,找到Vi
2、sual C+6.0或者C+ Builder 5.0图标,双击之。为了不使Visual C+6.0或者C+ Builder 5.0的默认搜索路径与WINDOWS其他软件相冲突,清修改Visual C+6.0或者C+ Builder 5.0图标的属性。在属性|程序|批处理一栏中填上你自己的批处理程序名。以便启动时,首先运行你的批处理程序。然后在该批处理程序中,写上path.路径。2.设置用户程序子目录 设置用户程序子目录的目的是,将所有编程时产生的中间文件和最终执行程序文件全部放在自己的目录中,以便管理。 3.创建和输入程序 Visual C+6.0启动后,要先建立一个project工程文件。方
3、法为: ·选择File|new 菜单项,将弹出New 对话框。 ·单击Projects 选项卡 ·在Location中填用户子目录路径 ·在Project name中填入工程名(如MyDll) ·在列表中选择MFC AppWizard(dll),表示你编制的应用程序将生成动态链接库文件(.dll) ·按照提示创建一个自己想要的工程 ² ² 定位到mydll.h文件处,添加动态链接库的函数原型声明,下面为mydll.h的内容: / M
4、yDll.h : main header file for the MYDLL DLL/ #if !defined(AFX_MYDLL_H_2D7DF429_D790_11D6_93F3_50784C6323E6_INCLUDED_)#define AFX_MYDLL_H_2D7DF429_D790_11D6_93F3_50784C6323E6_INCLUDED_ #if _MSC_VER > 1000#pragma once#endif / _MSC_VER > 1000 #ifndef _AFXWIN_H_#error include 's
5、tdafx.h' before including this file for PCH#endif #include "resource.h"/ main symbols / CMyDllApp/ See MyDll.cpp for the implementation of this class/int DrawTree(CPaintDC *dc,int xStart,int yStart,double length,double angle, int num);class CMyDllApp : public CWinApppublic:CM
6、yDllApp(); / Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CMyDllApp)public:virtual BOOL InitInstance();/AFX_VIRTUAL /AFX_MSG(CMyDllApp)/ NOTE - the ClassWizard will add and remove member functions here./ DO NOT EDIT what you see in these blocks of generated code
7、!/AFX_MSGDECLARE_MESSAGE_MAP(); / /AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line. #endif / !defined(AFX_MYDLL_H_2D7DF429_D790_11D6_93F3_50784C6323E6_INCLUDED_)² ²
8、60; 定位到mydll.cpp 文件处,添加动态链接库中的函数的实现部分,下面为mydll.cpp的内容: / MyDll.cpp : Defines the initialization routines for the DLL./ #include "stdafx.h"#include "MyDll.h"#include <math.h> #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif
9、60;/Note!/If this DLL is dynamically linked against the MFC/DLLs, any functions exported from this DLL which/call into MFC must have the AFX_MANAGE_STATE macro/added at the very beginning of the function./For example:/extern "C" BOOL PASCAL EXPORT ExportedFunction()/AFX_MANAGE_STATE(AfxGet
10、StaticModuleState();/ normal function body here/It is very important that this macro appear in each/function, prior to any calls into MFC. This means that/it must appear as the first statement within the /function, even before any object variable declarations/as their constructors may generate calls
11、 into the MFC/DLL./Please see MFC Technical Notes 33 and 58 for additional/details./ / CMyDllApp BEGIN_MESSAGE_MAP(CMyDllApp, CWinApp)/AFX_MSG_MAP(CMyDllApp)/ NOTE - the ClassWizard will add and remove mapping macros here./ DO NOT EDIT what you see in these blocks of generated code!/AFX_MS
12、G_MAPEND_MESSAGE_MAP() / CMyDllApp construction CMyDllApp:CMyDllApp()/ TODO: add construction code here,/ Place all significant initialization in InitInstance / The one and only CMyDllApp object CMyDllApp theApp;int DrawTree(CPaintDC *dc,int xStart,int yStart,double length,double
13、 angle, int num)int xEnd,yEnd;if (num=0) return 1;xEnd=xStart+(int)(length*cos(angle);yEnd=yStart+(int)(length*sin(angle);dc->MoveTo(xStart,yStart);dc->LineTo(xEnd,yEnd);DrawTree(dc,xEnd,yEnd,length*0.6,angle+0.624,num-1);DrawTree(dc,xEnd,yEnd,length*0.85,angle+0.08,num-1);DrawTree(dc,xEnd,yEn
14、d,length*0.65,angle-0.6,num-1);return 1; / CMyDllApp initialization BOOL CMyDllApp:InitInstance()if (!AfxSocketInit()AfxMessageBox(IDP_SOCKETS_INIT_FAILED);return FALSE; / Register all OLE server (factories) as running. This enables the/ OLE libraries to create objects from other appl
15、ications.COleObjectFactory:RegisterAll(); return TRUE; / Special entry points required for inproc servers STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)AFX_MANAGE_STATE(AfxGetStaticModuleState();return AfxDllGetClassObject(rclsid, riid, ppv); STDAPI DllCanUnl
16、oadNow(void)AFX_MANAGE_STATE(AfxGetStaticModuleState();return AfxDllCanUnloadNow(); / by exporting DllRegisterServer, you can use regsvr.exeSTDAPI DllRegisterServer(void)AFX_MANAGE_STATE(AfxGetStaticModuleState();COleObjectFactory:UpdateRegistryAll();return S_OK; ² ² &
17、#160; 在mydll.def中引出DrawTree函数,下面为mydll.def的内容。 ; MyDll.def : Declares the module parameters for the DLL. LIBRARY "MyDll"DESCRIPTION 'MyDll Windows Dynamic Link Library' EXPORTS ; Explicit exports can go hereDrawTreeDllCanUnloadNow PRIVATEDllGetClas
18、sObject PRIVATEDllRegisterServer PRIVATEl l 编译程序 思考问题² ² 程序中大小写用错了,结果会怎样?² ² 如果返回类型void没有,结果会怎样,是否需要return语句?² ²
19、160; 编译中若有警告信息,影响程序运行吗?² ² 如何编写动态链接库文件以及步骤? 综合实验二 动态链接库文件的调用1 实验目的(1) (1) 解和掌握类和指向函数的指针的使用。(2) (2) 了解和掌握宏的使用。(3) (3) &
20、#160; 掌握如何显示调用动态链接库文件。(4) (4) 掌握如何隐示调用动态链接库文件。(5) (5) 调用动态链接库文件的步骤。2 实验要求熟练掌握在自己的应用程序中调用动态连接库文件的方法3 实验步骤与内容1) 1) 启动Visual C+6.0或者C+ Builder 5.0 .在WINDOWS98或WINDOWS 2000环境下,找到Visual C+6.0
21、或者C+ Builder 5.0图标,双击之。为了不使Visual C+6.0或者C+ Builder 5.0的默认搜索路径与WINDOWS其他软件相冲突,清修改Visual C+6.0或者C+ Builder 5.0图标的属性。在属性|程序|批处理一栏中填上你自己的批处理程序名。以便启动时,首先运行你的批处理程序。然后在该批处理程序中,写上path.路径。2)设置用户程序子目录 设置用户程序子目录的目的是,将所有编程时产生的中间文件和最终执行程序文件全部放在自己的目录中,以便管理。 3)创建和输入程序 Visual C+6.0启动后,要先建立一个project工程文件。方法为: ·
22、选择File|new 菜单项,将弹出New 对话框。 ·单击Projects 选项卡 ·在Location中填用户子目录路径 ·在Project name中填入工程名 ·在列表中选择MFC AppWizard(exe),表示你编制的应用程序将生成可执行文件(.exe) ·按照提示创建一个自己想要的工程 4)调用步骤² ² 隐示链接包含导出函数(或C+类)声明的头文件(.h)导入库(.lib)文件实际的DLL(.dll)文件²
23、78; 显示链接 显示链接时,使用DLL的可执行程序在运行时通过函数调用来显示加载或卸载 DLL,并通过函数指针来调用DLL的导出函数,要显示链接DLL,应用程序必须调用LoadLibrary来加载DLL,并获取模块句柄,调用GetProcAddress来获取应用程序要调用的导出函数的指针,使用完DLL后,应调用FreeLibrary来卸载DLL. 5)主要程序代码 (1)定位到TestDllView.h文件处,添加函数原型声明,下面为TestDllView.h的内容: / TestDllView.h : in
24、terface of the CTestDllView class/ #if !defined(AFX_TESTDLLVIEW_H_CFCBD8ED_D7AF_11D6_93F3_50784C6323E6_INCLUDED_)#define AFX_TESTDLLVIEW_H_CFCBD8ED_D7AF_11D6_93F3_50784C6323E6_INCLUDED_ #if _MSC_VER > 1000#pragma once#endif / _MSC_VER > 1000 class CTestDllView : public CVi
25、ewprotected: / create from serialization onlyCTestDllView();DECLARE_DYNCREATE(CTestDllView) / Attributespublic:CTestDllDoc* GetDocument(); / Operationspublic: / Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CTestDllView)public:virtual void OnDraw(CDC* pDC); /
26、 overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);/AFX_VIRTUAL / Implementationpublic:virtu
27、al CTestDllView();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endif protected: / Generated message map functionsprotected:/AFX_MSG(CTestDllView)afx_msg void OnPaint();/AFX_MSGDECLARE_MESSAGE_MAP(); #ifndef _DEBUG / debug version in TestDllView.cppinline CTestDllDoc* CTestDllView:GetDocument() return (CTestDllDoc*)m_pDocument; #endif / /AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line. #endif / !defined(AFX_TESTDLLVIEW_H_CFCBD8ED_D7AF_11D6_93F
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 集训服务合同协议书
- 停车场转租合同协议书
- 酒店居住安全协议书
- 解除拆迁补偿协议书
- 非婚抚养孩子协议书
- 邯郸学院就业协议书
- 转让食堂摊位协议书
- 闲置校舍转让协议书
- 茶桌转让合同协议书
- 订单木耳采购协议书
- 数据中心的网络管理实践试题及答案
- 2024年中考二模 历史(四川成都卷)(考试版A4)
- 粉刷墙面施工协议书
- 辅导机构招聘合同协议
- 青年创新意识的培养试题及答案
- 客运车辆合伙经营合同6篇
- 2025届陕西省安康市高三下学期适应性模拟考试历史试题(原卷版+解析版)
- 《2025年CSCO肾癌诊疗指南》解读课件
- 备战2025年高考数学(新高考专用)抢分秘籍导数及其应用(九大题型)(学生版+解析)
- 村干部测试试题及答案
- 康复医学教学课件 - 基础医学 - 医药卫生 - 专业资料
评论
0/150
提交评论