一. 编写 DLL
File/New/Dll 生成 Dll 的向导,然后能够添加导出函数和导出类
导出函数:extern "C" __declspec(dllexport) ExportType FunctionName(Parameter)
导出类:class __declspec(dllexport) ExportType ClassName{...}
例子:(说明:只是生成了一个 DLL.dll )
 
#include "DllForm.h" // TDllFrm 定义
 
USERES("Dll.res");
USEFORM("DllForm.cpp", DllFrm);
 
class __declspec(dllexport) __stdcall MyDllClass { //导出类
public:
MyDllClass();
void CreateAForm();
TDllFrm* DllMyForm;
};
 
TDllFrm* DllMyForm2;
extern "C" __declspec(dllexport) __stdcall void CreateFromFunct();//导出函数
 
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
return 1;
}
//---------------------------------------------------------------------------
 
MyDllClass::MyDllClass()
{
}
 
void MyDllClass::CreateAForm()
{
DllMyForm = new TDllFrm(Application);
DllMyForm->Show();
}
//---------------------------------------------------------------------------
void __stdcall CreateFromFunct()
{
DllMyForm2 = new TDllFrm(Application);
DllMyForm2->Show();
}
 
 
二. 静态调用 DLL
使用 $BCB path\Bin\implib.exe 生成 Lib 文档,加入到工程文档中
将该文档拷贝到当前目录,使用 implib MyDll.lib MyDll.dll 生成
// Unit1.h // TForm1 定义
#include "DllForm.h" // TDllFrm 定义
//---------------------------------------------------------------------------
 
__declspec(dllimport) class __stdcall MyDllClass {
public:
MyDllClass();
void CreateAForm();
TDllFrm* DllMyForm;
};
extern "C" __declspec(dllimport) __stdcall void CreateFromFunct();
 
class TForm1 : public TForm{...}
 
 
// Unit1.cpp // TForm1 实现
void __fastcall TForm1::Button1Click(TObject *Sender)
{ // 导出类实现,导出类只能使用静态方式调用
DllClass = new MyDllClass();
DllClass->CreateAForm();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{ // 导出函数实现
CreateFromFunct();
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete DllClass;
}
 
三. 动态调用 DLL
// Unit1.h
class TForm1 : public TForm
{
...
private: // User declarations
void (__stdcall *CreateFromFunct)();
...
}
 
// Unit1.cpp // TForm1
HINSTANCE DLLInst = NULL;
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if( NULL == DLLInst ) DLLInst = LoadLibrary("DLL.dll"); //上面的 Dll
if (DLLInst) {
CreateFromFunct = (void (__stdcall*)()) GetProcAddress(DLLInst,
"CreateFromFunct");
if (CreateFromFunct) CreateFromFunct();
else ShowMessage("Could not obtain function pointer");
}
else ShowMessage("Could not load DLL.dll");
}
 
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if ( DLLInst ) FreeLibrary (DLLInst);
}
 
四. DLL 作为 MDIChild (子窗体) 【只编写动态调用的例子】
实际上,调用子窗体的 DLL 时,系统只是检查应用程式的 MainForm 是否为 fsMDIForm 的窗体,这样只
 
要把调用程式的 Application 的 Handle 传递给 DLL 的 Application 即可;同时退出 DLL 时也要恢复
 
Application
 
// MDIChildPro.cpp // Dll 实现 CPP
#include "unit1.h" // TForm1 定义
TApplication *SaveApp = NULL;
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
if ( (reason==DLL_PROCESS_DETACH) && SaveApp )
Application = SaveApp ; // 恢复 Application
return 1;
}
 
extern "C" __declspec(dllexport) __stdcall void TestMDIChild( //1024X768
TApplication* mainApp,
LPSTR lpCaption)
{
if ( NULL == SaveApp ) // 保存 Application,传递 Application
{
SaveApp = Application;
Application = mainApp;
}
// lpCaption 为子窗体的 Caption
TForm1 *Form1 = new TForm1 ( Application, lpCaption );
Form1->Show();
}
注:上面的程式使用 BCB 3.0 编译成功
 
