"Reprint" COM Component design and application (18)--Property bag

Source: Internet
Author: User

Original: http://vckbase.com/index.php/wv/1265.html

First, preface

On the back of the book, this fallback in the introduction of the property package IPersistPropertyBag interface implementation method and call mode. A property bag, which provides support for component persistence in the form of a "name-value", and "name-value" is just as good for textual representation. The following fragment is the style that is inserted in HTML after you insert the Microsoft MonthView control ActiveX Controls:

<object classid= "clsid:232e456a-87c3-11d1-8be3-0000f8754da1" id= "MonthView1" ><param name= "_ExtentX" value = "9393" ><param name= "_extenty" value= "4974" ><param name= "_version" value= "393216" ><param name= " ForeColor "value=" 0 "><param name=" Maxselcount "value=" 7 "><param name=" MonthColumns "value=" 1 ">< param name= "currentdate" value= "38632" ><param name= "MaxDate" value= "2958465" ><param name= "MinDate" value = " -53688" ></object>

  

Saving component properties As text is more intuitive and easy to modify, as is clear in the HTML example above. Here's how to implement the IPersistPropertyBag interface in your component.

Second, the realization of the component

(1) vc6.0 Development steps

1, establish a working space (WorkSpace).

2, in this workspace, the establishment of ATL project, the example program engineering for SIMPLE18.

3. Add ATL object class, default all options. The short name of the ATL object in the sample program is property.

4, add some attributes. In the previous chapter, we only introduced the method of adding interface functions, because today is the first time to increase the interface properties, so a little more careful. The step is, after selecting the interface (iproperty) in the ClassView card, perform the right mouse button menu "Add property ..."

5, add the interface property of the BSTR type STR, the same way, and then add a long type of interface property Interger. In the example program, these two properties are actually only for demonstration, and have no practical meaning.

6, the attribute in the interface, in most cases will correspond to a member variable inside the object, so we will now add the member variable. Select the object class name and execute the right mouse button menu "Add Member Variable ..."

7, add two member variables, one is CComBSTR m_str corresponds to the interface property str; the other is a long m_integer corresponds to the interface property integer.

(2) Vc.net Development steps

1, establish a blank solution.

2. In the solution, add the ATL project. The project name in the sample program is called Simple18, and be careful not to select the "Attributed Programming" method.

3. Add an ATL class. Select simple objects for ATL. Default all options. In the sample program, the ATL class short name is property and the class name is Cmyproperty. (Note 1)

4, add some attributes. In the previous chapter, we only introduced the method of adding interface functions, because today is the first time to increase the interface properties, so a little more careful. Step is, after selecting the interface (iproperty) in the Class View card, perform the right mouse button menu "Add Properties ..."

5, add the interface property of the BSTR type STR, the same way, and then add a long type of interface property Interger. In the example program, these two properties are actually only for demonstration, and have no practical meaning.

6, the attribute in the interface, in most cases will correspond to a member variable inside the object, so we will now add the member variable. Select the object class name, execute the right mouse button menu "Add Variable ..."

7, add two member variables, one is CComBSTR m_str corresponds to the interface property str; the other is a long m_integer corresponds to the interface property integer.

(3) Implementation code

Now that the framework of our component has been completed, the following is the completion of the function function:

STDMETHODIMP Cxxx::get_str (bstr* pVal) {*pval = m_str. Copy (); return S_OK;} STDMETHODIMP Cxxx::p ut_str (BSTR newval) {m_str = Newval;return S_OK;} STDMETHODIMP Cxxx::get_integer (long* pVal) {*pval = M_integer;return S_OK;} STDMETHODIMP Cxxx::p ut_integer (LONG newval) {M_integer = Newval;return S_OK;}

  

There is nothing complicated, that is, the setting and reading functions of STR, integer two attribute values.

(4) Add IPersistPropertyBag Interface

