动态链接库(DLL)是Windows编程常碰着的编程要领,下面我就先容一下在BCB (C++ Builder下简称BCB) 中如何建设利用DLL和一些能力。
一、建设:
利用BCB File|NEW成立一个新的DLL工程,并生存好文件BCB,生成一个DLL的措施框架。
1.DllEntryPoint函数为一个进口要领,假如利用者在DLL被系统初始化可能注销时被挪用,用来写入对DLL的初始化措施和卸载措施;参数:hinst用来指示DLL的基地点;reason用来指示DLL的挪用方法,用于区别多线程单线程对DLL的挪用、建设、卸载DLL;
2.在措施中插手本身所要建设的DLL进程、函数;
3.用dllimport描写出口;
例措施如下:
#include
#pragma hdrstop
extern "C" __declspec(dllexport) int test();
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason,void *)
{
return 1;
}
int test()
{
return 3;
}
留意:动态链接库中挪用进程、函数时有差异的CALL方法 __cdecl、__pascal, __fastcall、__stdcall,BCB中默认的方法为__cdecl(可不写),假如思量兼容性可用时__stdcall声明要领为:
extern "C" __declspec(dllexport) int __stdcall test();
对付个中进程、函数也改为:
int __stdcall test()
二、利用DLL
在BCB中利用DLL有两种要领:
1.用静态挪用法
首先需要在BCB的项目中插手输入接口库(import library),打开工程项目,利用BCB View|Project Manager打开项目列表,向项目中插手接口库(*.lib)。
其次在头文件中插手接口声明。
例措施如下:
//define in include file
extern "C" __declspec(dllimport) int __cdecl test();
//use function in main program
int I;
I=test();
留意:
(1)动态链接库挪用进程、函数时CALL方法 与建设时方法一样不写为__cdecl,其它需要声明。
(2)BCB建设的DLL有对应的输入接口库(import library),如只有DLL而无库时,可用BCB的implib东西发生:implib xxx.lib xxx.dll;别的可用:tlibxxx.lib,xxx.lst 发生DLL的内部函数列表,很多Windows的未果真技能就是用这种要领发明的。
2.动态挪用法
动态挪用法要用Windows API 中的LoadLibrary()和GetProcAddress()来调入DLL库,指出库中函数位置,这种要领较常见。
例措施如下:
HINSTANCE dd;
int _stdcall (*ddd)(void);
dd=LoadLibrary("xxx.dll");
ddd=GetProcAddress(dd,"test");
Caption=IntToStr(ddd());
FreeLibrary(dd);
三、留意:
建设DLL时编译链接时留意配置Project Options。
Packages标签:去除Builder with runtime packages查抄框。
Linker标签:去除Use dynamic RTL查抄框。
不然建设的DLL需要Runtime packages or Runtime library。