How to use the Delphi control in C++builder

Source: Internet
Author: User
Tags bool constructor

The method that enables C++builder to use the DELPHIVCL class library is based on a more general way of using DLLs in Windows. In practical application, the VCL control is found to be converted to DLL library, and DLL is called dynamically in C++builder. This method applies to Non-visual VCL controls.

False order in Delphi has a sample control, has attributes actived, Pro1, Pro2, want to transfer this control to c++builder use. One, Delphi in the production of DLL in Delphi new DLL Project Sampledll, when you create a new class in this project Tttempcomp base class for Tcomponent is also a control, in which add a ConstructorCreate1, but without any action; Add the function (actived, Pro1, Pro2) &create, destroy frame of the property to be exported in the DLL. Exports to add the exported function, procdure name, in the main process of the DLL to Ttempcomp instance Temp1 Create1, save the export and set exitproc; The Hwctrl:=sample1.create (TEMP1) is added to the Opensample function to instantiate the sample, and the closesample and other attributes are added to the corresponding statements; second, C + + The use of DLL in builder the DLL generated in Delphi with Implib to generate LIB file to join the C++builder project file;

Add in header file

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);

After opensample you can use the attributes in Delphi actived, Pro1, Pro2

Third, reference DLL source is as follows

Library Sampledll;
Uses
Sysutils, Classes, Sample;
TYPE
Ttempcomp = Class (Tcomponent)
Private
Public
Constructor Create1;
Published
End
Var
Sample1:sample;
Saveexit:P Ointer;
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.

Explain:

Because the VCL control inherits from the Tcomponent,tcomponent constructor requires a aowner and is also the TCOMPONENT,VCL control's create, destroy are operated by the owner of the control, which is aowner; so I In this DLL, a new Ttempcomp class is set up to inherit from the Tcomponent and a constructor (constructor) Create1 is set, while the actual construction does nothing, the second as the Aowner to create;

Another option is to use application as a aowner but it is based on the tform that the DLL is too large; in fact, Inprise (formerly Borland) can use some form of component like DCOM to make production Products in the same product era to maintain a certain interoperability, to increase the vitality of the product group.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.