COM编程模型

类别:Asp 点击:0 评论:0 推荐:
概述
如果你用ASP做开发, 你可能已经在你的ASP页面中用过COM了.但是,在你自己开发一个COM部件或阅读一本详细介绍COM的书之前,你很可能并没有完全理解COM,因而不能充分利用它来创建你的ASP页面.同时,你也不能很好的理解那些随COM部件带来的使用文档.如果你知道COM的标准和限制,你就可以很快的学会开发其他的COM部件.
在这个教程里,我们将学到COM是怎样工作的,你还将学到精通COM的知识.

本教程的读者
本教程将给那些已经使用过VBscript 语言的,特别是用过ADO但是不知道那就是COM的人详细描述COM模型.他将告诉你:

1.属性和方法的区别
2.属性需要参数吗
3.只读属性是什么意思
4.什么是集合对象
5.每个集合对象都有些什么属性
6.不调用方法如何对集合排序
7.在一个DLL中可以有多少个COM部件.

基础知识
COM是一个对象接口的标准.定义一个COM对象只需要定义方法和属性,没有其他的接口.从一个程序员的观点来看,属性和方法之间没有太大的区别.方法可以带参数,属性不能.属性可以读写,方法如果要返回值的话,是只读的.

尽管从编程角度看,属性和方法没有太大的区别,但是部件开发者用他们完成不同的功能.属性通常代表一个对象的状态,但是调用方法可以完成任何想完成的任务,不管他包含对象的状态与否.

属性
属性不需要参数,用来描述或设置对象的状态.所有的属性返回一个值,有些属性是只读的,有些是可读可写的.下面是VBscript中读取属性的表达式例:

例 1:

value = object.property


注意这里没有用括号.例二是设置属性例:

例 2:

object.property = value


方法
方法可以带参数,可以返回值.通常用来初始化一个对象的事件.当给方法传递参数时,方法可以用来设置值.如果方法只返回值,不设置值的话,表达式如下:

例 3:

value = object.method()


注意例3中用了括号.调用方法来返回值时必须用括号.例如,对象Connection有一个Execute方法返回一个Recordset对象.例:

例 4:

Set RS = Conn.Execute("SELECT * FROM TABLE")


不用返回值,不用参数的方法,如Connection对象的Close方法:

例 5:

Conn.Close


参数
方法可以带一个或多个参数,或一个也不要.但是,参数并不是必需的. 一旦一个参数是可选的,其后的参数都是可选的.例如,参数一和参数二是必需的,参数三是可选的,则参数四必定是可选的.一个很好的例子是Connection对象的Open方法.他有八个可选的参数.前三个用来传递数据库和等录的信息.你可以像例6那样调用Open方法:

例 6:

Conn.Open "DSN","sa",""


为了提供DSN名,用户名,口令为空,你也可以想例7那样调用:

例 7:

Conn.Open "driver=SQL Server;server=yourServerName;uid=someUID;" &_
"pwd=somePWD;database=someDatabase;"


注意在例6中我们用了三个参数,在例7中只用了一个,结果是一样的.

调用方法时,以逗号分隔,让可选的参数空着,将给该参数传递空值,
在例6中,可选参数用缺省值,在例八中用空值.

例 8:

Conn.Open "DSN","sa","", , , ,


集合
集合是本身包含了许多对象的对象集,所有的集合都包含一些预定义的方法和属性.一个集合有一个Item方法,一个Count属性,一个 _NewEnum方法.集合有建立类型与他相同的对象的能力.换句话说,如果一个对象可以被包含进一个集合中,那么,哎,这句话好难,我不翻了,给出原文吧.( In other words, if a particular object can be group in a set then that object will have a collection object that can create an instance of an object within the set. For instance, a Drives collection object will contain a set of drives that might represent all the drives on a particular computer).

Count属性返回一个代表集合中元素个数的长整型值.给Item方法传递一个长整数(当然应在1和Count之间),就返回集合中这个索引所指向的对象.就像数组那样.(原文此处混乱,稍做调整)

例 9(1):

