132.html ( File view )

  • By 2010-08-21
  • View(s):12
  • Download(s):0
  • Point(s): 1

Safari | Python Developer's Handbook -> Introduction to COM Objects

< BACKMake Note | BookmarkCONTINUE >

Introduction to COM Objects

Let's learn a little about what is behind the Microsoft Common Object Model (COM) technology before seeing how you can use it along with Python.

COM is the most widely used component software model in the world. It provides a rich set of integrated services, a wide choice of easy-to-use tools, and a large set of available applications. COM underlies a large majority of the new code developed for Windows and Windows NT operating systems, whether created by Microsoft or by others.

COM consists of a well-defined, mature, stable, and freely available specification, as well as a reference implementation, which has been widely tested and adopted worldwide. It provides the richest set of existing services for applications today, as well as the largest set of development tools available for any component or object model on the market. Of course, Windows is the only Operating System in which you can be assured of finding COM, which makes us think that COM doesn't appear to be a standard because it doesn't provide cross-platform solutions.

The COM Specification

COM is a specification and a set of services that enables you to create modular, object-oriented, customizable and upgradable, distributed applications using a number of languages. You can even use components that you already have written in other languages.

The COM specification describes the standards that you need to follow in order to create interoperable COM components. This standard describes what COM objects should look like and how they should behave. The specification is backed up by a set of services, or APIs. The COM library provides these services, which are part of the operating system for Win32 platforms, and available as a separate package for other operating systems.

COM components can be packaged as EXE or DLL filesCOM provides the communication mechanism to enable components in different modules to talk to each other. They are true objects in the usual sensethey have identity, state, and behavior. COM components that implement a common interface can be treated polymorphically, enabling easy customization and upgrades of your applications.

COM components link with each other dynamically, and COM defines standard ways of locating components and identifying their functionality, so individual components are swappable without having to recompile the entire application.

COM provides a communication mechanism that enables components to interact across a network. More importantly, COM provides location transparency to applications (if desired) that enables them to be written without regard to the location of their components. The components can be moved without requiring any changes to the application.

COM is a binary standard. Any language that can cope with the binary standard can create or use COM objects. The number of languages and tools that support COM increases every day. C, C++, Java, JScript, Visual Basic, VBScript, Delphi, and PowerBuilder form just part of that growing list, which means that any one of these languages can easily interoperate with Python. Keep in mind that COM is a standard for interaction between programsan Object Request Broker service.

COM is the object model that underlies most of the Microsoft technologies; here are a few of those COM applications:

  • ActiveX uses COM to provide controls.

  • OLE uses COM to combine documents.

  • OLEDB and ADO use COM for data access.

  • DirectX uses COM for graphics.

Any COM-aware program is able to interact with other COM-aware programs. One program can even execute commands of the other. The program that executes the method call is called the COM server, and the program that calls the object method is called the COM client. Because COM is a Microsoft product, most applications for Windows can act as COM servers or clients.

Python's support for the COM technology is included in the Python for Windows (PythonWin) extensions.

COM Interfaces

The COM technology is very broad and complex. Basically, it enables objects to be shared among many applications, without applications knowing the implementation details of the objects. Objects that implement the COM technology can communicate with each other without the need for knowing the others'details.

COM components do business with interfaces. An interface defines functionality, but not implementation. Objects must handle the implementation. COM objects are small pieces of self-contained software that interact with other applications by exposing well-defined, language-independent interfaces.

COM is an object model that relies heavily on interfaces. These interfaces are entirely separate from their implementations. Although COM defines the interfaces, its model doesn't provide the interface's implementation. Each object's class has the task of defining the implementations. The interfaces can be standard ones that other objects also expose, or they can be special ones that are particular to that object. A unique ID, called an IID (Interface ID), identifies each interface. IIDs use Universally Unique Identifiers (UUID). UUID is a format used for many COM IDs to allocate a unique identification string for objects. Many tools can generate unique UUIDs. As you will see later in this chapter, Python's pythoncom module has a function called CreateGuid() that generates UUID strings.

In order to create an object, COM locates the required class and creates an instance of it. The concept of COM classes is identical to the other Python classes. Additionally, each COM class needs to implement two identifiers: Class ID (_reg_clsid_), which is another UUID, and Program ID (_reg_progid_), which is a identification string that must be easier to remember than the Class ID. This string is not guaranteed to be unique. In order to create an object, the programmer must specify either the progid, or the clsid.

All interfaces are derived from the IUnknown interface. Therefore, they support its methods. The IUnknown interface is the base of all COM interfaces. This interface contains only three methods:

Sponsored links
  • Sent successfully!
  • 1 point

132.html (1.65 MB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point


Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com


Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D