Sign in Join
Wiki copytemporary

LazActiveX: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Overview
22
33The 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).
44
55## Installing
66
77> **Warning:** LazActiveX needs FPC newer than 2.6.1. Importing type libraries only works on Windows XP or newer.
88
99From the 'Package'/'Install/Uninstall Packages ...' window, select LazActiveX 0.1, click 'Install Selected' and then 'Save and Rebuild IDE'. Alternatively, open the LazActiveX.lpk package and click on 'Use \>\>'/'Install'.
1010
1111When the installation succeeds, a new 'ActiveX' tab is added to your component palette.
1212
13-<figure>
14-<img src="LazActiveX-Install1.png" title="LazActiveX-Install1.png" />
15-<figcaption>LazActiveX-Install1.png</figcaption>
16-</figure>
13+![](/wiki-images/9/91/LazActiveX-Install1.png)
1714
1815## Usage
1916
2017There are 3 different ways of using ActiveX components:
2118
22191. drop the TActiveXContainer on a form and assign the ActiveX ClassName to the OleClassName property. Suitable for late binding and without direct event support.
23202. 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.
24213. 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.
2522
2623### TActiveXContainer late binding
2724
2825Although the theory is quite complex, late binding is the fastest way to get an ActiveX component running.
2926
3027Late 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.
3128
3229The 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.
3330
3431Good 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" also adds quite some overhead.
3532
3633**Example:** Embed VLC player in a form. VLC has to be installed !!
3734
3835- Drop a TActiveXContainer, a TButton and a TFileNameEdit on a form.
3936- 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:
4037
41-<figure>
42-<img src="LazActiveX-VLC-Form.png" title="LazActiveX-VLC-Form.png" />
43-<figcaption>LazActiveX-VLC-Form.png</figcaption>
44-</figure>
38+![](/wiki-images/3/37/LazActiveX-VLC-Form.png)
4539
4640- create an Onclick event for the button and enter
4741
4842``` pascal
4943procedure TForm1.Button1Click(Sender: TObject);
5044var
5145 actx: variant;
5246 fn: widestring;
5347begin
5448 actx := ActiveXContainer1.ComServer;
5549 fn := UTF8Decode('file:///'+StringReplace(FileNameEdit1.FileName, '\', '/', [rfReplaceAll]));
5650 // or using uriparser (add uriparser unit to uses clause)
5751 // fn := UTF8Decode(UriParser.FilenameToURI(FileNameEdit1.FileName));
5852 actx.playlist.add(fn);
5953 actx.playlist.play;
6054end;
6155```
6256
6357- 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.
6458
6559### TActiveXContainer early binding
6660
6761Early 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.
6862
69-<figure>
70-<img src="LazActiveX-Importtl.png" title="LazActiveX-Importtl.png" />
71-<figcaption>LazActiveX-Importtl.png</figcaption>
72-</figure>
63+![](/wiki-images/4/47/LazActiveX-Importtl.png)
7364
7465Check 'Convert dependent typelibs' to convert also the typelibs for the COM objects that are eventually used by the selected object. Clicking OK will start the typelib import and create new units in the IDE. The units have the following naming: LibraryName_MajorVersion_MinorVersion_TLB. Ex: Access_9_0_TLB.
7566
7667
7768**Delphi users**: Delphi unit naming does not use version numbers.
7869
7970Instantiating an ActiveX object is done at run-time by calling the Create method for the appropriate CoClass created for the library. Some ActiveX libraries expose several objects and multiple CoClasses are created. Consult the documentation for the ActiveX library in case of doubt. Assigning the instantiated ActiveX object to the ComServer property of an TActiveXContainer on a form and setting it's active property to true is all that is needed to embed it in the application.
8071
8172**Example:** Embed VLC player in a form. VLC has to be installed !!
8273
8374\- Drop a TActiveXContainer, a TButton and a TFileNameEdit on a form. Resize and position TActiveXContainer to your liking
8475
8576\- Open 'Import Type Library' from the tools menu and select 'axvlc.dll' in your VLC installation directory. Click OK. The AXVLC_1_0_TLB unit is created.
8677
8778\- Add AXVLC_1_0_TLB to the uses clause.
8879
8980\- Create an OnClick event for the button and an OnCreate event for the form. Enter the following code:
9081
9182``` pascal
9283uses ..., variants, ....
9384
9485var
9586 VLC:IVLCControl2;
9687
9788procedure TForm1.Button1Click(Sender: TObject);
9889var fn: widestring;
9990begin
10091 fn := UTF8Decode('file:///'+StringReplace(FileNameEdit1.FileName, '\', '/', [rfReplaceAll]));
10192 VLC.playlist.add(fn,'',vararrayof([':aspect-ratio=16:9']));
10293 VLC.playlist.play;
10394end;
10495
10596procedure TForm1.FormCreate(Sender: TObject);
10697begin
10798 VLC:=CoVLCPlugin2.Create;
10899 ActiveXContainer1.ComServer:=VLC;
109100 ActiveXContainer1.Active:=true;
110101end;
111102```
112103
113104\- Run the program, select a movie and click the button to view the movie.
114105
115106The typelib importer adds also event support for ActiveX that generate them. In that case, alongside the CoClass a TEventSink descendant is defined that implements the ActiveX object and all its OnEvent properties. Use the TEventSink descendant instead of the CoClass if you want to use the events. Its name starts with TEvs followed by the class name.
116107
117108**Example:** Internet Explorer in a form with event support.
118109
119110\- Drop a TActiveXContainer, a TButton, TLabel and a TEdit on a form. Resize and position TActiveXContainer to your liking
120111
121112\- Open 'Import Type Library' from the tools menu and select 'C:\WINDOWS\system32\ieframe.dll'. Click OK. The SHDocVw_1_1_TLB unit is created.
122113
123114\- Add SHDocVw_1_1_TLB to the uses clause.
124115
125116\- Create an OnClick event for the button and an OnCreate event for the form.
126117
127118\- Create in class TForm1 the private procedure OnStatusTextChange(Sender: TObject;Text_:WideString);
128119
129120\- Enter the following code:
130121
131122``` pascal
132123var
133124 Browser:TEvsWebBrowser;
134125
135126procedure TForm1.Button1Click(Sender: TObject);
136127var
137128 url,onull:Olevariant;
138129
139130begin
140131 url:=Utf8decode(Edit1.Text);
141132 onull:=NULL;
142133 Browser.ComServer.Navigate2(url,onull,onull,onull,onull);
143134end;
144135
145136procedure TForm1.FormCreate(Sender: TObject);
146137begin
147138 Browser:=TEvsWebBrowser.Create(Self);
148139 ActiveXContainer1.ComServer:=Browser.ComServer;
149140 ActiveXContainer1.Active:=true;
150141 Browser.OnStatusTextChange:=@OnStatusTextChange;
151142end;
152143
153144procedure TForm1.FormDestroy(Sender: TObject);
154145begin
155146 Browser.Free;
156147end;
157148
158149procedure TForm1.OnStatusTextChange(Sender: TObject; Text_: WideString);
159150begin
160151 Label1.Caption:=UTF8Encode(Text_);
161152end;
162153```
163154
164155\- Run the program, enter an URL in the Edit control and click the button to navigate to the URL. Label1 will show the status messages sent by the browser.
165156
166157\- To access the document object (DOM) you have to import also the typelib from C:\WINDOWS\system32\mshtml.tlb and add the created MSHTML_4_0_TLB to the uses clause. To display for example the contents of the \<body\> tag of the downloaded page, add a TMemo and a second TButton to the form and add the following code:
167158
168159``` pascal
169160procedure TForm1.Button2Click(Sender: TObject);
170161var Doc: IHTMLDocument2; //defined in MSHTML_4_0_TLB
171162begin
172163 Doc := Browser.ComServer.Document as IHTMLDocument2;
173164 Memo1.Lines.Text := utf8encode(Doc.parentWindow.document.Get_body.innerHTML);
174165end;
175166```
176167
177168Note that CoClasses are not destroyed. As all COM objects they are reference counted and destroy themselves when the last reference goes out of scope. The TEventSink descendant has to be destroyed.
178169
179170### Creating visual ActiveX components from type libraries or objects
180171
181172Instead of using the TActiveXContainer component directly the importtl tool can create TActiveXContainer descendants for every ActiveX object embedded in the type library and create a lpk package for you. The descendants have full support for events generated by the objects. To create a package with the ActiveX components open the 'Import Type Library' window in the 'Tools' menu. Select the library to import, check 'Create package' and eventually 'Convert dependent typelibs', click on 'OK' to create the package. You will be prompted for a directory to store the package. Install the new package. After the Lazarus rebuild you'll find the new components in the ActiveX tab of the component palette.
182173
183-<figure>
184-<img src="LazActiveX-Install2.png" title="LazActiveX-Install2.png" />
185-<figcaption>LazActiveX-Install2.png</figcaption>
186-</figure>
174+![](/wiki-images/2/21/LazActiveX-Install2.png)
187175
188176**Example**: Embed VLC player in a form. VLC has to be installed !!
189177
190178\- Open 'Import Type Library' from the tools menu and select 'axvlc.dll' in your VLC installation directory. Check 'Create package'. Click OK. Select a directory to store the package. The AXVLC_1_0_TLBP package and all the needed units are created. Install the AXVLC_1_0_TLBP package and rebuild the IDE.
191179
192180\- Drop a TAxcVLCPlugin2 (from ActiveX component tab), a TButton and a TFileNameEdit on a form.
193181
194182\- Set TAxcVLCPlugin2.Active to true and resize and position TActiveXContainer to your liking.
195183
196184\- Create an Onclick event for the button and enter:
197185
198186``` pascal
199187uses variants, URIParser;
200188...
201189procedure TForm1.Button1Click(Sender: TObject);
202190begin
203191 AxcVLCPlugin2_1.OleServer.playlist.add(URIParser.FilenameToURI(FileNameEdit1.FileName),'',vararrayof([':aspect-ratio=16:9']));
204192 AxcVLCPlugin2_1.OleServer.playlist.play;
205193end;
206194```
207195
208196\- Run the program, select a movie and click the button to view the movie.
209197
210198Event handlers for the components can be created directly from the Object Inspector. (Note the VLC version 1.1.5.0 typelib advertises events but fails to establish a connection point. This appears to be a bug in VLC).
211199
212200
213201**Delphi users**: Delphi creates a stub for every method and property in its ActiveX components. importtl does not add this additional layer and the AxctiveX methods are called through the components OleServer property. Fe. AxcVLCPlugin2_1.OleServer.playlist.play is the equivalent to VLCPlugin21.playlist.play in Delphi.
214202
215203---
216204*From the Free Pascal wiki, page "LazActiveX", as it stood on 2023-04-09 after a change by Serbod, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
217205Category: Components
218206Category: Lazarus
219207Category: Lazarus IDE plugins

Back to history