Remember how we added IPersistStreamInit in the last book? The method of adding IPersistPropertyBag is the same, but this time we change the way that we do not derive from IPersistPropertyBag, but derive from Ipersistpropertybagimpl<>. In ATL, the system has done a lot of the default implementations of the interfaces, so we can just derive from ixxximpl<> and add some necessary mappings and variables. This is obviously much simpler than having to implement all the functions of the interface yourself. In fact, if you understand this back ipersistpropertybagimpl<> derived method, you can completely modify the implementation method in the former get books back, from the IPersistStreamInit derivation to the Ipersiststreaminitimpl<> derived.

Class atl_no_vtable Cxxx:public ccomobjectrootex<...>,public ccomcoclass<...>,public IDispatchImpl< <b>public ipersistpropertybagimpl//Manually add the derived class </b> {....... Begin_com_map (Cxxx) ... <b>com_interface_entry (ipersistpropertybag)//Manually add Interface Table </b> End_com_map () .... <b>//Manually add the attribute mapping table, which is required by Ipersistxxximpl. In the future, when you write ActiveX, the ATL Wizard will help us Add the property mapping Table Begin_prop_map (CXXX)//parameter: "attribute name", interface property ordinal (see IDL file), property Page Dialog prop_entry ("str", 1, Clsid_null) prop_entry ("Integer", 2, Clsid_null) End_prop_map () </b> .... Public: ..... <b>//. This member variable, which is required by the Ipersistxxximpl bool m_brequiressave;//, indicates whether the property data has changed and needs to be saved </b>};

  

We just add the above content by hand, without having to write any function of IPersistPropertyBag interface, how simple! The sky is out of rosy clouds, and the ground is open with red flowers ... The students who will sing this song please raise their hands, each reward Vckbase experts 500!

Third, the caller's implementation

When we read MSDN about IPersistPropertyBag interface functions, you'll find that you also need an interface ipropertybag to work with to implement property bag functionality. IPropertyBag, however, requires us to implement the interface in the caller (container). The relationship between them is as follows:

In the preceding few get books back, we have learned to derive classes from IUnknown, also learned from IDispatch derived classes, also learned from icallback derived classes ... Again, this time we're going to derive from IPropertyBag. In the example program, we added a class Cpropertybag::p ublic ipropertybag, and all the virtual functions were overloaded.

STDMETHODIMP Cpropertybag::queryinterface (const struct _GUID &iid,void * * PPV) {*PPV = This;return S_OK;} ULONG __stdcall cpropertybag::addref (void) {return 1;} Just make a fake, because this object will not quit before the program ends. ULONG __stdcall Cpropertybag::release (void) {return 0;} Make a fake, because this object will not quit until the end of the program Stdmethodimp Cpropertybag::read (lpcolestr pszpropname,variant *pvar,ierrorlog * Perrorlog) {//According to the property name specified by pszPropName, you provide the value of the property. The data type of the value is already specified in PVAL->VT. if (if it can provide the specified data) return S_ok;elsereturn E_fail;} STDMETHODIMP Cpropertybag::write (lpcolestr pszpropname,variant *pvar) {//Based on Psapropname specified property name and PVar provided value//You save it in text. return S_OK;}

  

The above is the key part of the caller (container) program, the other management and coordination sections, the reader to read the sample program code. Compile the registration component and run the caller sample program, which appears as follows:

In the edit window you can specify the value of STR and Interger, and then "Start component", then the property value you set will be set to the component by the IPersistPropertyBag interface while starting the component (restoring the persistent environment). You can then "set/Read" The properties of the component in the following property grouping operation. When the component is closed, the program saves the component's property name and value to the text of the edit window by calling the IPersistPropertyBag interface function and then re-acquiring its properties.

Iv. Summary

Understanding the functionality of this property-bag interface, you can see how IE loads the ActiveX (note 2) control and sets the state of the control.

Note 1: In Vc.net, because the system already has the Cproperty class, so here we change the name to Cmyproperty.

Note 2: Through the 18-back learning, we have learned some common interfaces of components, which lays the groundwork for us to learn about the component programming of ActiveX. Next book, we will start learning ActiveX.

"Reprint" COM Component design and application (18)--Property bag

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.