Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects
Download
Report
Transcript Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects
Visual Basic: An Object
Oriented Approach
12 – Creating and using ActiveX
objects
Component Object Technologies
Components are packages of software
Composed of one or more classes
Can be used to provide service to programs
Can be used flexibly
a VB component can be used by a C++ program, a web
page or an excel spreadsheet
Current development trends emphasize the
development and use of components
More flexible systems
Reusability
Easier team development
ActiveX
ActiveX is Microsoft’s take on components
A marketing term, covering several technologies
Object Linking and Embedding (OLE)
OLE Automation
Component Object Model (COM)
ActiveX makes it possible to create
components that can interact regardless of
location, language or type
COM is the standard that ActiveX components
conform to
DCOM (Distributed COM) operates across local and
wide area networks
OLE Automation
Use statements in a program to control
objects within components
Components are library files (DLLs) or separate
executable files (EXEs)
Controlling program can be written in almost any
language
VB, C++, VBScript, JavaScript, Java, Perl, Python…
Execute efficient binaries from inefficient (but easy to
use) scripting languages
Use of OLE Automation
Typical use is to build a
complex Business
application from prebuilt components
e.g an Access database,
an Excel calculating
engine and a Word
reporting engine
Quick, simple and easy
to maitain
Business Application
Document
Production
Calculation
Typical OLE Automation Code
Sub CreateCustomerDocument(CustDetails As String)
Dim WOLE As Word.Application
Set WOLE = CreateObject(“Word.Application”)
WOLE.Documents.Add “ATemplate”, 0
WOLE.Selection.Goto wdGotoBookmark, wdGotoAbsolute, _
1, “Customer”
WOLE.Selection.Find
.Text = “”
.Replacement.Text = “”
End With
Selection.InsertAfter CustDetails
Application.Printout
Set WOLE = Nothing
End Sub
Code uses Word to create a document (e.g. a Customer’s
invoice) and print it out.
Where to get information on OLE
Auto servers (ActiveX servers)
Documentation – e.g. manuals supplied
with libraries
VB on-line help – for servers provided
by Microsoft (e.g. Office applications)
Primary source – The Object Browser
Built in to VB
Provides accurate interface information on
any components on the system
VB’s Object Browser
VB’s Object Browser showing
a user-defined class
Creating an ActiveX server
Almost trivial in VB
VB creates COM components without need for
extra code (unlike VC++)
Only changes required are type of project
selected, and a few (very few) property settings
Instancing – component’s strategy for creating objects
from its classes
ActiveX type
In-process (Dynamic Link Library – DLL)
Out-of-process (Executable – EXE)
Instancing
States how objects can be created for use by the
client application
Private: only creatable by another class in the same
component (Server)
PublicNotCreatable: component creates objects for client
application
SingleUse: only one object of class allowed – client creates it
Global SingleUse: as above, but no need to create object
reference
MultiUse: possible to create many objects from the
component – many clients can create them
GlobalMulitUse: can be used by many clients
Use of Instancing Property
Allows memory and processor allocations to be
optimised depending on the type of component
e.g. no point in a MultiUse Printing component if there is
only one printer that can be used by one client at a time
e.g. Many applications could make use of several Invoice
objects simultaneously – MultiUse obviously
Careful design consideration at the stage of deciding
on instancing setting can greatly improve application
efficiency
Process allocation
Server can be in-process or out-of-process
In-process uses the client application’s memory
allocation
Client finds it inherently easier to work with because it is
inside its own memory space
Faster
Limited to one client per instance of the component
Out-of-process runs in its own memory allocation
Clients need to communicate with it using operating
system resources
Slower and more resource hungry
Several clients can make use of the same server
Server can execute in its own right (without a cllient)
E.g. Word, Excel, Access etc.
Building an ActiveX project
Before use, server component must be
compiled
It will be executed by another program out-with
Visual Basic
This can complicate debugging, since client is
required to make component work
Can crate VB Project groups (for in-process
components)
Can run multiple instances of VB (for out-ofprocess components)
Can use the Immediate Window (to do limited test
and debug)
Deploying an ActiveX Component
Must be registered on the system it will be
used on
VB does this automatically on the system it was
developed on
To move the component to another system, must
register it explicitly
Use RegSvr32.exe, or…
Build a setup program using the Packaging and
Deployment Wizard – registers component automatically
Old components can clutter up a system
Use RegServer32 /unregserver to remove registration
Version Compatibility
Once a component is registered
Windows maintains its registry entry
Can not replace it with an incompatible version
VB allows three forms of compatibility setting
None – Windows does not check for compatibility
Project – for use while a project is under development
Changes to component interfaces will result in registration
information being updated accordingly to allow compatibility
between components and clients
Binary – for use in deployed components
changes made to the interface of one or more classes will
result in VB updating the registry information to ensure that
incompatible client programs do not try to interoperate with
the component
Creating a Server
Start an ActiveX EXE or DLL Project
Implement server classes as required
Set appropriate instancing for each class
Create suitable public methods and properties for
communication with client
Compile project to target form (EXE or DLL)
For an ActiveX EXE, run it once on the target
computer
For an ActiveX DLL, use Package and Deployment
Wizard, or execute RegSvr32.exe…
RegSvr32 c:\MyComponents\NewComponent.dll
Using a Server in a VB Program
Register the server
with the program using
Project/References
menu and checking on
the server component
Create an instance of
the component and
use it’s methods