Set Object = Collection.Item(2)

因为Item是缺省方法,所以你也可以如下调用:

例 9(2):

Set Object = Collection(2)



_NewEnum 方法可以反复调用,

例 9:

For Each Object in Collection

Next Object

(以下不译)
Notice that the _NewEnum method is not referenced within the syntax of the
statement in example 6. This is because the _NewEnum method has a special
index that Visual Basic recognizes as being used for the For Next statement. As a
little background, all methods and properties in a COM object are indexed and
certain indexes are used for particular tasks. For example the zero index is used
for the default method or property.
The Default Method or Property
The method or property that has the COM index of zero is called the default
property. Visual Basic allows the programmer to not use the regular
method/property syntax when calling the default value, you can leave the
syntactical call to the method/property off all together. For example, the default
method in all collections is the Item method. If you where going to call the Item
method, you could do it like it in example 9.



为了在ASP中建立一个COM对象,你可以:

例 11:

Set Object Server.CreateObject("SMUM.XCheck.1")


给Server的CreateObject方法只传递了一个参数,就是一个ID值,这是一个由COM部件提供者给出的,唯一地标识一个COM对象的符号.为了创建一个COM对象的实例,你必须知道该对象的ID值.

有另外一种方法可以获得一个对象的实例,你可以用一个已经存在的对象实例来创建一个新的对象实例,事实上使用集合时就是这样工作的,你调用Item方法,返回了一个对象实例.

例 12:

Set Object = Collection.Item(2)


例11和例12有一点是一样的,那就是都是从别的对象创建对象,区别是,CreateObject可以创建任何类型的对象,而Item只能返回集合中的对象.就像先有鸡,还是先有蛋的问题一样,你可能要问,Server对象又是怎么来的呢?事实上,这是内置对象.他存在于ASP当中.


内置对象
ASP中有六个内置对象,他们是:
Server
Request
Response
ObjectContext
Application
Session
这些对象与其他对象唯一不同的是,不需要创建实例.他们与其他对象表现得一样,有自己的方法和属性.因为他们是内置的,所以你不需知道他们的ID,事实上,你根本不需调用CreateObject去创建他们.

对象ID
如果你创建对象的主要方法就是调用 CreateObject 的话,知道对象的ID就非常重要了COM部件提供者在他们的文档里回提供对象的ID的.

(以下不译)
The Documentation
Now that we have established the understanding between methods and properties
along with their different attributes, we need to understand how the documentation
for the objects represents these attributes. For examples, we are going to look at
15 Seconds' component section, which is in the same format as the IIS 4.0
component documentation.
Read and Write Properties
A good example of a read/write property is that of the PhoneTranslate property of
the XCheck object, shown here in example 11:

Example 13
object.PhoneTranslate[= value]
Notice the value syntax, this is the indication of a property that can be written to.
The brackets denote that the property is optional, in other words you do not need
to set the property to use the object. Click here to view the full documentation.
Read Only Properties
A good example of a read only property is the Expires property of the ASPMail
object.

Example 14
object.Expires
Notice that unlike example 11 there is not an equal symbol, indicating this is read
only. Click here to view the full documentation.
Optional Method Arguments
A good example of the optional arguments is the SendX method of the OCXMail
object. The documentation syntax can be seen here in example 12:

Example 12
object.SendX(mailserver[, fromName[, fromAddress[, priority[,
returnReceipt[, toAddressList[, ccAddressList[, bccAddressList[,
attach[, messageSubject[, messageText]]]]]]]]]])
Notice that the only required argument is the mailserver argument. All the rest,
noted by the brackets are optional. Click here to view the full documentation.
Summary
With a fundamental understanding of COM and it's abilities, coupled with good
documentation you can expand the flexibility of your Active Server page
programming. Take the information that you already know about programming IIS
objects, like Session objects and ADO, and expand on that by adding more
COM objects to your repertoire. Third party COM object will allow you to
expand your Active server applications and accomplish tasks rapidly by leveraging
the component object model.

本文地址:http://com.8s8s.com/it/it9542.htm