Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / ATL
Article

Improving ATL-ActiveX serialization

Rate me:
Please Sign up or sign in to vote.
2.69/5 (10 votes)
20 Jun 20031 min read 49.4K   30   3
If an ActiveX container has to contain several Controls, serialization of controls may be made much faster with this technique.

Introduction

I have had to make an ActiveX container with the possibility to contain several controls (~100,200,600...). Apart from drawing considerations and optimizations, saving and loading times of controls may be a drawback when the number of them increase. And why? If you see the ATL code for the serialization of control's properties, you find the BEGIN_PROP_MAP() macro that defines, the properties of the controls that are serialized "one by one". At this point, is where resided my improvement.

Control properties serialization

See the following sample of ATL-Map with the properties of a control to serialize:

BEGIN_PROP_MAP(CMyControl
    PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
    PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
    PROP_DATA_ENTRY("value", m_nValue, VT_I4)
    PROP_DATA_ENTRY("mask", m_nMask, VT_I4)
    PROP_ENTRY("SafFile", DISPID_CTCSafFile, CLSID_NULL)
    ... etc.
END_PROP_MAP()

As you can see, properties' serialization are defined one by one. When the control is saved, this is translated to internal calls to IPersistStreamInit::Save(), and same when the control is loaded, with IPersistStreamInit::Load(). The key here is that, every PROP_DATA_ENTRY macro has an intrinsic call to IPersisteStreamInit::Load() or Save() (remember that an ATL-ActiveX control inherits and implements this interface by IPersistStreamInitImpl<> template).

If you have hundred of controls to save at the same time, and they have several properties saving or loading by one call to IPersistStreamInit interface per property, this may take several time. This can be improved by the following code:

BEGIN_PROP_MAP(CMyControl) 
    // (you left empty de properties map)
END_PROP_MAP() 
typedef struct CONTROLPROPERTIES
{
    // Define here the control properties
} CONTROLPROPERTIES;
CONTROLPROPERTIES m_properties;
// IPersistStreamInit 
public:
STDMETHOD(Load)( LPSTREAM pStm ) 
{ 
    if ( pStm == NULL ) return( E_POINTER );
    pStm->Read( &m_properties, sizeof(m_properties), NULL );
    return( S_OK );
}
STDMETHOD(Save)( LPSTREAM pStm, BOOL fClearDirty ) 
{
    if ( pStm == NULL ) return( E_POINTER );
    pStm->Write( &m_properties, sizeof(m_properties), NULL );
    if ( fClearDirty ) SetDirty(FALSE);
    return( S_OK );
}

With this way for saving and loading, every control is saved or loaded with an unique call to IPersistStreamInit::Save() or Load(), not one call per property, and so we can save time!!!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
Computer engineer from 2000; specialized in critical software for SCADA systems; expert in Win32, C++, graphical programming with GDI,GDI+,MFC,etc. One of the less people who has integrated VBA in an comercial application in Spain ( Wink | ;-) ).
I like literature, jogging and reading computer books on my spare time; nowdays I live with my girlfriend in Las Pajanosas (near Sevilla).



Comments and Discussions

 
GeneralNice! Pin
Ralph Wetzel21-Jun-03 22:11
Ralph Wetzel21-Jun-03 22:11 
GeneralRe: Nice! Pin
René Greiner23-Jun-03 2:54
René Greiner23-Jun-03 2:54 
GeneralRe: Nice! Pin
TW23-Jun-03 5:25
TW23-Jun-03 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.