Académique Documents
Professionnel Documents
Culture Documents
序分为好几个模块,团队每一个成员开发一个模块。问题来了:如何将模块组合
并成一个完整系统?还有,我们开发的软件需要不断升级,如
何升级?难道每次非得把整个工程重新编译一次再发布给用户吗?解决这些问
题的科学办法,就是开发动态链接库 DLL。
现在以开发 myDLL.dll 动态链接库为例,讲讲 BCB 中开发动态链接库的方法。
1、新建立一个工程:File-New-Other...在 New 卡中选择 DLL Wizard
2、将工程存为 myDLL.bpr
3、在 myDLL.cpp 中写接口代码:
///
/--------------------------------------------------------------------
-------
#include <vcl.h>
#include <windows.h>
#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 or 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
exported from the
//// DLL. Adding MEMMGR.LIB to your project will change the DLL and
its calling
//// EXE's 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
//// ShortString 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 n2);
#pragma argsused
int 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 "曾棕根好你个大笨蛋,居然现在才学会用 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,
并勾选 Auto-increment build number,再在里面设置好版权信息
6、现在可以进入 Project-Build myDLL 生成 myDLL.dll 和 myDLL.lib 这两个文
件。
二 静态调用动态链接库 DLL
调用 DLL 有两种方式,一种是静态调用,另一种就是动态调用。静态调用需要
LIB 库文件和 DLL 文件,程序编译时,需要用到 LIB 文件,发布时这
个 LIB 文件就不再需要,而且,编译系统时,这个动态链接库已编译进程序,
这样,在程序一开始运行时就会查找这个 DLL 文件,如果这个 DLL 文
存空间,提高系统的运行效率。注意到,DLL 的编制与编程语言无关,只要遵守
DLL 的接口规范,许多语言都可以开发出高效的 DLL 程序,其它
////-----------------------------------------------------------------
----------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
////-----------------------------------------------------------------
----------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *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::Button2Click(TObject *Sender)
{
ShowMessage(aboutMe());
}
////-----------------------------------------------------------------
----------
三 动态调用动态链接库 DLL
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
////-----------------------------------------------------------------
----------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
////第一步:函数定义。这里的函数为地址转换函数。下面这个函数其实就是定
义 int __stdcall myAdd(int,int);
int __stdcall (*myAdd)(int,int);
AnsiString __stdcall (*aboutMe)(void);
////第二步:定义模块句柄,全局变量,它是载入 DLL 文件后的实例
HINSTANCE HmyDLL;
////-----------------------------------------------------------------
----------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
////第三步:装入 DLL 文件,同时获得它的句柄
HmyDLL=LoadLibrary("myDLL.dll");
}
////-----------------------------------------------------------------
----------
转自:http://blog.csdn.net/ww425/archive/2005/03/01/306500.aspx 转发请注明!!!!!