c++builder建立及调用dll

上传人:wt****50 文档编号:32826974 上传时间:2018-02-12 格式:DOC 页数:7 大小:41.50KB
返回 下载 相关 举报
c++builder建立及调用dll_第1页
第1页 / 共7页
c++builder建立及调用dll_第2页
第2页 / 共7页
c++builder建立及调用dll_第3页
第3页 / 共7页
c++builder建立及调用dll_第4页
第4页 / 共7页
c++builder建立及调用dll_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《c++builder建立及调用dll》由会员分享,可在线阅读,更多相关《c++builder建立及调用dll(7页珍藏版)》请在金锄头文库上搜索。

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

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

3、r function results,/ you will need to add the library MEMMGR.LIB to both the DLL project and/ any other projects that use the DLL. You will also need to use MEMMGR.LIB/ if any other projects which use the DLL will be performing new or delete/ operations on any non-TObject-derived classes which are e

4、xported from the/ DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling/ EXEs to use the BORLNDMM.DLL as their memory manager. In these cases,/ the file BORLNDMM.DLL should be deployed along with your DLL./ To avoid using BORLNDMM.DLL, pass string information using char * or/ Sh

5、ortString parameters./ 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 for you/-extern C _declspec(dllexport) _stdcall int myAdd(int,int);extern C _declspec(dllexport) _stdcall AnsiString aboutMe(void);int add(int n1,int

6、n2);#pragma argsusedint WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)return 1;/-_declspec(dllexport) _stdcall int 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 曾棕根好你个大笨蛋,

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

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

9、要 DLL 文件,程序运行时,程序不需要知道这个 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-Add to project 将 myDLL.lib 库导入到工程。如果要从工程中清除库文件,方法有两种:a、Project-Remove from projectb、View-Project Manager3、在工程的 Unit1.cpp 中写程序代码:/-#include #pragma hdrstop#include Unit1.h/-#pragma package(smart_init)#pragma

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

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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 机械/制造/汽车 > 机械理论及资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号