LazActiveX

From Free Pascal wiki
Revision as of 18:04, 11 April 2012 by Ludob (talk | contribs)
Jump to navigationJump to search

Overview

The LazActiveX package contains the TActiveXContainer component and the IDE integration of the necessary tools to create ActiveX components from a type library or directly from the object (exe or dll). ActiveX is a Microsoft Windows technology and can only be used on that platform. Cross compilation to windows is possible but the typelib importer used to create the COM bindings works only on Windows (wine not tested).

Installing

LazActiveX needs FPC >= 2.6.1.

Usage

There are 3 ways of using ActiveX components:

- drop the TActiveXContainer on a form and assign the ActiveX ClassName to the OleClassName property. Suitable for late binding and without direct event support.

- import the ActiveX type library, drop the TActiveXContainer on a form, instantiate the ActiveX object and assign it to the ComServer property. Suitable for early binding and with event support.

- create a new component from the type library or the object and drop that new component on the form. This also uses early binding and has full event support. Event handlers can be assigned from the IDE.

TActiveXContainer late binding

Although the theory is quite complex, late binding is the fastest way to get an ActiveX component running.

Late binding refers to the fact that the interfacing to the object is all done at run-time. References to objects are stored as dispinterfaces in variants and the methods and properties of the objects are "discovered" at run-time. The compiler generates all the run-time magic for you but the downside is that the compiler isn't able to help you in finding the names of methods or properties at design time. Good documentation for the ActiveX object is very important (sample code for VB can easily be translated to pascal). All errors in method names and their parameters are run-time errors. The run-time "discovery" is also adding quite some overhead.

Example: Embed VLC player in a form. VLC has to be installed !!

- Drop a TActiveXContainer, a TButton and a TFileNameEdit on a form.

- Resize and position TActiveXContainer to your liking and enter 'VideoLAN.VLCPlugin.2' as OleClassName, set Active to true. This will give a form that resembles the following:

LazActiveX-VLC-Form.png

- create an Onclick event for the button and enter

procedure TForm1.Button1Click(Sender: TObject);
var
  Actx:variant;
begin
  Actx:=ActiveXContainer1.ComServer;
  Actx.playlist.add(widestring(FileNameEdit1.FileName));
  Actx.playlist.play;
end;

- Run the program, select a movie and click the button to view the movie. Note that vlc and gdb seem to cause some problems (slow start-up in the best case, hangs or crashes). Run the program preferably outside the IDE.

TActiveXContainer early binding

Early binding means that all interfaces are known to the compiler. So we need to create the pascal bindings for the ActiveX object. The importtl.exe tool in the fpc utils directory does that for you and creates coclasses and event listeners to facilitate the use of the objects in pascal. A GUI for the importtl.exe tool is integrated with the LazActiveX package and is accessible in the Tools menu: Import Type Library.

LazActiveX-Importtl.png


Creating visual ActiveX components from type libraries or objects