ATL PersistXML Implementation

Environment: VC6, ATL

The easiest way to add XML persistence to an existing IDispatch-based ATL COM object.


Think, how many times have you wanted to add PersistXML support to your existing ATL COM object?

It is true that it is supposed to be as easy as adding the IpersistPropertyBagImpl or IpersistStreamInitImpl ATL templates. Moreover, those classes already share the use of property maps.

CLSID_MyClassPropPage2, IID_IMyDual1 )

Let's do this to add XML support!

The Interface

I used the IPersistVarXML interface name because Microsoft has already taken IPersistXML for Commerce Server 2002 and it was less convenient than I wanted. (It uses BSTR for the XML input output instead of VARIANT.)

I designed my own IPersistVarXML interface (see the IDL description below). This interface supports VARIANT as an XML input and output. In fact, it can even use the ISAXContentHandler (MXXMLWriter) for the XML output!

  helpstring("IPersistVarXML Interface"),
interface IPersistVarXML : IUnknown
  [propget, helpstring("property ElementName")] HRESULT
            ElementName([out, retval] BSTR* bstrElementName);
  HRESULT LoadXML([in] VARIANT varSource, [in, optional,
                  defaultvalue(0L)] IUnknown * pAttributes);
  HRESULT SaveXML([in] VARIANT varSource);

The Implementation

The interface IPersistVarXML is implemented via the IPersistVarXMLImpl<class T> template in IPersistVarXMLImpl.h:

template <class T>
class ATL_NO_VTABLE IPersistVarXMLImpl:
                    public IPersistVarXML,
                    public ISAXContentHandlerImpl<T>

This implementation makes heavy use of the Microsoft MS XML SDK (version 3 and higher). Therefore, you need to have msxml3.dll or higher installed on your machine. (These DLLs always come with all latest versions of MS Internet Explorer or can be installed separately from

Supported Properties

If your class is already derived from one of the IPerPropertyBrowsingImpl, IPersistPropertyBagImpl, IPersistStreamInitImpl, and ISpecifyPropertyPagesImpl classes and have the BEGIN_PROP_MAP() macro, IPersistVarXMLImpl can reuse this and you do not need to do anything extra!

If you need to save an internal object or handle some XML entries "manually," you can use the BEGIN_XML_CONTENT_MAP() macro. It creates one extra MAP for the XML parser and saver. This map is optional.


HRESULT OnENTRY_HandleFunction(ISAXAttributes * pAttributes);
HRESULT OnXMLContextUser(IPersistVarXML* pAnOblect);

CComObjectGlobal<CMyInternalObject>    m_objInternal1;
CComObjectGlobal<CMyInternalObject>    m_objInternal2;

How to Add XML Support

To add PersistXML support to your ATL COM object, you need the following:

  • Add include: IPersistVarXMLImpl.h and IPersistVarXMLImpl.cpp in your project.

  • #include IPersistVarXMLImpl.h

  • Drive your class from IPersistVarXMLImpl:

  • ///////////////////////////////////////////////////////////
    // CSystemUser
    class ATL_NO_VTABLE CSystemUser :
      public CComObjectRootEx<CComSingleThreadModel>,
      public CComCoClass<CSystemUser,&CLSID_SystemUser>,
      public IDispatchImpl<ISystemUser,&IID_ISystemUser,
    public IPersistVarXMLImpl<CSystemUser>
  • Modify the com map. Add the ISAXContentHandler and IPersistVarXML interfaces to your object.
  • BEGIN_COM_MAP(CSystemUser)
  • Add a public member,
  • unsigned m_bRequiresSave:1;
    as is required for all IPersistXXX interfaces.
  • Add or modify the property map. Add only properties that need to be XML persistable!
  • BEGIN_PROP_MAP(CSystemUser)
      PROP_ENTRY_EX("Name",    1, CLSID_NULL, IID_ISystemUser)
      PROP_ENTRY_EX("Address", 2, CLSID_NULL, IID_ISystemUser)
  • Add an XML Context map for extra functionality (handle dynamic object creation and savings, not standard data types support, and so forth).
  • That's it!


Use the LoadXML(varSource) and SaveXML(varDest) functions of IPersistVarXML to save and load XML content.

VarSource can be all supported ISAXXMLReader::parse() Variant types:

"The application can use this method to instruct the reader to begin parsing a document from a variety of sources. Supported VARIANT types are: VT_BSTR, SafeArray of bytes (VT_ARRAYVT_UI1), VT_UNK(IStream), and VT_UNK(ISequentialStream). One-level referencing is also permitted.

(from the MS XML SDK manual)

varDest can be all supported IMXWriter::output() Variant types:

"Determines the output for MXXMLWriter. By default, the output property is a BSTR (string) value. You can also set this property to any implementation of the IStream interface, and the resulting document will be written into the provided IStream. Setting this property to the empty value (VT_EMPTY or " ") will return output to the internal string and reset it."

(from the MS XML SDK manual)

Because this implementation uses CComDispatchDriver::PutProperty() and CComDispatchDriver::GetProperty(), the base class must be IDispatch derived.

Sample Application

I have created two Visual Studio 6.0 projects as an example of how easy it is to add XML support to ATL objects:

  • AtlPersistXmlSample.exe—the main Dialog-based application
  • MyObjects.dll—SystemUser and MySystem classes implementations

It can be compiled for both the Unicode and ASCII platforms.

Load the AtlPersistXmlSample.dsw workspace into Visual Studio, build two projects, and run.

Click here for a larger image.

It demonstrates the XML file save/restore configuration for the whole application and dynamic XML string generation (including all internal classes and collections) and visualization.

Reference Material


Download demo project - - 51 Kb—Simple application
Download source - - 18 Kb—ATL XML persistence add-on


  • Thanks!

    Posted by Legacy on 11/14/2002 12:00am

    Originally posted by: Sergey

    Big thanks!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date