SAMPLE: Collect.exe - OLE Automation Collection

ID: Q107546


The information in this article applies to:
  • Microsoft OLE Libraries for Windows and Win32s
  • Microsoft OLE Libraries, used with:
    • Microsoft Windows NT, versions 3.5, 3.51
    • Microsoft Windows 95


SUMMARY

Collect.exe demonstrates how to create an OLE automation collection object.


MORE INFORMATION

The following file is available for download from the Microsoft Download Center. Click the file name below to download the file:

Collect.exe
For more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address
http://www.microsoft.com/downloads/search.asp
and then click How to use the Microsoft Download Center.

The collection object implements the following methods and properties: Add Method, Count Property, Item Property, NewEnum Property, and Remove Method.

Error handling in the collection object involves raising exceptions. CreateStdDispatch does not allow exceptions to be raised, so the collection object implements the IDispatch interface using DispGetIDsOfNames and DispInvoke.

Add Method


Description:     Adds the indicated item to the collection. If an object
                 is created, it should be returned.
Arguments:       Varies. It can be a pointer to the object that is to be
                 added or it can the information required to create the
                 object.
Return type:     Varies. If no object is created, the return type should
                 be void. If an object is created, the return type should
                 be VT_DISPATCH. 
NOTE: The Add method is not appropriate for all collections, so it is not required. For many application-created collections, objects are automatically added to the collection for the user.

Count Property


Description:     Returns the number of items in the collection. Read-only
                 property.
Arguments:       None.
Return type:     VT_I4. 

Item Property


Description:     Returns the indicated item in the collection.
Argument:        Specifies the index. Some collections allow various types
                 of indexing. For example, this sample allows an integer or
                 string to be specified as an index.
Return type:     VT_DISPATCH. 
NOTE: Item is the default value for the object, so it should have the special DISPID, DISPID_VALUE. MkTypLib automatically assigns this DISPID if the default attribute is specified in the ODL file.

_NewEnum Property


Description:     Returns an enumerator that supports IEnumVariant for the
                 items currently in the collection. Read-only property.
Arguments:       None.
Return type:     VT_UNKNOWN. 
NOTE: NewEnum will not be accessible to users and must have the restricted attribute in the ODL file. The NewEnum method must have a special DISPID, DISPID_NEWENUM. The defining characteristic of a collection is the ability for a user to iterate over the items in it. Some languages will have built- in support for collections. The NewEnum method allows an OLE automation controller to support "for each" iteration over a collection:

   For Each Item In Collection
   Debug.Print Item.Text
   Next Item 
OLE automation controllers that support "for each" iteration will call the NewEnum method on the collection object and then QueryInterface on the resulting IUnknown to get the desired IEnumVariant.

Remove Method


Description:     Removes the specified item from the collection.
Argument:        Specifies the index. Some collections allow various types
                 of indexing. For example, this sample allows an integer
                 or string to be specified as an index.
Return type:     void. 
NOTE: The object is not deleted. It is simply removed from the collection. Remove should support the same kinds of indexing as the Item() method for the same collection. The Remove method is not appropriate for all collections, so it is not required. For many application-created collections, objects are automatically removed the collection for the user.

To Run

The collection sample application object exposes the following:

   ProgID :  Collection.Application 

   Method and Property Names     Notes
   -----------------------------------------------------------
   [App Object]
   Collection  (read only prop)  Returns empty collection.
   New Item    (method)          Creates and returns new item.

   [Item Object]
   Text        (default prop)    Sets and returns string.

   [Collection Object]
   See above for exposed properties and methods. 
Update the path in HELLO.REG to the current location of the object and the type libraries.

To Compile

Requires OLE version 2.01 or later.

Include device=vmb.386 in the [386Enh] section of SYSTEM.INI.
NOTE: vmb.386 can be found in \OLE2\BIN. Run WXSERVER.EXE from \OLE2\BIN before running the makefile.

Additional query words:

Keywords : kbfile kbsample kbAutomation kbNTOS350 kbNTOS351 kbOLE200 kbWinOS95 kbGrpCom kbDSupport
Version : :
Platform :
Issue type :


Last Reviewed: December 7, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.