Sunday, March 29, 2020

Chapter 6 Component Object Model









Introduction (COM)
To create applications that can interact with any other application requires that there can be a standard manner in which two applications will interact.

Microsoft has defined a model that set standards that will enable applications to interact with each other.  This model is referred to as Component Object Model (COM).  It defines rules when implemented will enable applications to interact with other applications in a Uniform Manner.

“Both calling application and called application need to follow rules specified by COM.
COM is defined as an Open, Extensible standard for software Inter-Operability.”

Ø  It is called an open standard because COM specifies rules in general terms and not specifies.
Ø  It is called an extensible because it is an evolving standard and newer rules are being added.

Implementing COM enables to use features of other applications; extending the capabilities of out application.

Object Linking and Embedding
COM defines a model that enables applications to interact with each other.  It is, an open standard as it defines rules in general and not specific terms.

For example for our application, to call another application the calling applications need to know the location of it.  Hence the calling application has to search for the application it needs and then to execute it.

To have the calling application search for the hard disk will be a time consuming affair.  Thus, COM states that all applications that can provide services to other applications must be registered at a Central Location. 

COM needs to be implemented in terms of specifics so that it can be used Object Linking Embedding (OLE) is one such implementation of COM.

How OLE Work?
Suppose you want to use Microsoft Word document where MS-Word is referred to as Client and Ms-Graph is called as Server application.  The graph that Ms-Graph creates in word document is referred as an object.  As server application creates an object for client application, it is sometimes also referred to as an Object Application.

So for doing this First we have to create a New document in word.  Where we have to insert a graph for Company’s Report for which we have to invoke Microsoft-Graph.  As Ms-Word doesn’t have capability to draw graphs and Ms-Graph has the ability to draw it, we can use it in our document.  In wordà Insert àObject.  We can invoke graph for our application.  After going through necessary steps a graph can be drawn on to our document.

Some points to remember
1)                  Ms-Word is calling application referred to as Client.
2)                  Ms-Graph is called Server Application or Object Application.
3)                  Graph created by Ms-Graph in word document is called as an object.
4)                  To modify graph object we need to use Microsoft-Graph as Microsoft Word can not modify this object that is created by Graph.

Registering A Server
COM states that all server applications; that are programmed to share their features with other applications, are to be registered at central location.  OLE states that, in case of Windows Central Location is the “Windows Registry”

On all computers running Windows Operating System there is a file system.dat called as Windows Registry.  When a Server Application installed, among other information it writes its name and location to the registry.  Thus when Ms-Office was installed, all products like Word, Excel, PowerPoint etc. got registered with the registry.

Hence all client application has to do is read registry to determine Server application it can use.  Each time when user clicks on “Object” under menu “Insert” Word reads the registry and displays the names of all Server Applications.

Note:   Only Server Applications are to be registered and not the Client Applications.

About The Object
Anything or any Object created on the client application is called as an Embedded Object.

User can also create objects such that they are stored separate from the document.  In this case only a reference to the object is stored in the document while object is stored as a separate file.  Document is said to contain a linked object.

Also, the object can be manipulated independent of the document.  However, when document containing a link to this object is opened the next time, changes made to the object are reflected in the document.

Linking V/s Embedding
Ø  Embedding creates documents of larger size because it stores a copy of the object within it.
Ø  Documents containing linked objects are smaller in size.
Ø  Linking is used when we need the object to be updated each time data on which the object is created, changes.



Advantages of COM
1)                  COM saves development time.  COM enables to build application with specialized abilities and use abilities of other applications as and when required.
2)                  COM promotes reusability if we know that a particular feature is required in multiple applications then we can code functionality just once and use COM from different applications.
3)                  COM enables to get the best in all the situations.  User can use the application that is the best in any category.

Types of COM Servers
1)                  Simple Servers
These are Servers where Client applications can only invoke the Server but can not access its custom variables and methods.  For example Microsoft-Graph many have variables like back Color that determine the background color of the graph.  We can not access this variable in the code of calling application to modify background color.

2)                  Automatic Servers
These are Servers that allows us to access certain methods and properties of the object.  We can use these methods and properties to manipulate the object via code.  Programmer of Automation Server decides which of the methods and properties the client needs to access to and exposes only those properties.

3)                  Active X
Typically, an ActiveX is a user interface component like a Button, List Box etc. and will have methods, properties and events that the client can manipulate.

Types of Libraries
In the case of Automation Servers, ActiveX Components like Client Application needs to determine the exposed methods, properties and events.  This is done via Type Libraries.

All Automation Servers and ActiveX components have associated with them a Type Library.  Location of the Type Library is written to registry along with all information when Server application is registered.

Client reads registry, determines location of Type Library and reads it to determine properties, methods and events of the object.

Using OLE Control
Visual Basic provides an OLE Control that enables to embed or link an OLE object an our application
àPicture

By placing it on the form we can create a link to other object to our Application.  The Property Class holds the String that defines the type of object associated with OLE control.  By double clicking on OLE control a Server application can be started.


Methods of OLE Control
Ø  Close
It closes Active Server application; if any.  It is responsibility of the Client application that it should shut down the Server.

Ø  Create Embed
Creates an embedded object from the specified file and associates it with OLE control.  It enables an existing file to be embedded in the application.
Syntax:
OLE1.CreateEmbed (“C:\Try.Doc”)

Ø  CreateLink
Creates a linked object from specified file and associates it with OLE Control.  This method enables an existing file to be embedded in the application.
Syntax:
OLE1.CrateLink (“C:\Try.Doc”)

Ø  Insert ObjDLg
Displays ‘Insert Object’ dialog box that enables to select the type of object to create.  When we click on OK of the ‘Insert Object’ dialog an object of selected type is created and associated with the control.

Using this function in our application enables us to use any of the registered servers with our application.  It reads registry for list of registered servers each time it is displayed and so each time a new server is registered; it appears in the list.
Syntax:
OLE.InsertObject ( )

Properties of OLE Control
Ø  Auto Activate 
This property decides how to activate the server.  The options available are
à    On Double_Click,
à    Get Focus,
à    Automatic and
à    Manual

Ø  Class
It is used to set or determine the name of the object class or type of object.

Ø  Object
Returns a reference to OLE object held in the OLE control.

Events of an OLE Control
Ø  Object Move
This event is called whenever active object is moved.

Ø  Update
Occurs when object’s data has been changes.  It occurs every time the server object is active and user changes it in any manner.


Top

No comments:

Post a Comment