Design and application of COM components (17)--Persistence

Source: Internet
Author: User

First, the preface

When we write programs, we often need to achieve this requirement:

Example one, the program runs produces a window, the user needs to record the window position when closes, in order to maintain the position unchanged next time;

For example, due to the long running time of the program, part of today's implementation will continue tomorrow. Then restore the previous state before the next run;

... ... ... ...

Teacher of Wisdom: how can these requirements be realized?

Ignorant students: This simple, as long as the withdrawal of the necessary information before the program to save to the file, the next time to run from the file read out, set the OK.

Wisdom Teacher: Well, yes, this classmate's thought deserves praise.

Ignorant students: Sorry, this is to thank the teacher's cultivation, I admire you like surging river water ...

Wisdom Teacher: Don't stink p, I have not said finish that ... If you need to extract and save a lot of information, the structure is very complex ... What to do?

Ignorant student: Well done, I design a structure to record this information.

Teacher of wisdom: Well ... Not bad. But what do you do if the information provider is a module written by someone else and changes frequently with different versions?

Ignorant Students: ...

Wisdom Teacher: The way to solve these problems is---persistence.

Second, the principle

Persistence, also called permanence. The component side provides the IPERSISTXXX interface, the caller (container) provides the storage media, such as file, memory, registry, streaming, text ... La La. To save, the caller uses the Ipersistxxx::save () interface function to let the component store the property information itself, while the caller does not care about the storage format and storage content; When the state is restored, the caller opens the storage media and then calls Ipersistxxx: The Load () interface function lets the component read the property information itself and complete the initialization settings.

At present, Microsoft has defined the following types of continuous interface, enough to meet your needs. As long as we implement one or several of the persistent interfaces in the component we write, the caller can negotiate the preservation and state restoration of the property information in a unified manner with our components.

Continuity interface Brief description
IPersist The root of all persistent interfaces, and most of the following interfaces derive from it. This interface is simple, only one function getclassid () it returns the component's CLSID number so that the caller can save this number for future CoCreateInstance () startup components.

Implementing this function is also very simple, as long as you return to the clsid_xxx in your component, or the more convenient way is to return Getobjectclsid ().

IPersistStream

derives from the IPersist and adds 4 functions to read and write component property information from the stream (IStream).

IsDirty () Whether the internal properties of the component have changed. Provides a basis for whether the caller needs to save information
Load () Read the information from the IStream, initialize the component properties
Save () To save the attribute information in IStream
GetSizeMax () Returns the size of the information so that the caller can open up space in advance
IPersistStreamInit Derived from the IPersistStream and added a function initnew () to complete a default component property initialization.

This persistence interface is most commonly used and is implemented in the example in this article.

Ipersistmemory Similar to IPersistStreamInit, but uses a memory block instead of a IStream stream with a variable size.
IPersistStorage Similar to IPersistStream, but saving property information uses a storage istorage and can have multiple IStream in a istorage.
IPersistFile Similar to IPersistStream, but storage media is a file.
IPersistPropertyBag Saves information using the text of a property bag (property name, property value). In IE browsers, HTML embedded ActiveX controls typically use this method.

Inserting controls in HTML, <param name= "property name" value= "value" > You should have seen it?!

In the next article, we'll introduce this interface. Because in ActiveX, it is too common.

IPersistPropertyBag2 extended the IPersistPropertyBag interface. Provides a richer range of property management functions.
Ipersistmoniker A persistent interface for naming (moniker) storage and read state.
Ipersisthistory Run on IE to store and read the persistent interface of the state while the user is browsing the WEB page.

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.