Home arrow Python arrow Page 2 - Windows Programming in Python

What is COM? - Python

This article shows you how to call COM components from Python, and shows you how to use COM extensions to access the mail merge functionalities of MS Word in Python.

TABLE OF CONTENTS:
  1. Windows Programming in Python
  2. What is COM?
  3. Calling COM components from Python
  4. COM Programming in Python, in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 26
April 19, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Component Object Model is a software architecture that allows software to be built from binary components. This implies that if COM objects are used in building software, they can communicate with each other without knowing each other's details. Even if the components are implemented in different languages, they can communicate with each other.

At the lowest level, COM is all about interfaces. It leaves the implementation details up to the class that implements the interface. In short, COM makes a clear distinction between the interfaces and the objects that implement these interfaces. When working with COM it is important to understand how COM views certain common terms. They are:

1. Interface

2. Objects

3. Globally Unique Identifier

4. COM Libraries

All of these together provide a framework-like implementation to COM specifications. Let's look at the details to understand how these provide the framework-like implementation.

1. Interface:

By definition, "Interface is a class in which all the methods are pure virtual methods." When seen in accordance with the COM specification, interface is a strongly typed "contract" between software components. This contract provides a small but useful set of methods. In essence COM treats interfaces as the definition of an expected behavior and responsibilities. One of the main characteristic features of a COM interface is that if new methods are added to the interface, it becomes a new interface.

The convention for naming interfaces is that the names are preceded with an "I." The best example is the IUnkown interface. It is defined by COM to implement some essential functionality. All COM components are required to implement the IUnkown interface. The specialty of the IUnkown interface is that by implementing it, COM objects can control their own lifespan. Thus through the IUnknown interface, COM provides the basis of a likely framework.

2. Objects:

In OOPs, an object is an instance of a class. But in COM, an object is a piece of compiled code that provides some specific service to rest of the software system. As described under Interface above, the COM objects (or components as they are generally called) support the IUnknown interface. The main difference between simple objects and COM components is that unlike other objects, one COM component can never access another COM component directly. The access must be through interface pointers.

COM defines many interfaces. One of the implemented interfaces is the IDispatch interface. It allows COM components to be called from scripting environments.  Also, the components that implement IDispatch are known as automation components. The reason is that by using such a COM object, OLE automation can be implemented. As I said in the Interface section, each component has a unique identifier. It is known as GUID.

3. Globally Unique Identifier:

Globally Unique Identifier, or GUID for short, is a 128 bit integer that is guaranteed to be unique across all the COM components used the world over and created in the past or present. The human readable names are provided just for convenience, and their scope is local, which prevents accidental connections to wrong interfaces or methods. When a GUID refers to the class, it is known as CLSID, whereas GUID referring to the interfaces are called IID. The GUIDs can be created using tools such as uuidgen, provided by Microsoft.

4. COM Libraries:

COM library is a system component that provides the mechanics of COM as well as encapsulating the leg work associated with launching COM components and communicating with other components. Generally when a COM component has to be launched, the CLSID of the component is passed to the COM library, which in turn uses the CLSID to look up the associated server code in the registry and launches the component using either CoRegisterClassFactory or DllGetClassFactory.

That's about all the overview about COM. The above described four aspects form the basis for any type of COM application. In the next section I will focus on how Python maps the COM techniques into its domain.



 
 
>>> More Python Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: