Está en la página 1de 2

Tips on creating ActiveX controls

By Kris Golko
I recall the day when I saw Delphi 3 presented for the first time. I remember it as if it was yesterday. The presenter was so excited about ActiveX control creation but the audience didnt seem very interested in this subject. It looked so far away from the development problems of my experience. Now I work in a mixed Delphi/C++ environment and creating ActiveX controls in Delphi and handing them over to the C++ guys is an ideal solution. I can say now that it really can be done, but there are some limitations and some things you should know before you start.

Hire the wizard


To run the ActiveX Control Wizard, select File|New and from the ActiveX tab select the ActiveX Control icon. The wizard will create the Type Library and ActiveX control implementation unit. If the open project was not an ActiveX library, it will create a new ActiveX library project as well. Alternatively, you can run the ActiveX Library Wizard first to create ActiveX library project, save it and then run ActiveX Control Wizard (ActiveX control must be part of ActiveX library). On the wizard screen, select the VCL component (which, as Ive mentioned before must be registered first), select threading mode (default Apartment model is suitable for most situations) and change the other fields if appropriate and save your project.

Delphi versions
ActiveX control creation was introduced with Delphi 3. The Inprise development team did a terrific job here (apparently) to no avail, as I suspect that only a few people tried it in those days. Delphi 4 offers vital improvements in this area. Delphi 5 offers just a few improvements over Delphi 4, but Ive heard complaints from Delphi 4 users about the stability of the Type Library Editor, so Ive been using Delphi 5 and I would recommend it (at least if somebody is interested in ActiveX stuff).

Before you start


First, you have to be aware that you need to do some reading: without an understanding of the basics of the COM technologies youll get lost. I assume here that you have at least a basic understanding of issues such as interfaces, dispatch interfaces, co-classes, etc. In this article, I will try to complement the Developers Guide rather then repeat it. The Delphi way of creating ActiveX controls is to run the ActiveX Control Wizard which will convert a native Delphi component. Conversion actually is wrapping a VCL component and exposing its properties and methods as ActiveX properties and methods. The VCL component still exists as a private variable of the ActiveX Control. To create an ActiveX control from scratch is a daunting task; hopefully the next version of Delphi will make it easy, and Im looking forward to it. You should address a number of issues before you run the ActiveX Control Wizard. First, there has to be a registered component inherited from TWinControl. And now a very important note: the Wizard translates only those properties and events which use the Automation compatible types. The reason for it is that if the property is accessed through the dispatch mechanism, parameters are marshalled and COM marshalling supports only the Automation compatible types. Refer to the Delphi help for more information on Automation compatible types. Before you run the wizard, the interface part of the VCL component should be in its final state if possible. After the ActiveX control has been created, to make any changes in the interface, you have to either rerun the wizard or make the changes manually. The second isnt easy; you have to take a close look at the code generated by the wizard to discover how it has been done and then change the source code. Be careful, you can easily make a mistake. Lets take a closer look at the Type Library. Type libraries hold information about data types, interfaces, member functions, object classes, etc. Typically, type libraries are used along with COM servers (ActiveX is an example of a COM server) to identify what types of objects and interfaces they provide. A Type Library can be stored as a binary file or inside an executable as a resource. Note that an ActiveX control is an executable and has an associated type library as a resource.) Type libraries are independent from a programming language and a development tool. Any development tool which supports ActiveX should provide a way to translate them to a native language. To do this in Delphi, select Project|Import type library from the main menu. Although a type library typically accompanies a COM server, you can create a standalone type library, for example, to share type definitions between Delphi and C++. The ActiveX Control Wizard creates a binary file containing type library (CONTROLNAME.TLB), corresponding interface unit (CONTROLNAME_TLB.PAS) and implementation unit doing the actual job of wrapping a VCL component.

Before you use your ActiveX control, some manual changes may be required, because some properties, events or methods might not have been translated by the wizard. You use the Type Library Editor to do this: one way to invoke it is to select View|Type library from the main menu.

Editor, and you have to change manually the code of the implementation unit. Take a close look at the implementation unit to figure out how Delphi does it and try to mimic it. As Ive mentioned before, you can easily make a mistake. When you have successfully recompiled the control after changing its type library, you possibly need to reregister it and its likely that youll need to import it again into your development tool. If you use it with Delphi, you have to remove the control from the component palette and import it again (ignoring warnings about overridden units). Be aware that changing even a single line of code may result in repeating the entire sequence.

How to use it
When you have finally compiled the control successfully, you need to register it to the system (Windows Registry). You can do it from the Delphi IDE Run|Register ActiveX Server, or using TRegSvr.exe utility bundled with Delphi, or regsvr32.exe bundled with MS Windows. Once your control is registered, you can test it in a development tool, like Visual Basic, Visual C++, C++Builder and, of course, Delphi. To use it with Delphi, you need to install it on a component palette (you can do it by selecting Component|Import ActiveX Control). Delphi will place the control on the ActiveX tab and will translate the controls type library (stored as a resource in the controls OCX file) into an Object Pascal unit. This Pascal unit will be placed by default in the imports directory (something like \Delphi5\Imports). Note that, although the unit is also called CONTROLNAME_TLB and also contains the same information, this is a completely different unit. It is not subject to change: if the control has changed you need to uninstall it and reimport it to ensure the changes are reflected.

Final thoughts
ActiveX controls can really be created by Delphi, although with a little bit of drudgery Wizards provide simple solutions for a start, but you have to find your own way of understanding the complexities of COM technologies. Working with COM in Delphi can be a good preparation for working with CORBA. These two have a lot in common, especially in Delphi.

Changes
One of the biggest disadvantages of ActiveX control creation in Delphi is making changes. Its easy if you dont have to change the type library; just change the VCL component code and recompile everything. If you want to make changes to the type library, you have to be absolutely sure you know what youre doing. Change the type library using Type Library

Kris Golko is an independent Delphi and JBuilder consultant. He holds the title of Certified Delphi 4 Client/ Server Suite Consultant. You can reach him on krzysztofgolko@yahoo.com.

También podría gustarte