⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 132.html

📁 Python Ebook Python&XML
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; Introduction to COM Objects</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="128.html" class="navtitle">7. Objects Interfacing and Distribution</a> &gt; <span class="nonavtitle">Introduction to COM Objects</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="131.html" title="Interfacing Objects"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=132" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="132.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="133.html" title="Implementing COM Objects in Python"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A37%3A04+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147035189204015059156047</font><a href="read9.asp?bookname=0672319942&snode=132&now=5%2F31%2F2002+4%3A37%3A04+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Introduction to COM Objects</h3>
				<p>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.</p>

				<P>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.</P>

				<P>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.</P>

				<h4>The COM Specification</h4>
					<p>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.</p>

					<P>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 <A NAme="idx1073744268"></a><a NAME="idx1073744269"></a>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.</p>

					<p>COM components can be packaged as EXE or DLL files桟OM provides the communication mechanism to enable components in different modules to talk to each other. They are true objects in the usual sense梩hey have identity, state, and behavior. COM components that implement a common interface can be treated polymorphically, enabling easy customization and upgrades of your applications.<a name="idx1073744270"></a><a name="idx1073744271"></a><a name="idx1073744272"></A><a nAme="idx1073744273"></a><A name="idx1073744274"></A></p>

					<p>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.</p>

					<P>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.</P>

					<P>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梐n Object Request Broker service.</P>

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

					<ul>
<LI><P>ActiveX uses COM to provide controls.</P>
</li>
<li><P>OLE uses COM to combine documents.</P>
</LI>
<li><p>OLEDB and ADO use COM for data access.</p>
</LI>
<LI><p>DirectX uses COM for graphics.</p>

						</li>
</ul>
					<p>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 <i>COM server,</i> and the program that calls the object method is called the <i>COM client.</i> Because COM is a Microsoft product, most applications for Windows can act as COM servers or clients.</p>

					<p>Python's support for the COM technology is included in the Python for Windows (PythonWin) extensions.<a name="idx1073744275"></a><a NamE="idx1073744276"></a><a nAme="idx1073744277"></a><a Name="idx1073744278"></A><A NAme="idx1073744279"></a></p>

				
				<H4>COM Interfaces</H4>
					<P>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.</P>

					<p>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.</p>

					<p>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 <a NAME="idx1073744280"></a><a naME="idx1073744281"></A><A name="idx1073744282"></a>IID (Interface ID), identifies each interface. IIDs use <a name="idx1073744283"></a><a name="idx1073744284"></a><a naMe="idx1073744285"></a><I>Universally Unique Identifier</i>s (<i>UUID</i>). 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 <Tt claSs="monofont">CreateGuid()</tt> that generates UUID strings.</P>

					<P>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 (<I><Tt claSS="monofont">_reg_clsid_</TT></i>), which is another UUID, and Program ID (<i><tt CLASs="monofont">_reg_progid_</tt></i>), 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 <I><TT Class="monofont">progid,</tt></i> or the <i><tt class="monofont">clsid.</tt></i><a nAme="idx1073744286"></A><a naMe="idx1073744287"></a><a nAme="idx1073744288"></a><A NAMe="idx1073744289"></a><a nAME="idx1073744290"></A><a namE="idx1073744291"></A></P>

					<P>All interfaces are derived from the <a namE="idx1073744292"></A><A Name="idx1073744293"></a><tt class="monofont">IUnknown</tt> interface. Therefore, they support its methods. The <tt class="monofont">IUnknown</Tt> interface is the base of all <tT claSs="monofont">COM</tt> interfaces. This interface contains only three <a Name="idx1073744294"></A><A NAme="idx1073744295"></a><a NAME="idx1073744296"></a>methods:</p>

					<ul>
<LI><P><A name="idx1073744297"></A><A NAme="idx1073744298"></a><tt class="monofont">AddRef()</tt> and <a name="idx1073744299"></a><a nAme="idx1073744300"></A><tt cLass="monofont">Release()</tT> are used for managing COM lifetimes, which are based on reference counts.</p>
</li>
<LI><P><A name="idx1073744301"></A><A NAme="idx1073744302"></a><tT CLAss="monofont">QueryInterface()</tt> is used for obtaining a reference to one of the other interfaces that the object exposes. In other words, interfaces are obtained by using the <TT CLass="monofont">IUnknown::QueryInterface()</tt> method. <a name="idx1073744303"></a><a name="idx1073744304"></a><a naMe="idx1073744305"></a></P>

						</li>
</uL>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -