CBuilder建立及调用DLL._第1页
CBuilder建立及调用DLL._第2页
免费预览已结束,剩余3页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C+Builder 建立及调用 DLLDLL 简称动态链接库,是 Windows 中程序的重要组成部分。想象一下,一个程序 需要多人共同完成开发,怎么个共同法?这时我们就要考虑把程序分为好几个模块, 团队每一个成员开发一个模块。 问题来了: 如何将模块组合 并成一个完整系统?还有,我们开发的软件需要不断升级,如何升级?难道每次非得把整个工程重新编译一次再发布给用户吗?解决这些问 题的科学办法,就是开发动态链接库 DLL。现在以开发 myDLL.dll 动态链接库为例,讲讲 BCB 中开发动态链接库的方法。1、 新建立一个工程:File-New-Other. 在 New 卡中选择 DLL Wiz

2、ard2、 将工程存为 myDLL.bpr3、 在 myDLL.cpp 中写接口代码:/- #include #include #pragma hdrstop/-/Important note about DLL memory management when your DLL usesthe/static version of the RunTime Library:/If your DLL exports any functions that pass String objects (orstructs/classes containing nested Strings) as parame

3、ter or functionresults,/you will need to add the library MEMMGR.LIB to both the DLLproject and/any other projects that use the DLL. You will also need to useMEMMGR.LIB/if any other projects which use the DLL will be performing newor delete/operations on any non-TObject-derived classes which areexpor

4、ted from the/DLL. Adding MEMMGR.LIB to your project will change the DLL andits calling/EXEs to use the BORLNDMM.DLasL their memorymanager. In thesecases,/the file BORLNDMM.DLL should be deployed along with your DLL./To avoid using BORLNDMM.DLpLa, ss string information using charor / ShortString para

5、meters./ If your DLL uses the dynamic version of the RTL, you do not need to/explicitly add MEMMGR.LIB as this will be done implicitly foryou/- extern C _declspec(dllexport)_stdcall int myAdd(int,int); extern C _declspec(dllexport) _stdcall AnsiStringaboutMe(void); int add(int n1,int n2);#pragma arg

6、susedint WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*lpReserved)return 1;/- _declspec(dllexport) _stdcallint myAdd(int n1,int n2)int T;T=add(n1,n2);return T;int add(int n1,int n2)return n1+n2;_declspec(dllexport) _stdcall AnsiString aboutMe(void)return 曾棕根好你个大笨蛋,居然现在才学会用 DLL!半年前

7、施勇强就告诉了你 呀! 研究进度太慢 !;4、需要注意的是,在编写 DLL 这样的程序时,要力求简单,少用大量内存分配, 尽量按照标准 C 的程序设计方法,以模块化结构设计为好,少采用面向对象的程序设计方法。5、进入 Project-Options :勾掉 Linker 页中的 Use Dynamic RTL勾掉 Packages 页中的 Build with runtime packages 按一次 Compiler 中的 Release 按钮在 Version Info 页 中 勾 选 Include version information in project ,并勾选 Auto-inc

8、rement build number ,再在里面设置好版权信息6、现在可以进入 Project-Build myDLL 生成 myDLL.dll 和 myDLL.lib 这两个文 件。二 静态调用动态链接库 DLL 调用 DLL 有两种方式,一种是静态调用,另一种就是动态调用。静态调用需要 LIB 库文件和 DLL 文件,程序编译时,需要用到 LIB 文件,发布时这个 LIB 文件就不再需要,而且,编译系统时,这个动态链接库已编译进程序,这 样,在程序一开始运行时就会查找这个DLL 文件,如果这个 DLL 文件不存在,那么,程序是启动不起来的。相反,动态调用DLL 则不是这样,它只需要 DL

9、L 文件,程序运行时,程序不需要知道这个 DLL 文件当前是 否存在,只有当程序运行到某个点,才需要去调用 DLL 文件多个应用程序调用 DLL 时,DLL 在内存中只产生一个实例,因此,可以有效地节省内 存空间,提高系统的运行效率。注意到,DLL 的编制与编程语言无关,只要遵守 DLL 的接口规范,许多语言都可以开发出高效的DLL 程序,其它语言开发的 DLL,同样可以在 BCB 中调用。下面介绍以 myDLL.dll 为例静态调用 DLL 的步骤:1、 将 myDLL.dll 和 myDLL.lib 文件拷入到开发工程中,注意到,应用程序发布时,这个 lib 文件是不需要的。如果是其它语言

