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

📄 ch15.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//-->                                                                </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css">	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">	<TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 15 -- COM and ActiveX</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1></CENTER><CENTER><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch14/ch14.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch16/ch16.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1>- 15 -</H1></CENTER><CENTER><H1>COM and ActiveX</H1></CENTER><UL>	<LI><A HREF="#Heading1">Understanding COM</A>	<UL>		<LI><A HREF="#Heading2">COM Terminology</A>		<LI><A HREF="#Heading3">Reference Counting</A>		<LI><A HREF="#Heading4">The IUnknown Interface</A>		<LI><A HREF="#Heading5">Creating a COM Object</A>		<LI><A HREF="#Heading6">Adding Code</A>	</UL>	<LI><A HREF="#Heading7">Understanding ActiveX</A>	<UL>		<LI><A HREF="#Heading8">Using Third-Party ActiveX Controls</A>		<LI><A HREF="#Heading9">Creating New ActiveX Controls</A>		<LI><A HREF="#Heading10">Changing the ActiveX Palette Bitmap</A>	</UL>	<LI><A HREF="#Heading11">Web Deploying ActiveX Controls and ActiveForms</A>	<UL>		<LI><A HREF="#Heading12">Web Deployment Options</A>		<LI><A HREF="#Heading13">Web Deploy</A>	</UL>	<LI><A HREF="#Heading14">Summary</A>	<UL>		<LI><A HREF="#Heading15">Q&amp;A</A>		<LI><A HREF="#Heading16">Quiz</A>		<LI><A HREF="#Heading17">Exercises</A>	</UL></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>OLE, ActiveX, COM, DCOM, VCL, CORBA, MTS. . . the industry certainly doesn't lackfor acronyms when it comes to the subject of component architecture! In this chapter,I explain some of these acronyms and at least mention the others in passing. I'llexplain what these terms mean and try to shed some light on the often-confusing worldof COM and ActiveX. Specifically, I cover</P><UL>	<LI>What COM is	<P>	<LI>Creating COM objects	<P>	<LI>The Delphi Type Library Editor	<P>	<LI>Creating ActiveX controls	<P>	<LI>Creating ActiveForms	<P>	<LI>Deploying ActiveX controls</UL><P>I'd be lying if I said that COM, ActiveX, and OLE are easy to understand. Theyare not. It can be very confusing at first. I can't do justice to this subject inone chapter. My goal for this chapter is to give you enough of a background on thesearchitectures so that you can better understand the acronyms you see bandied aboutthese days. You will also get some good hands-on training in creating COM and ActiveXcontrols. Fortunately, Delphi takes a great deal of the pain out of dealing withthese APIs.</P><P><H2><A NAME="Heading1"></A>Understanding COM</H2><P>You can't talk about OLE and ActiveX without talking about COM, which stands forComponent Object Model.</P><P><strong>New Term:</strong> <I>COM</I> (<I>Component Object Model</I>) is a Microsoftspecification for creating and implementing reusable components.</P><P>&quot;Components? I thought Delphi used VCL components.&quot; Certainly VCL componentsare the most effective use of components in Delphi. They aren't the only possibility,though. As you work through this hour, you will get a clearer picture of how COMand ActiveX work with Delphi.</P><P>COM is the basis for both OLE and ActiveX. An analogy might be the TObject classin VCL. All classes in VCL are ultimately inherited from TObject. Derived classesautomatically get the properties and methods of TObject. They then add their ownproperties and methods to provide additional functionality. Similarly, OLE and ActiveXare built on top of COM. COM is the foundation for all OLE and ActiveX objects.</P><P>As a component architecture, COM has two primary benefits:</P><UL>	<LI>COM object creation is language independent. (COM objects can be written in many	different programming languages.)	<P>	<LI>A COM object can be used in virtually any Windows programming environment including	Delphi, C++Builder, Visual C++, Visual Basic, PowerBuilder, Visual dBASE, and many	more.</UL><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> One major drawback to COM is that it is heavily tied to the WinTel	(Windows/Intel) platform. So although you can use a COM object in many different	Windows programming environments, you can't necessarily use that COM object in a	UNIX programming environment. Recently Microsoft has tried to move COM to non-Windows	platforms, but it remains to be seen whether this attempt will ultimately succeed.	This chapter deals only with COM and ActiveX as they exist in the Win32 programming	environment. <HR></BLOCKQUOTE><P>You can use a number of different languages and environments to write COM objects.You can create COM objects with Delphi, C++Builder, Visual C++, Visual Basic, andprobably a few other development environments. When created, a COM object can beused in an even wider variety of development environments. A COM object created inDelphi can be used by a VB programmer, a C++Builder programmer, or even a VisualdBASE or PowerBuilder programmer.</P><P>A COM object is typically contained in a DLL. The DLL might have an extensionof .DLL or it might have an extension of .OCX. A single library file (DLL or OCX)can contain an individual COM object or can contain several COM objects.</P><P><H3><A NAME="Heading2"></A>COM Terminology</H3><P>COM is full of confusing terminology. The following sections explain some of theterms used in COM and how the many pieces of COM fit together. All of these piecesare interrelated, so you'll have to read the entire section to get the big picture.</P><P><H4>COM Objects</H4><P><strong>New Term:</strong> A <I>COM object</I> is a piece of binary code that performsa particular function.</P><P>A COM object exposes certain methods to enable applications to access the functionalityof the COM object. These methods are made available via COM interfaces. A COM objectmight contain just one interface, or it might contain several interfaces. To a programmer,COM objects work a lot like Object Pascal classes.</P><P><H4>COM Interfaces</H4><P>Access to a COM object is through the COM object's interface.</P><P><strong>New Term:</strong> A COM <I>interface</I> is the means by which the user of aCOM object accesses that object's functionality.</P><P>A COM interface is used to access a COM object; to <I>use</I> the object, if youwill. The interface in effect advertises what the COM object has to offer. A COMobject might have just one interface, or it might have several. In addition, oneCOM interface might implement one or more additional COM interfaces.</P><P>COM interfaces typically start with the letter I. The Windows shell, for example,implements interfaces called IShellLink, IShellFolder, and IShellExtInit. Althoughyou can use any naming convention you like, the leading I universally and immediatelyidentifies the class as a COM interface to other programmers.</P><P>COM interfaces are managed internally by Windows according to their interfaceidentifiers (IIDs). An IID is a numerical value contained in a data structure (arecord). The IID uniquely identifies an interface.</P><P><H4>COM Classes</H4><P><strong>New Term:</strong> A COM <I>class</I> (also known as a <I>coclass</I>) is a classthat contains one or more COM interfaces.</P><P>You can't use a COM interface directly. Instead, you access the interface througha coclass. A coclass includes a class factory that creates the requested interfaceand returns a pointer to the interface. COM classes are identified by class identifiers(CLSIDs). A CLSID, like an IID, is a numerical value that uniquely identifies a COMclass.</P><P><H4>GUIDs</H4><P>COM objects must be registered with Windows. This is where CLSIDs and IIDs comeinto play. CLSIDs and IIDs are really just different names for the same base datastructure: the Globally Unique Identifier (GUID).</P><P><strong>New Term:</strong> A <I>GUID</I> is a unique 128-bit (16-byte) value.</P><P>GUIDs are created by a special COM library function called CoCreateGUID. Thisfunction generates a GUID that is virtually guaranteed to be unique. CoCreateGUIDuses a combination of your machine information, random number generation, and a timestamp to create GUIDs. Although it is possible that CoCreateGUID might generate twoGUIDs that are identical, it is highly unlikely (more like a statistical impossibility).</P><P>Thankfully, Delphi programmers don't have to worry about creating GUIDs. Delphiautomatically generates a GUID when you create a new automation object, COM object,ActiveX control, or ActiveForm control. GUIDs in Delphi are defined by the TGUIDrecord. TGUID is declared in System.pas as follows:</P><P><PRE>TGUID = record  D1: Integer;  D2: Word;  D3: Word;  D4: array[0..7] of Byte;end;</PRE><PRE>When you create a new COM object, Delphi automatically creates the GUID for you. For example, here's the GUID for a test COM object I created:</PRE><PRE>Class_Test: TGUID = `{F34107A1-ECCF-11D1-B47A-0040052A81F8}';</PRE><P>Because GUIDs are handled for you by Delphi, you won't typically have to worryvery much about GUIDs. You will, however, see GUIDs a lot as you create and use COMobjects (including ActiveX controls).</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> If you need to generate a GUID manually, you can type Ctrl+Shift+G	in the Code Editor. Delphi will generate a GUID for you and insert it at the cursor	point. <HR></BLOCKQUOTE><H4>Type Libraries</H4><P>COM objects often use a type library.</P><P><strong>New Term:</strong> A <I>type library</I> is a special file that contains informationabout a COM object. This information includes a list of the properties, methods,interfaces, structures, and other elements that the control contains. The type libraryalso provides information about the data types of each property and the return typeand parameters of the object's methods.</P><P>This information includes the data types in the object, the methods and propertiesof the object, the version information, interfaces in the object, and so on. Typelibraries can be contained in the COM object as resources or as a standalone file.Type library files have a .TLB extension. A type library is necessary if other developersare going to use your COM objects as development tools. A COM object's type librarycontains more information about the object than is available by simply querying theobject for its interfaces. The Delphi IDE, for example, uses the information foundin type libraries to display an ActiveX control on the Component palette. Users ofa COM object can examine the type library to see exactly what methods and interfacesthe object contains.</P><P><H4>DCOM</H4><P>Distributed COM (DCOM) is a subset of COM that provides the capability to useCOM objects across networks or across the Internet. DCOM extends COM to provide themechanism for using COM in a networking environment. A detailed discussion of DCOMis beyond the scope of this book, but note that DCOM is definitely prevalent in certaintypes of network programming.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> CORBA (Common Object Request Broker Architecture) is a competing	technology to DCOM. CORBA is platform-independent, which makes it more desirable	than DCOM in many ways. In addition, CORBA is an open architecture supported by a	consortium of software companies (unlike DCOM, which is a Microsoft-specific solution).	Fortunately, Delphi gives you the option of creating both DCOM and CORBA objects.	<HR></BLOCKQUOTE><H3><A NAME="Heading3"></A>Reference Counting</H3><P>Every COM object has a reference count. The <I>reference count</I>, naturally,contains the number of processes that are currently using the COM object. A <I>process</I>is any application or DLL that uses a COM object. Because a COM object can be usedby any number of processes at one time, reference counting is used to determine whenthe COM object is no longer needed in memory.</P><P>When a COM object is created, its reference count is initialized to 1. The referencecount is incremented by one each time a process attaches to the COM object. Whena process detaches from the COM object, the reference count is decremented by one.When the reference count gets to 0, the COM object is freed from memory.</P><P><H3><A NAME="Heading4"></A>The IUnknown Interface</H3><P>All COM interfaces descend from a base interface called IUnknown. Table 15.1 liststhe methods of IUnknown.</P><P><H4>TABLE 15.1. IUnknown METHODS.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Method</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">QueryInterface</TD>		<TD ALIGN="LEFT">Queries the interface to obtain a list of supported interfaces.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">AddRef</TD>		<TD ALIGN="LEFT">Increments the interface's reference count.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Release</TD>		<TD ALIGN="LEFT">Decrements the interface's reference count. When the reference count reaches 0, the			object is freed from memory.</TD>	</TR></TABLE></P><P>I mention IUnknown primarily for historical reasons. Delphi programmers don'treally have to worry much about IUnknown as other programmers do. Delphi takes careof handling reference counting and freeing the memory for the COM object. Delphialso elevates dealing with COM objects to a level that makes an intimate knowledgeof IUnknown all but obsolete.</P><P><H3><A NAME="Heading5"></A>Creating a COM Object</H3><P>To help bring this into perspective, let's create a COM object. This COM objectwill be ridiculously simple but should illustrate how to use and build COM objectsin Delphi. The COM object you create will have these characteristics:</P>

⌨️ 快捷键说明

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