COM programming model

Source: Internet
Author: User
Tags dsn
Overview
If you use ASP for development, you may have used com on your ASP page. however, before you develop a com part or read a book about com, you may not fully understand COM, so you cannot use it to create your ASP page. at the same time, you cannot understand the usage documents that come with COM components. if you know the standards and restrictions of COM, you can quickly learn to develop other COM components.
In this tutorial, we will learn how com works, and you will also learn the knowledge of COM proficient.

Readers of this tutorial
This tutorial will describe the com model for those who have used the VBScript language, especially those who have used ADO but do not know that it is com. He will tell you:

1. Differences between attributes and Methods
2. Do attributes require parameters?
3. What does read-only attribute mean?
4. What is a collection object
5. What are the attributes of each set object?
6. How to sort a set without calling a method
7. How many com parts can be contained in a DLL.

Basic knowledge
Com is the standard of an object interface. to define a COM object, you only need to define methods and attributes without any other interfaces. from a programmer's point of view, there is no big difference between attributes and methods. the method can contain parameters, but the attribute cannot. the attribute can be read and written. The method is read-only if the returned value is required.

Although from the programming point of view, attributes and methods are not much different, component developers use them to complete different functions. A property usually represents the State of an object. However, you can call a method to complete any task that you want to complete, whether it contains the state of the object or not.

Attribute
The attribute does not require a parameter to describe or set the object state. all attributes return a value. Some attributes are read-only and some are readable and writable. the following is an example of an expression for reading attributes in VBScript:

Example 1:

Value = object. Property

Note that brackets are not used here. Example 2: Set attributes. Example 2:

Example 2:

Object. Property = Value

Method
The method can contain parameters and return values. events used to initialize an object. when passing parameters to a method, the method can be used to set the value. if the method only returns the value without setting the value, the expression is as follows:

Example 3:

Value = object. Method ()

Note that brackets are used in Example 3. When calling a method to return a value, brackets must be used. For example, an execute method of the object connection returns A recordset object. For example:

Example 4:

Set rs = conn. Execute ("select * from table ")

No return value, no parameter method, such as the close method of the connection object:

Example 5:

Conn. Close

Parameters
The method can contain one or more parameters. however, parameters are not required. once a parameter is optional, the subsequent parameters are optional. for example, parameter 1 and parameter 2 are required, and parameter 3 is optional, parameter 4 must be optional. A good example is the open method of the connection object. it has eight optional parameters. the first three are used to transmit information about databases and other records. you can call the open method as in Example 6:

Example 6:

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

To provide the DSN name, username, and password empty, you can also call it as in Example 7:

Example 7:

Conn. Open "driver = SQL Server; server = yourservername; uid = someuid ;"&_
"Pwd = somepwd; database = somedatabase ;"

Note that three parameters are used in Example 6 and only one parameter is used in Example 7. The result is the same.

When you call a method, use commas (,) to separate the values. If an optional parameter is left blank, a null value is passed to the parameter,
In Example 6, the default value is used for optional parameters and the null value is used in Example 8.

Example 8:

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

Set
A set contains many object sets. All sets contain predefined methods and attributes. A set has an item method, a Count attribute, and a _ newenum method. the set has the ability to create objects of the same type as it does. in other words, if an object can be included into a collection, it is very difficult. I will not repeat it. Let's give the original article. (in other words, if a particle 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 in a set of drives that might represent all the drives on a particle computer ).

The Count attribute returns a long integer that represents the number of elements in the set. to pass a long integer (of course between 1 and count) to the item method, return the object pointed to by this index in the set. just like an array. (the original text is messy here, so make some adjustments)

Example 9 (1 ):

Set object = collection. Item (2)

Because item is the default method, you can also call it as follows:

Example 9 (2 ):

Set object = Collection (2)

_ The newenum method can be called repeatedly,

Example 9:

For each object in Collection

Next object

(Not translated below)
Notice that the _ newenum method is not referenced within the syntax of
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
Little background, all methods and properties in a COM object are indexed and
Certain indexes are used for particle 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
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 can do it like it in Example 9.

To create a COM Object in ASP, you can:

Example 11:

Set Object Server. Createobject ("smum. xcheck.1 ")

Only one parameter is passed to the Createobject method of the server, which is an ID value, which is provided by the COM component provider and uniquely identifies a COM object. to create a COM object instance, you must know the id value of the object.

There is another way to get an object instance. You can use an existing object instance to create a new object instance. In fact, this works when using a set, you call the item method and return an object instance.

Example 12:

Set object = collection. Item (2)

In Example 11 and example 12, objects are created from other objects. The difference is that Createobject can create any type of objects, and item can only return objects in the set. just like the question of having a chicken or an egg, you may have to ask, how does the server object come from? In fact, this is a built-in object, which exists in ASP.

Built-in object
ASP has six built-in objects:
Server
Request
Response
Objectcontext
Application
Session
The only difference between these objects and other objects is that you do not need to create an instance. they behave like other objects and have their own methods and attributes. because they are built in, you do not need to know their ID. In fact, you do not need to call Createobject to create them.

Object ID
If the main method for creating an object is to call Createobject, it is very important to know the Object ID. The COM component provider provides the Object ID in their documents.

(Not translated below)
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
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
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.
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 [,
Returnreceept [, 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
Expand your active server applications and accomplish tasks rapidly by leveraging
The component object model.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.