See All Titles |
![]() ![]() Introduction to COM ObjectsLet'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 SpecificationCOM 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 files—COM provides the communication mechanism to enable components in different modules to talk to each other. They are true objects in the usual sense—they 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 programs—an Object Request Broker service. COM is the object model that underlies most of the Microsoft technologies; here are a few of those COM applications:
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 InterfacesThe 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:
IStream, IStorage, and IPropertyPage are examples of standard interfaces defined by COM. They define file-like operations, file system-like semantics, and how a control exposes a property page, respectively. Besides the standard interfaces, COM also enables you to define your own custom interfaces by using an Interface Definition Language (IDL). The IDispatch interface enables any COM objects to be used from a scripting environment. This interface was designed explicitly for languages that cannot use normal COM interfaces. The objects that implement this interface are known as automation objects because they expose a programmable interface that can be manipulated by another program. This interface exposes dynamic object models whose methods and properties can be determined at runtime. Basically, this interface is used whenever you are handling an object whose interface is not known at compile time, or if there is no compile time at all. Note
Note for CORBA programmers: IDispatch is equivalent to the interface repository and dynamic invocation interface that are standard parts of CORBA. To access a method or a property of an object, you can use either late or early binding. All the examples that you see in this book use late bindings because the Python interpreter doesn't know what the object interfaces look like. It doesn't know which are the methods and properties that compound the object. It just makes the calls dynamically, according to the function names that you provide. Late bindings use the IDispatch interface to determine the object model at runtime. Python function win32com.client.Dispatch() provides this runtime facility. Most examples in this chapter use the IDispatch interface. However, the win32com.client.Dispatch() function hides many implementation details from us. Internally, Python converts the names into IDs using the internal function GetIDsOfNames(). Then, this ID is passed as an argument to the Invoke() function. You can try to improve the performance of your program by calling the Invoke() function directly. Usually, the performance gets better when names are not resolved at runtime. Just be careful to provide the right ID. If you implement this way, an early binding operation is executed. For the early bindings, we have the concept of Type Libraries, wherein the object model is exposed at compile time. In this kind of implementation, you don't call the methods and properties directly. The GetIDsOfNames() method gets an ID for the method or property that you want to use, and the Invoke() method makes the call. For example, a function call would be invoked as id = GetIDsOfNames("YourMethodCall") Invoke(id, DISPATCH_METHOD) And a property would be collected as id = GetIDsOfNames("ObjectProperty") Invoke(id, DISPATCH_PROP_GET) Usually, you don't have to worry about this kind of implementation. You just say YourObject.YourMethodCall() and YourObject.ObjectProperty In order to implicitly call the Invoke() method without causing data type problems, the IDispatch interface assumes the data type VARIANT for all variables. That's because late bindings do not know the specific types of the parameters, whereas early bindings do. Late bindings do not know about parameters passed by reference, so no parameters are passed by reference. However, early bindings accept parameters passed by reference, and return them as tuples. COM objects can be implemented as InProc objects, which are implemented as DLLs. These objects are loaded into the calling process providing that best performance because no marshalling is required. Of course, for most objects, some marshaling will be needed to marshal Python parameters into a form that can be passed to the COM object. The other option is to implement COM objects as LocalServer/ RemoteServer objects. This kind of object is implemented as a standalone EXE, which is safer than the first option because of process isolation. COM can also be used to decide which implementation should be used. If both types of implementation are available, the caller interface is able to decide which option is the best one to choose. The Windows RegistryAll the information concerning a COM object, such as the mapping between its progid and clsid, is stored in the Windows Registry. The Windows Registry also stores the name of the DLL file of an InProc object, and the name of the EXE LocalServer object. Object security, threading models, and many other details are also stored there. Check the following link for more details about the COM specification: Microsoft— Common Object Model http://www.microsoft.com/com/resources/specs.asp ADOActiveX Data Objects (ADO) is an automation-based interface for accessing data. This technology uses the OLE DB interface to access an extensive range of data sources, including but not limited to data provided by the ODBC. Microsoft Remote Data Service (RDS) is a component of ADO that provides fast and efficient data frameworks for applications hosted in Microsoft Internet Explorer. RDS uses data-aware ActiveX controls to provide data access programming to Web developers, who need to build distributed, data-intensive applications for use over networks. RDS is based on a client/server, distributed technology that works over HTTP, HTTPS (HTTP over Secure Sockets layer), and DCOM application protocols. ActiveXAn ActiveX control is an OLE control that can live inside an HTML page; it can be simple Window objects, such as buttons, text boxes, or scrollbars. It also can be quite complicated, for example, a bar chart graph display can be an ActiveX control. An entire spreadsheet can also be a single control. Each ActiveX control has properties and reacts to external events. Its properties can be modified to change its appearance. For example, its containing program can set color and fonts. External events such as a mouse click or keyboard input can cause a control's event handler to execute. Note that the ActiveX technology is another Windows only thing, and not really any use in a cross platform environment. Microsoft's Web browser, Internet Explorer, is ActiveX-aware, meaning that Web application developers can package ActiveX components to create more dynamic content in their Web pages. ActiveX controls use COM technologies to provide interoperability with other types of COM components and services. ActiveX controls provide a number of enhancements specifically designed to facilitate distribution of components over high-latency networks and to integrate controls into Web browsers. These enhancements include features such as incremental rendering and code signing, which enables users to identify the authors of controls before allowing them to execute.
|
© 2002, O'Reilly & Associates, Inc. |