visual basic: an object oriented approach 12 – creating and using activex objects
TRANSCRIPT
![Page 1: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/1.jpg)
Visual Basic: An Object Oriented Approach
12 – Creating and using ActiveX objects
![Page 2: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/2.jpg)
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
![Page 3: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/3.jpg)
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
![Page 4: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/4.jpg)
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
![Page 5: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/5.jpg)
Use of OLE Automation
Typical use is to build a complex Business application from pre-built components
e.g an Access database, an Excel calculating engine and a Word reporting engine
Quick, simple and easy to maitain
Business Application
DocumentProduction
Calculation
![Page 6: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/6.jpg)
Typical OLE Automation CodeSub 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 = NothingEnd Sub
Code uses Word to create a document (e.g. a Customer’s invoice) and print it out.
![Page 7: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/7.jpg)
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
![Page 8: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/8.jpg)
VB’s Object Browser
![Page 9: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/9.jpg)
VB’s Object Browser showing a user-defined class
![Page 10: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/10.jpg)
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)
![Page 11: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/11.jpg)
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
![Page 12: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/12.jpg)
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
![Page 13: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/13.jpg)
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.
![Page 14: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/14.jpg)
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-of-
process components) Can use the Immediate Window (to do limited
test and debug)
![Page 15: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/15.jpg)
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
![Page 16: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/16.jpg)
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
![Page 17: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/17.jpg)
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
![Page 18: Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649e1b5503460f94b0a1c2/html5/thumbnails/18.jpg)
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