使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都由控件的拥有者来动作,也就是
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




