首页 / 软件开发 / Delphi / 如何在C++Builder中使用Delphi控件
如何在C++Builder中使用Delphi控件2011-04-23施江杰使C++Builder使用DelphiVCL类库的方法基于Windows中较通用的DLL方式。在实际应用中 找到了将VCL控件转化为DLL库,在C++Builder动态调用DLL。此法适用于非可视VCL控件。假令在Delphi中有一Sample控件,有属性Actived、Pro1、Pro2,欲将这个控件转到 C++Builder中使用。一、Delphi中DLL的制作在Delphi中新建一DLL项目SampleDLL,时在此项 目中Create一个新的类TTtempcomp基类为TComponent即也为一个控件,在其中加入一个 constructorCreate1,但不作任何动作;在DLL中加入要导出的属性的Function(Actived、 Pro1、Pro2)&Create、Destroy的框架,Exports中加入导出的Function、Procdure名称 ;在DLL的主过程中对TTempcomp的实例temp1进行Create1,另外保存出口和设置ExitProc; 在OpenSample的函数中加入HwCtrl:=Sample1.Create(temp1)对Sample进行实例化,对 CloseSample和其它属性加入相应的语句;二、C++Builder中DLL的使用将Delphi中生成的DLL 用implib生成LIB文件加入C++Builder的工程文件;在头文件中加入extern "C" __declspec(dllimport) bool _stdcall OpenSample(void);extern "C" __declspec(dllimport) void _stdcall CloseSample (void);extern "C" __declspec(dllimport) bool _stdcall Actived (void);extern "C" __declspec(dllimport) int _stdcall Pro1 (void);extern "C" __declspec(dllimport) int _stdcall Pro2 (void);在OpenSample后你就可以使用Delphi中的属性Actived、Pro1、Pro2三、参考DLL Source如下library SampleDLL;
uses
SysUtils, Classes, Sample;
TYPE
TTempcomp = class(TComponent)
private
public
constructor Create1;
published
end
var
Sample1 :Sample;
SaveExit :Pointer;
temp1 :TTempcomp;
constructor TTempcomp.Create1;
begin
// inherited Create(self);
end;
/==============================================
function OpenSample: Boolean; stdcall; export;
begin
HwCtrl:= Sample1.Create(temp1);
If Sample1.Actived then result:=true;
end;
procedure CloseSample; stdcall; export;
begin
Sample1.Destroy;
end;
function Actived: Boolean; stdcall; export;
begin
result:=Sample1.Actived;
end;
function Pro1: Interger; stdcall; export;
begin
result:= Sample1.Pro1;
end;
function Pro2: Interger; stdcall; export;
begin
result:= Sample1.Pro2;
end;
/==============================================
procedure libexit; far
begin
if Sample1.Actived =true then
Sample1.Destroy;
ExitProc:=SaveExit;
temp1.Destroy;
end;
exports
OpenSample,CloseSample,Actived ,Pro1,Pro2;
begin
temp1:=TTempcomp.Create1;
SaveExit:=ExitProc;
ExitProc:=@libexit;
end.
解释:因为VCL控件都继承于TComponent,TComponent的构造函数需要一个AOwner并且也是 TComponent,VCL控件的Create、Destroy都由控件的拥有者来动作,也就是AOwner;所以我 在此DLL中新设置了一个TTempcomp类继承于Tcomponent且性设置了一个constructor(构造函 数)Create1,而实际构造时什么都不做,以次作为要Create的Aowner;其他还有一种办法就是用Application作为Aowner但是它是基于Tform的作出来的DLL太大 ;其实,Inprise(原Borland)尽可以象MicroSoft一样用一些象DCOM类似的组件形式使得产 品在同一产品时代保持一定的互用性,来增加产品群的生命力。