五. BCB 调用 VC 编写的 DLL
1. 名字分解:
没有名字分解的函数
TestFunction1 // __cdecl calling convention
@TestFunction2 // __fastcall calling convention
TESTFUNCTION3 // __pascal calling convention
TestFunction4 // __stdcall calling convention
有名字分解的函数
@TestFunction1$QV // __cdecl calling convention
@TestFunction2$qv // __fastcall calling convention
TESTFUNCTION3$qqrv // __apscal calling convention
@TestFunction4$qqrv // __stdcall calling convention
使用 extern "C" 不会分解函数名
 
使用 Impdef MyLib.def MyLib.DLL 生成 def 文档查看是否使用了名字分解
2. 调用约定:
__cdecl 缺省
是 Borland C 的缺省的 C 格式命名约定,他在标识符前加一下划线,以保留
他原来任何的全程标识符。参数按最右边参数优先的原则传递给栈,然后清栈。
extaern "C" bool __cdecl TestFunction();
在 def 文档中显示为
TestFunction @1
注释: @1 表示函数的顺序数,将在“使用别名”时使用。
 
__pascal Pascal格式
这时函数名全部变成大写,第一个参数先压栈,然后清栈。
TESTFUNCTION @1 //def file
 
__stdcall 标准调用
最后一个参数先压栈,然后清栈。
TestFunction @1 //def file
 
__fastcall 把参数传递给寄存器
第一个参数先压栈,然后清栈。
@TestFunction @1 //def file
 
3. 解决调用约定:
Microsoft 和 Borland 的 __stdcall 之间的区别是命名方式。 Borland 采用
__stdcall 的方式去掉了名字起前的下划线。 Microsoft 则是在前加上下划线,在
后加上 @ ,再后跟为栈保留的字节数。字节数取决于参数在栈所占的空间。每一个
参数都舍入为 4 的倍数加起来。这种 Miocrosoft 的 DLL 和系统的 DLL 不相同。
 
4. 使用别名:
使用别名的目的是使调用文档 .OBJ 和 DLL 的 .DEF 文档相匹配。假如还没有
.DEF 文档,就应该先建一个。然后把 DEF 文档加入 Project。使用别名应不断
修改外部错误,假如没有,还需要将 IMPORTS 部分加入 DEF 文档。
IMPORTS
TESTFUNCTIOM4 = DLLprj.TestFunction4
TESTFUNCTIOM5 = DLLprj.WEP @500
TESTFUNCTIOM6 = DLLprj.GETHOSTBYADDR @51
这里需要说明的是,调用应用程式的 .OBJ 名和 DLL 的 .DEF 文档名是等价的,
而且总是这样。甚至不用考虑调用约定,他会自动匹配。在前面的例子中,函数被
说明为 __pascal,因此产生了大写函数名。这样链接程式不会出错。
 
5. 动态调用例子
VC DLL 的代码如下:
extern "C" __declspec(dllexport) LPSTR __stdcall BCBLoadVCWin32Stdcall()
{
static char strRetStdcall[256] = "BCB Load VC_Win32 Dll by __stdcall mode is OK!";
 
return strRetStdcall;
}
 
extern "C" __declspec(dllexport) LPSTR __cdecl BCBLoadVCWin32Cdecl()
{
static char strRetCdecl[256] = "BCB Load VC_Win32 Dll by __cdecl mode is OK!";
 
return strRetCdecl;
}
 
extern "C" __declspec(dllexport) LPSTR __fastcall BCBLoadVCWin32Fastcall()
{
static char strRetFastcall[256] = "BCB Load VC_Win32 Dll by __fastcall mode is OK!";
 
return strRetFastcall;
}
 
