- Back to Home »
- Component Object Model
Posted by : Unknown
Sunday, June 30, 2013
WHAT IS COM
COM stands for Component Object
Model. It is a Microsoft specification that describes how to create reusable
objects for programmers working in a Win32 programming environment. This makes
it easy to develop objects that can be distributed independently of an
application program file.
In earlier versions of VB or other
windows programming environment, the COM under other names such as OLE Server
or Activex Server.
Component:
It is an independent piece of code
that may be shared with several different programs.
Object:
Object is a set of code that
is designed to be reusable with a well defined interface.
COM is based on Client-Server Model
,each COM object operates s a Server that receives and processes requests from
a client program and generates responses
COM Object can
interact with Client Program in three
ways:
Property:
A Property
represents variable in your objects, although it’s often implemented as a pair
of routines: One to retrieve the value form the property and another to assign
a value to the property.
Method:
Method is merely a function or subroutine that can be called from a
client program.
Event:
An event is a subroutine whose parameters are defined in the control
and that exists in the client program. It is
called by the control to inform
programs of various situations.
NECESSICITY OF COM
Components and Objects are used to
develop an application. Since many languages have the ability to include
objects in source code, why should you use COM?
COM is the
foundation of most things that Microsoft does. Without COM ,Visual Basic
wouldn’t work, nor would Windows, SQL
Server, or any number of Microsoft products. The true test of
Microsoft’s confidence in COM is that its applications are based on COM
technology. Word 2000 and Excel 2000 are based heavily on COM, as are many
other Microsoft applications.
When you build multiple program
using objects, the objects are compiled into the object code. This has a couple
of disadvantages:
·
If you have multiple programs,
each executable program has its own copy of the objects. This means that any
time you change the objects, you must recompile all of the programs that use
them.
·
You must use the same programming
language that the objects were written in to create your application programs.
This restricts your choice of programming languages.
Since COM Objects are stored in their own independent object file,
you can change the object without recompiling the applications that use it. You
can also add new interface to the COM objects to offer new features and
capabilities.
FROM
DDE TO COM
Many of the features we see in
today’s COM technology go back many years. COM draws its roots from a number of
different technology including DDE (Dynamic Data Exchange), Object Linking
Embedding.(OLE).
DDE:
In early days of Windows , people used a clipboard much like
they do today. Users could copy parts of a document in program on to the
clipboard and paste them into a document in another program.
However, when dealing with complex
graphical documents, users found that a simple clipboard didn’t work very well.
In late 1980’s Microsoft
developed a technology called DDE to allow programmers to simplify the process
of sharing the data using a clipboard. This technology was available for only
Microsoft applications, so it was not
universal solution.
OLE:
In 1991 Microsoft created a more
general solution to this problem called OLE .
This technology allowed users to combine documents from multiple
applications into a single compound
document developers could build the best possible word processor without
worrying about adding a limited function spread sheet. For example, we can embed excel worksheet into word document. The word
document is acting as a container for excel worksheet. This is really the
foundation of OLE.
While working on OLE the Microsoft
engineers recognized that the issue of creating compound documents was really a
special case of determining how two or more system or application components
communicate with each other. In the past a number of different techniques were
employed. Some times simple subroutine calls were used. Other times more
complex messaging systems was used. Programmers were often required to use
several different techniques for communication among components.
While implementing
OLE Microsoft employed a number of new technologies; for most among them was
COM.
COM:
COM creates a layer of abstraction
between software components . Each COM object is designed to operate as a
server to a client application or
another software module. The COM object is an instance of a specific class that
defines one or more interfaces to the functions it provides. Each interface
contains one or more methods to perform specific functions.
With COM, doesn’t matter if the
component is implemented as a simple set of subroutines as an independent
process that communicates with the application using complex synchronization
techniques.
Although COM determines how two
components communicate with one another, OLE requires a number of other
standard methods in addition to those required by COM. These includes the
capability for the object to present its own user interface, send events to the
controllers containers and lets a container sets the properties of the control.
SERVICES PROVIDED BY COM
The Services provided by COM are summarized
below:
OBJECT
MANAGEMENT:
COM
provides object management a service by using reference counting. With
reference counting, COM enables developers to control when object references
and their objects are released from memory.
OBJECT
PERSISTANCE:
COM provides object persistence
services that allow objects to be stored in a file. This is particularly
important when you wish to extract an application specific object from a
document file.
STRUCTURED
STORAGE:
When objects are stored in files,
they are stored using the structured storage services provided in COM. With
structured storage we can store data in a hierarchical format within a file
very much as files are stored in directories and sub directories. Structured
storage allows applications to read and save items to files with out
restricting the lay out of the file .
DATA
TRANSFER:
COM provides DATA TRANSFER, a service that enable you to
transfer and share data between applications.
NAMING AND
BINDING SERVICES:
With Naming and Binding services,
application create, store and manage objects that provide complex operations
such as downloading of files. These services are usually not directly accessed
by the programmers however you will use these services via objects and
functions that a class provides.
WHAT’S INSIDE A COM OBJECT
COM objects communicate to the world through a series of interfaces.
Standard interfaces are generally used to access information about the COM
objects. These interfaces include or perform various COM related tasks. Custom
interfaces are those interfaces designed by the user as COM objects.
The interfaces implemented in one of
two basic ways:
IN-PROCESS
OBJECT SERVERS:
It run in the same address space as
the client process. Using them dose not add much more overhead to your
application than calling a function or subroutine.It is loaded from a regular
DLL file, which contain a set of standardized function and tables that you load
and use COM object dynamically.
FINDING AND
LOADING IN-PROCESS COM OBJECTS:
When an object in the COM library is
accessed for the first time, the COM library is loaded into your address space,
just like any other DLL. However, unlike DLL files, which require Windows to
search in a number of places to find the files, COM objects are quickly located
by using a GUID.
A GUID(Globally Unique Identifier)
is a 128 bit number i.e. unique, not
just in a specific computer, but in the entire world. The same people that
developed the Distributed Computing Environment derive this value from complex
algorithm desined. The GUID generator uses your system clock and the MAC address from your network interface card
to create a value that is guaranteed to be unique on your system. If your
computer does not have a network interface card, the GUID will be created from
a value based on your hard disk space
and the total time since the computer was booted.
OUT OF
PROCESS OBJECT SERVERS:
It run in a different address space.
They come in two different flavors:
Local and remote. Using these objects is more expensive in terms of overhead;
however, in long run, they may actually help your application to run faster.
While your program calls them like a function or a subroutine, a lot of work
needs to be done to marshal your request
to another process, wait for results, and then marshals the results in the same
fashion as an IN-PROCESS COM object.
FINDING AND LOADING OUT –OF- PROCESS COM
OBJECTS:
Finding out-of-process objects is
similar to locating inprocess objects. The calling program supplies the
object’s CLSID, which is used to find information about the object in the
windows registry.
RUNNING OUT OF PROCESS COM OBJECTS
REMOTELY:
If your computer is attached to a
network, windows includes the capability to run your object on another machine.
The same interface that your program sees with a local out-of-process COM
object still exist , but now they direct your request to another computer where
the objects exist.
To direct your COM calls to another
computer, COM uses a proxy component that intercepts your calls to methods and
properties and passes them on to the remote computer using Remote Procedure
Calls (RPC).
INTERFACES
An interface is a contact between a
COM server and a COM client. It specifies the methods, properties, and events
that are used to communicate between the client and the server. This is perhaps
the most important part of the COM object, because the interface isolates the
internal working of the COM object from how it is used by the client.
Every COM object presents at least
two interfaces to the outside world. In most cases, more than two will be
present because a number of interfaces are dedicated by the COM architecture.
ACCESSING INFORMATION THROUGH AN
INTERFACE:
An interface is a binary
specification that allows you to locate a particular variable or function in
the code module. This information stored in data structure called a virtual method table. A VMT is an array of
function pointers, where each pointer points to a particular method or property
contained in the object.
Each function pointed to by the VTM
contains not only the address of the function, but additional information, such
as the name of the function, the data type it will return, and the name and
data type for each of it’s parameters.
BINDING TO AN INTERFACE:
In order to use a COM object, you must find and load the library
containing the object. Then you must
create a link between the object variable you plan to use and the desired
interface. This process is known as binding.
There are two basic types of binding: early and late.
In
late binding, visual basic is unable to determine the type of the object your
are using.
With early binding, you must declare
your object variable with a specific object type with this information visual
basic is able to bind your variable to the object at designed time.