ATLCollections Sample: Demonstrates ICollectionOnSTLImpl, CComEnumOnSTL, and Custom Copy Policy Classes

The ATLCollections sample demonstrates the use of ICollectionOnSTLImpl and CComEnumOnSTL, and the implementation of custom copy policy classes.

Security noteSecurity Note:

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

Building and Running the Sample

To build and run this sample

  1. Open the solution file ATLCollections.sln.

  2. From the Build menu, click Build Solution.

  3. From the Debug menu, click Start Without Debugging. This will test the collections using the supplied C++ client. This client outputs the content of the collections to a command window by looping through using the Count and Item properties and by enumerating the items using the _NewEnum property. The Add, Remove, and Clear methods of the IItems interface are also tested.

About the Sample

This project implements two COM collections based on data stored in STL containers:

  • The simpler of the two classes, CWords, is a read-only BSTR collection based on data stored as std::string objects in a std::vector. This class uses the custom copy policy class VCUE::GenericCopy, defined in VCUE_Copy.h and VCUE_CopyString.h, to convert the data from std::string objects to the type appropriate for the collection or enumerator interface.

  • The second class, CItems, is a read-write VARIANT collection based on data stored in a std::map. The CComVariant data in the map uses CComBSTRs (actually CAdapt<CComBSTR> objects) as the keys. This class uses the custom copy policy class VCUE::MapCopy, defined in VCUE_Copy.h, to convert the stored data to VARIANTs that can be passed back to COM clients. CItems derives from class VCUE::ICollectionOnSTLCopyImpl, defined in VCUE_Collections.h. The latter class derives from ICollectionOnSTLImpl and overrides get__NewEnum so that each enumerator has its own copy of the collection data. CItems implements the Add, Remove, and Clear methods in the collection interface.

Keywords

This sample uses the following keywords:

_Copy, CComEnumOnSTL, ICollectionOnSTLImpl, CAdapt, IEnumVARIANT, std::map, std::string, std::vector

Note

Some of the samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.

See Also

Other Resources

ATL Samples