10、开发的DLL,在没有lib 文件的情况下,可以用 implib.exe 工具程序,生成一个 lib 文件, 用法: implib.exe文件名 .lib 文件名 .DLL2、 Project-Ad d to project将 myDLL.lib 库导入到工程。 如果要从工程中清除库文件,方法有两种:a、 Project-Remove from projectb、 View-Project Manager3、 在工程的 Unit1.cpp 中写程序代码 :/- #include #pragma hdrstop #include Unit1.h/-#pragma package(smart_ini

11、t)#pragma resource *.dfmTForm1 *Form1;extern C _declspec(dllimport) _stdcall int myAdd(int,int);extern C _declspec(dllimport) _stdcall AnsiString aboutMe(void);/- _fastcallTForm1:TForm1(TComponent* Owner): TForm(Owner)/- void _fastcallTForm1:Button1Click(TObject *Sender)int n; n=myAdd(1,2);ShowMessa

12、ge(IntToStr(n);/- void _fastcallTForm1:Button2Click(TObject *Sender)ShowMessage(aboutMe();/-动态调用动态链接库 DLL动态调用 DLL 函数可分为八步:第一步:函数定义。这里的函数为地址转换函数。 下面这个函数其实就是定义_stdcall myAdd(int,int);int _stdcall (*myAdd)(int,int);int第二步:定义模块句柄,全局变量,它是载入DLL 文件后的实例HINSTANCE HmyDLL;第三步:装入 DLL 文件,同时获得它的句柄HmyDLL=LoadLibra

13、ry(myDLL.dll); 第四步:定义函数地址变量FARPROC P; 第五步:获取动态链接库内的某一函数的内存地址P=GetProcAddress(HmyDLL,myAdd);第六步:强制类型转换,即将所获取的函数地址强制转换为函数 myAdd=(int _stdcall(_cdecl *)(int,int)P; 第七步:函数调用 n=myAdd(10,20);第八步:释放 DLLFreeLibrary(HmyDLL);下面以动态调用 myDLL.dll 函数为例 , 进行讲解 :/- #include #pragma hdrstop #include Unit1.h/- #pragma

14、 package(smart_init)#pragma resource *.dfmTForm1 *Form1;/ 第一步:函数定义。 这里的函数为地址转换函数。 下面这个函数其实就是定 义 int_stdcall myAdd(int,int);int _stdcall (*myAdd)(int,int);AnsiString _stdcall (*aboutMe)(void);/ 第二步:定义模块句柄,全局变量,它是载入 DLL 文件后的实例 HINSTANCEHmyDLL;/-_fastcall TForm1:TForm1(TComponent* Owner) : TForm(Owner)

15、Illi 第三步:装入 DLL 文件,同时获得它的句柄HmyDLL=LoadLibrary(myDLL.dll);llll- void _fastcallTForm1:Button1Click(TObject *Sender)int n;llll 第四步:定义函数地址变量 FARPROC P;if(HmyDLL!=NULL)llll 第五步:获取动态链接库内的某一函数的内存地址P=GetProcAddress(HmyDLL,myAdd);if(P=NULL)ShowMessageC 打开 myAdd()函数错误!);else/ 第六步:强制类型转换,即将所获取的函数地址强制转换为函数myAdd=(int _stdcall (_cdecl *)(int,int)P;/ 第七步:函数调用n=myAdd(10,20);ShowMessage(IntToStr(n);elseShowMessage(”打开动态链接库文件 myDLL.dll 错误!);/- void _fastcallTForm1:FormDestroy(TObject *Sender)/ 第八步:释放 DLL FreeLibrary(HmyDLL);/- void _fastcallTForm1:Button2Click(TObject *Sender)FARPROC P;if(HmyDLL!=NULL)P=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论