其实动态调用和调用 BCB 编写的 DLL 没有区别,关键是查看 DLL 的导出函数名字
能够使用 tdump.exe(BCB工具) 或 dumpbin.exe(VC工具) 查看
tdump -ee MyDll.dll >1.txt (查看 1.txt 文档即可)
由于 VC6 不支持 __pascall 方式,下面给出一个三种方式的例子
void __fastcall TForm1::btnBLVCWin32DynClick(TObject *Sender)
{
/*cmd: tdbump VCWin32.dll >1.txt
Turbo Dump Version 5.0.16.4 Copyright (c) 1988, 1998 Borland International
Display of File VCWIN32.DLL
 
EXPORT ord:0000='BCBLoadVCWin32Fastcall::'
EXPORT ord:0001='BCBLoadVCWin32Cdecl'
EXPORT ord:0002='_BCBLoadVCWin32Stdcall@0'
*/
if ( !DllInst )
DllInst = LoadLibrary ( "VCWin32.dll" );
if ( DllInst )
{
BCBLoadVCWin32Stdcall = (LPSTR (__stdcall *) () )
GetProcAddress ( DllInst, "_BCBLoadVCWin32Stdcall@0" ); //VC Dll
// GetProcAddress ( DllInst, "BCBLoadVCWin32Stdcall" ); //BCB Dll
if ( BCBLoadVCWin32Stdcall )
{
ShowMessage( BCBLoadVCWin32Stdcall() );
}
else ShowMessage ( "Can't find the __stdcall Function!" );
 
BCBLoadVCWin32Cdecl = (LPSTR (__cdecl *) () )
GetProcAddress ( DllInst, "BCBLoadVCWin32Cdecl" );
if ( BCBLoadVCWin32Cdecl )
{
ShowMessage( BCBLoadVCWin32Cdecl() );
}
else ShowMessage ( "Can't find the __cdecl Function!" );
 
//Why?不是 'BCBLoadVCWin32Fastcall::',而是 '@BCBLoadVCWin32Fastcall@0'?
BCBLoadVCWin32Fastcall = (LPSTR (__fastcall *) () )
//GetProcAddress ( DllInst, "BCBLoadVCWin32Fastcall::" );
GetProcAddress ( DllInst, "@BCBLoadVCWin32Fastcall@0" );
if ( BCBLoadVCWin32Fastcall )
{
ShowMessage( BCBLoadVCWin32Fastcall() );
}
else ShowMessage ( "Can't find the __fastcall Function!" );
}
else ShowMessage ( "Can't find the Dll!" );
}
 
6. 静态调用例子
静态调用有点麻烦,从动态调用中能够知道导出函数的名字,但是直接时(加入 lib 文档到工程文档)
 
Linker 提示不能找到函数的实现
从 4 看出,能够加入 def 文档连接
(能够通过 impdef MyDll.def MyDll.dll 获得导出表)
建立和 DLL 文档名相同的 def 文档和 lib 文档一起加入到工程文档
上面的 DLL(VCWIN32.dll) 的 def 文档为(VCWIN32.def):
LIBRARY VCWIN32.DLL
 
IMPORTS
@BCBLoadVCWin32Fastcall = VCWIN32.@BCBLoadVCWin32Fastcall@0
_BCBLoadVCWin32Cdecl = VCWIN32.BCBLoadVCWin32Cdecl
BCBLoadVCWin32Stdcall = VCWIN32._BCBLoadVCWin32Stdcall@0
 
对应的函数声明和实现如下:
extern "C" __declspec(dllimport) LPSTR __fastcall BCBLoadVCWin32Fastcall();
extern "C" __declspec(dllimport) LPSTR __cdecl BCBLoadVCWin32Cdecl();
extern "C" __declspec(dllimport) LPSTR __stdcall BCBLoadVCWin32Stdcall();
 
void __fastcall TfrmStatic::btnLoadDllClick(TObject *Sender)
{
ShowMessage ( BCBLoadVCWin32Fastcall() );
ShowMessage ( BCBLoadVCWin32Cdecl() );
ShowMessage ( BCBLoadVCWin32Stdcall() );
}
 

