The "ActiveX Shell" technologyeasy way to enable ActiveX scripting on Delphi and C++ Builder application |
This technology is for Delphi and C++ Builder.IntroductionIt allows to create ActiveX object from any Delphi object at design time and at RUN-TIME by one function call. That allows OLE automation to access properties and methods of Delphi object.
There is nothing to coding with this technology. Just call one function and use ALL your objects as true ActiveX object now.
The technology can be used, for example, to embed scripting language such as VBScript, JScript into the application. With scripting based on MS ActiveX Scripting or VBA.
In this document you can find all what you need for that.
It is known that Delphi and C++ Builder have own object model which is distinct from object model of MS Windows named COM / DCOM / OLE / ActiveX. Therefore to apply Delphi objects as ActiveX objects it is necessary each time to create ActiveX Library and separated ActiveX object as wrapper. But will be very problematic to make it for all objects used by Delphi application. Moreover that will not be Delphi application.Possibilities"ActiveX Shell" technology overcomes this misunderstanding and makes properties and methods of Delphi objects accessible for OLE automation that has a HUGE range of use.
Typical example of use is an embedding a scripting language into the application by using Microsoft ActiveX Scripting. Where in role of the scripting language can be used VBScript, JScript or any other maintained MS ActiveX Scripting.
I. Windows applications
supporting OLE, VBAII. Internet Explorer III. ASP IV. Windows Scripting Host | OLE automation <- MS ActiveX Scripting <- JScript, VBScript, Perl etc | | | | "ActiveX Shell" object
[ threadsafe ]Delphi application's
internal scripts| Delphi objects MS ActiveX scripting is only one of ways to use application's object model created by "ActiveX Shell" technology. You can use with your application any other scripting engine that support AciveX object model.
If you create OCX library with "YourAppName.Application" object then is possible to execute your application and to use its object model in Visual Basic or VBA of Windows application supporting OLE such as Word, Excel, Outlook, Internet Explorer, IIS ASP etc.
Windows Scripting Host is new MS technology that allows to create JScript and VBScript program as Windows batch file. It can use an application's object model created by "ActiveX Shell" technology too.
It is enough to create one "ActiveX Shell" object to receive access to all other linked Delphi objects in application which is accessible from first as properties, child-components, methods. That allow to create not only one ActiveX object but APPLICATION'S OBJECT MODEL.
What does it mean ? For example if adding to script engine an "ActiveX Shell" reference to Form1 then all others on the form will be accessible via OLE automation as properties, child components or method results like this:
Form1.Panel1.Caption
Form1.Panel1.Font.Name
Form1.DBGrid1.Columns.Item(1).Title
Form1.Query1.SQL.Text
...
Form1 |____ Panel1 | |-- Caption | |-- Font | |-- Size | |-- Color | |-- Name |____ Panel2 | |-- ... | |____ Button1 | |-- ... | |____ DBGrid1 | |-- Visible | |-- Columns | |-- Item(i) | |-- FieldName | |-- Title | |-- Font | |- ... | |____ Query1 | |-- SQL | | |- Text | | | |-- Active | |-- ...
It allows to create ActiveX object from any Delphi object at design time and at RUN-TIME by one function call. That allows OLE automation to access properties and methods of the Delphi object.See below how to create "ActiveX Shell" object for your Delphi object.
"ActiveX Shell" object is TRUE ActiveX object.
From the point of view of OLE a "ActiveX Shell" object have same properties and methods as defined in corresponding native Delphi object. OLE works with native Delphi object by its "ActiveX Shell" object.
Now it is possible through this mechanism:
For object received as result of first two listed actions a new "ActiveX Shell" object will created automatically. To return an object as result of "ActiveX Shell" methods you have to create new "ActiveX Shell" object manually by calling obj_to_variant or obj_to_activex function in the method implementation.
- to get and set values of published properties
- for classes inherited from TComponent to receive child-components objects by name
- for classes implementing Iactivex_shell_executable to call methods with variable number of arguments
- raising an exception in those methods as in normal Delphi methods
Now it is possible too:
An "ActiveX Shell" object is true ActiveX object. Therefore to reference such object in Delphi is used Variant variable. Function var_is_object described below is used to check that Variant contains "ActiveX Shell" object.
- to store and return Delphi objects in Variant variable
- to receive object with reference counting and autodestruction features, see below
When having "ActiveX Shell" object it is possible anytime to access corresponding Delphi object by calling function var_to_object described below.
For some Delphi classes it creates automatically extended "ActiveX Shell" object for receiving access to basic features of Delphi object via OLE automation. If the class is frequently used in Delphi but have not sufficient published properties to manipulate. Technology allows you to define such extended "ActiveX Shell" objects for any Delphi class. See below in detail.
All request to Delphi object via "ActiveX Shell" objects is always automatic synchronized. That allow to create multithread application without care about threads synchronization. The technology garantees that only one thread in multithread framework can access some Delphi object at one time even if you create more then one "ActiveX Shell" object for one Delphi object. But of course any two different Delphi objects work independently.
Thread 1 (main)
ActiveX Shell for same Object1 Thread 2
ActiveX Shell for same Object1 Thread 3
ActiveX Shell for same Object1 \
waiting\|
waiting
| /working
/
Delphi Object1
Limitations of the technology is based on the Run Time Type Information's features.Available versions, downloadingThe request on TPersistent is explained by availability the Run Time Type Information (RTTI) for the object. Though classes inherited from TPersistent is the majority of Delphi classes. And practically absolutely all published properties of Delphi objects are simple data types or reference to classes inherited from TPersistent. For example, TComponent, TPicture, TGraphic, TFont, TStrings, TCollection are inherited from TPersistent.
- the technology works only for classes inherited from TPersistent
- it is necessary to have one version of Delphi or C++ Builder (it mean RTTI) for all separately compiled modules (.exe, .dll, .ocx etc) in application if they use the given technology together
- "ActiveX Shell" can't know when corresponding Delphi object will be destroyed by other objects
Available compiled versions for:How to installCompiled versions for other Delphi and C++ Builder releases is not available now.
- C++ Builder 3
- Delphi 3
- Delphi 4
- Delphi 5
Use the source code with those releases.Source code is compatible with Delphi 3 or higher and C++ Builder 3 or higher.
Download now latest version of axshell.zip (~100 Kb).
Usage
1. Unzip archive axshell.zip with subdirectories. 2. Directory CB3\ is for CBuilder 3 users. File activex_shell.obj.
Directory D3\ is for Delphi 3 users. File activex_shell.dcu.
Directory D4\ is for Delphi 4 users. File activex_shell.dcu.
Directory D5\ is for Delphi 5 users. File activex_shell.dcu.Copy file activex_shell.dcu (.obj) from one of this to any Delphi library path or to your project path.
Or install activex_shell.dcu (.obj) from Menu > Component > Install Component.
3. Just add activex_shell to uses statement when need it. Examples source code is located in the corresponding directory ( CB3\, D3\, D4\, D5\ ) too.
Also see examples below.
It is easy to use. Just call one function to create "ActiveX Shell" object for Delphi objectfunction obj_to_variant(obj : TObject) : Variant;
Returned Variant datatype will be varDispatch because it is ActiveX object.
For example,
unit Unit1; interface
uses
activeX_shell;type
TForm1 = class (TForm)
Button1: TButton;
procedure Button1Click (Sender: TObject);
private
end;implementation
{$R *.DFM}
procedure TForm1. Button1Click (Sender: TObject);
var v: Variant;
begin// creating "ActiveX Shell" object for this form
v := obj_to_variant(Self);// using published properties
v.Caption := 'New caption';
v.Font.Size := 22;// using child-components by name -
// Button1 is not a published property
v.Button1.Caption := 'After click caption';// using an object type properties
v.ActiveControl := v.Button1;end;
end.
That is all you need for using published properties and for accessing child-components by name.
For using "ActiveX Shell" methods it is necessary to implement Iactivex_shell_executable interface. It is declared in activex_shell unit.
Important declarations in unit activex_shell
Interface declaration in unit activex_shell:
Iactivex_shell_executable = interface(IUnknown)
['{B0BA1D30-6F55-11D3-B11C-F9DBB0614516}']// to return a comma delimited list of methods supported by object
function activex_shell_methods : AnsiString;// to execute objects methods with variable number of arguments
function activex_shell_exec(method_name: AnsiString; var param: Variant): Variant;
end;// Notes:
// method_name is a name of a called method
// method_name is always on lower case
// param value is Unassigned or VarArray - array [0.. n] of Variant// Hint:
// Use standard Delphi functions to check
// that Variant is an array and for determining size of the array:
// VarIsArray( param)
// VarArrayHighBound( param, n )
Functions declaration in unit activex_shell
// checking that Variant is "ActiveX Shell" object
function var_is_object(value: Variant): Boolean;// getting back Delphi object from Variant containing "ActiveX Shell" object
function var_to_object(value: Variant) : TObject;// Getting textual representation of Variant or zero length string
function var_to_string(value: Variant): AnsiString;// Example: let variable obj is of type TObject and param is VarArray
// if var_is_object( param [i] ) then obj := var_to_object( param [i] );
For all classes implementing Iactivex_shell_executableinterface it is possible to use methods with variable number of arguments.
This is example of use Iactivex_shell_executable interface in Delphi 3. Additional IUnknown interface implementation is only for Delphi 3. In Delphi 4 and later TComponent class is already implements IUnknown and you should not do it.
Using TStrings, TCollectionFor some Delphi classes it creates automatically extended "ActiveX Shell" object for receiving access to basic features of Delphi object via OLE automation. If the class is frequently used in Delphi but have not sufficient published properties to manipulate. Technology allows you to define such extended "ActiveX Shell" objects for any Delphi class.
For TStrings class and all inherited classes it is possible to use in appropriate extended "Active X Shell" object already defined properties and methods:
Standard TStrings properties, see Delphi documentation
Standard TStrings methods, see Delphi documentation
- Count : Integer;
- Text : String;
Special methods for getting value of element
- function Add(value: String) : Integer;
- procedure Append(value: String) : Integer;
- procedure Clear();
- procedure Exchange(index1, index2: Integer);
- function IndexOf(value: String) : Integer;
- procedure Insert(index: Integer; value: String);
- procedure Move(CurIndex, NewIndex: Integer);
- procedure LoadFromFile(FileName: String);
- procedure SaveToFile(FileName: String);
Special method for setting value of element
- function Strings(index: Integer) : String;
- function Item(index: Integer) : String;
- procedure SetItem(index: Integer; value: String);
For TCollection class and all inherited classes it is possible to use in appropriate extended "Active X Shell" object already defined properties and methods:Standard TCollection properties, see Delphi documentation
Standard TCollection methods, see Delphi documentation
- Count : Integer;
- ItemClass : String;
Special methods for getting value of element
- function Add : TObject;
- procedure Clear();
- function FindItemID(id : Integer) : TObject;
Of course those properties and methods always return values as Variant. If result type is TObject it means Variant contains "ActiveX Shell" object for that TObject.
- function Item(index: Integer) : TObject;
Using with MS
ActiveX Scripting, embedding scripts into the application
MS ActiveX Scripting is only one of ways to using application's object model created by "ActiveX Shell" technology. You can use with your application any other scripting engine that use AciveX object model. See above about it.Using with HTML Template component to create HTML, XML, SGML reportsFor embedding scripts on VBScript or JScript into an application in addition is required
MS Script Engine - already is on your computer if MSIE 4.0 or above is installed. It is possible to install separately with the distribution kit loaded from Microsoft site
- MS Script Engine
- MS Script Control
http://www.microsoft.com/msdownload/vbscript/scripting.asp
Size: ~ 650 Kb
License: Freeware
GUID: {EE09B103-97E0-11CF-978F-00A02463E06F}
MS Script Control - is necessary for use in the application the MS Script Engine . It is possible to install separately with the distribution kit loaded from Microsoft site
http://msdn.microsoft.com/scripting/scriptcontrol/default.htm
Size: ~ 250 Kb
License: Freeware, see site
GUID: {0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC}
// Hint:
// use this function for checking availability of component by its GUID
function check_by_guid (component_guid: String): Boolean;
var v : Variant;
begin
try
v := CreateCOMObject( StringToGUID (component_guid) );
result := True;
except
result := False;
ShowMessage('Component is not installed: ' + component_guid);
end;
end;For using MS Script Control it is possible to import it ActiveX class to Delphi ( Delphi IDE Menu > Component > Import ActiveX Control ) and to use it in application as Delphi control, having placed on the necessary form at designer.
Other method is to create OLE object manually in Run Time. This is a small example of use MS Script Control. Where in Run Time two commands on VBScript are interpreted:
var this_form, scripting, module: Variant;
begin// creating instance of MS Script Control
scripting := CreateOLEObject('ScriptControl');
scripting.AllowUI := True;
scripting.Language := 'VBScript';
// creating "ActiveX Shell" for object
this_form := obj_to_variant(Self);
// example of creation of reference 'this' that is global name for all scripts code
scripting.AddObject('this', this_form, True);// example of use of the reference 'this'
scripting.ExecuteStatement('this.Caption = "New Caption 1" '
+ #13#10 + ' MsgBox (this.Caption) ');// example of creation and use of module for the object
module := scripting. Modules. Add ( 'form_module', this_form );
module.ExecuteStatement('Caption = "New Caption 2" '
+ #13#10 + ' MsgBox (Caption) ');end;
In detail about using MS Script Control, its properties, methods and objects see msscript.hlp from the MS Script Control distribution kit.
If you need to create in application HTML, XML, SGML or text reports then use HTML Template component. It is available for downloading. In detail see its manual.It works like well known ASP, JSP, PHP technologies and allows to produce HTML (XML, SGML, text) page from page template stored in file, database etc. However it works at application side without Web server engine and can access application's objects and data.
To design report view it is possible to use your faivorite editor. For example, Netscape Composer, MS FrontPage, MS Word, Notepad and so on.
Template page is formatted page with embedded script. The "ActiveX Shell" technology can be used as engine for the scripts execution.
This is only if you need to execute your application as OLE object.Professional version's advantagesFor creating OCX library with "YourAppName.Application" OLE object:
Now you have OCX library with "YourAppName.Application" object.
- use Delphi menu > New then choose ActiveX > ActiveX Library
- save project as "YourAppName"
- use Delphi menu > New then choose ActiveX > Automation object
- write "Application" as Class Name
- add properties and methods to that object and register library
Now it is possible to execute project and use its object model by calling
CreateOLEObject( 'YourAppName.Application' )
from Delphi or similar function from VBA and other languages.
In detail see Delphi documentation.
The "YourAppName.Application" object can have properties and methods of type Variant that is object reference. They can be implemented easy by using the "ActiveX Shell" technology.
I. Multithread support is in professional version only.Source code buy online
II. In professional version it is possible to create "ActiveX Shell" object with reference counting and without caring about clearing corresponding Delphi object. The Delphi object will be auto destroyed when reference count is 0.This feature is very powerful, for example, when returning value from function:
function some_function ... : Variant;
var strings : TStrings;
begin// creating some object
strings := TStringList.Create();strings.Add('line 1');
strings.Add('line 2');
strings.Add('line 3');// creating and returning as result an "ActiveX Shell" object
// with feature of reference counting
result := obj_to_activex( strings );// You have not to destroy here the TStringList object.
// It will be auto destroyed when will be not needed.end;
Note that
function obj_to_activex( obj : TObject ) : Variant;
is same as considered above obj_to_variant .
But function obj_to_activex create "ActiveX Shell" object with feature of reference counting and autodestruction.
This feature is very flexible not only in "ActiveX Shell" methods implementation, it can be used freely in application to return or store any objects without caring about clearing it. The object exists while exist one or more Variant reference to it.
Corresponding Delphi object can be received anytime by using var_to_object function.
The source code of professional version costs only 40 $.Contact information
Click the image and buy it online.After receiving the payment, maximum per two days we send you source texts with comments by e-mail or you can download it from our secure web site.
Thank you for support !
Contact person: Karim YusupovUseful Internet linksSend your questions and comments to mailto:apelseen@mail.com?Subject=About the ActiveX shell technology
Apelseen software website is http://www.apelseen.da.ru/
Version 1.5
Copyright (c) 1999, Apelseen software. All Rights Reserved