BCB编写DLL终极手册的更多相关文章

  1. QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)

    最近这阵子,接了个私活,封装一个开发包俗称的SDK给客户调用,查阅了很多人家的SDK,绝大部分用VC编写,而且VC6.0居多,估计也是为了兼容大量的XP用户及IE浏览器,XP自带了VC6.0运行库,所 ...

  2. delphi编写dll心得, 谢谢原作者的分享。转

    delphi编写dll心得 1.每个函数体(包括exports和非exports函数)后面加 'stdcall;', 以编写出通用的dll2.exports函数后面必须加'export;'(放在'st ...

  3. 如何编写Dll(用命令行编译加深理解)

    DLL的优点 简单的说,dll有以下几个优点: 1)      节省内存.同一个软件模块,若是以源代码的形式重用,则会被编译到不同的可执行程序中,同时运行这些exe时这些模块的二进制码会被重复加载到内 ...

  4. VC2010编写Dll文件(转)

    源:VC2010编写Dll文件 1. 打开VS2010[Flie / New / Project / Visual C++ / Win32 / Win32 Console Application]在下 ...

  5. 编写dll时的内存分配策略

    前一篇文章介绍了为何要共用内存管理器,有人要问可不可以在编写dll时更通用一些,可以兼顾其它编译器(如果是其它编译器的话,Delphi写的dll不能与其它语言共用内存管理器),采用一定的策略来避免在d ...

  6. C++编写DLL动态链接库的步骤与实现方法

    原文:http://www.jb51.net/article/90111.htm 本文实例讲述了C++编写DLL动态链接库的步骤与实现方法.分享给大家供大家参考,具体如下: 在写C++程序时,时常需要 ...

  7. Delphi 编写DLL动态链接库文件的知识

    一.DLL动态链接库文件的知识简介: Windows的发展要求允许同时运行的几个程序共享一组函数的单一拷贝.动态链接库就是在这种情况下出现的.动态链接库不用重复编译或链接,一旦装入内存,Dlls函数可 ...

  8. Delphi调用C# 编写dll动态库

    Delphi调用C# 编写dll动态库 编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目WZPayDll, using System.Runtime.InteropServices ...

  9. Delphi 编写DLL动态链接库文件的知识和样例(有详细步骤,很清楚)

    一.DLL动态链接库文件的知识简介: Windows的发展要求允许同时运行的几个程序共享一组函数的单一拷贝.动态链接库就是在这种情况下出现的.动态链接库不用重复编译或链接,一旦装入内存,Dlls函数可 ...

随机推荐

  1. js的基本语法规范

    1.不要在同一行声明多个变量: 2.使用===/!==来比较true/false的返回值: 3.使用字面量替代new Array这种形式: 4.不要使用全局函数: 5.switch语句必须带有defa ...

  2. windows 开启管理员权限

    在使用cmd为windows系统的电脑添加一条路由的时候,发现提示我权限不足,经过我的查找,需要在 我的电脑   右键  管理   本地用户管理    打开用户一栏   找到管理员账户   右键打开属 ...

  3. Vue-cli开发笔记二----------接口调用、配置全局变量

    我做的一个项目,本身是没用任何框架,纯手写的前端及数据交互,项目已经完结.最近学Vue,于是借用这个项目,改装成vue项目. (一)接口问题:使用axios的调用方法,proxyTable解决开发环境 ...

  4. Android开发 MediaPlayer入门_播放本地视频

    前言 MediaPlayer,可以播放视频/音频,并且它支持本地和网络文件的播放.本片博客作为入门教程,先以最通俗的方式解释播放文件本地视频.(如果你嫌MediaPlayer还是太麻烦可以试试选择Vi ...

  5. Mac电脑如何转换图片格式?ImageWell for Mac转换图片格式教程

    想用Mac电脑转换图片格式?我想你可以借助ImageWell for Mac软件!ImageWell是一款简单好用的的图像处理工具,具有显示,编辑,处理,保存等功能.下面小编来为大家演示在Mac电脑上 ...

  6. Batch - %~dp0 modifiers

    %~dp0 简易解释 The variable %0 in a batch script is set to the name of the executing batch file. The ~dp ...

  7. Servlet - Tomcat服务器相关

    1. 服务器 : 服务器其实就是代码编写的一个程序, 可以根据用户发送的请求, 调用执行对应的逻辑代码 2. Tomcat目录结构说明 : \bin : 存放启动和关闭Tomcat的可执行文件 \co ...

  8. Spring使用Redis

    1.引入依赖 <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...

  9. Java标识符&关键字

    1. 标识符&关键字 [标识符]: Java 对各种变量.方法和类等要素命名时使用的字符序列称为标识符. 凡是自己可以起名字的地方都叫标识符 命名规则:(一定要遵守,不遵守就会报编译的错误) ...

  10. 小程序生成海报:通过 json 配置方式轻松制作一张海报图

    背景 由于我们无法将小程序直接分享到朋友圈,但分享到朋友圈的需求又很多,业界目前的做法是利用小程序的 Canvas 功能生成一张带有二维码的图片,然后引导用户下载图片到本地后再分享到朋友圈.而小程序 ...