COM Component Design and Application (18) -- attribute package

Source: Internet
Author: User

COM Component Design and Application (18) -- attribute package
Http://www.vckbase.com/document/viewdoc? Id = 1547
Author: Instructor Yang

Download source code

I. Preface
This article describes the implementation and call methods of the ipersistpropertybag interface. Attribute packages provide continuous support for components in the form of "name-value", while "name-value" is suitable for representation in text mode. The following snippets are
The style after the Microsoft monthview Control ActiveX control is inserted in HTML:

<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>

The component attributes are saved in text format, which is intuitive and easy to modify. The <Param name = "attribute name" value = "value"> in the preceding HTML example is clear. The following describes how to implement the ipersistpropertybag interface in a component.

Ii. component implementation
(1) vc6.0 development steps
1. Create a workspace ).
2. Create an ATL Project in this workspace. The sample project is simple18.
3. added the ATL object class. All options are selected by default. The short name of the ATL object in the example program is property.
4. Add some attributes. In the previous chapter, we only introduced the method for adding an interface function. Because today is the first time that an interface attribute is added, it is a little more detailed. Step: Select an interface (iproperty) in the classview card and right-click the interface and choose "add property ..."

5. Add the interface attribute 'str' of 'bstr' type. In the same way, add another interface attribute 'interger of 'long' type. In the example program, these two attributes are only for demonstration and have no practical significance.



6. In most cases, attributes in the interface correspond to a member variable in the object. Therefore, we need to add member variables. Select the object class name and right-click the menu "add member variable ...."

7. Add two member variables. One is ccombstr m_str, which corresponds to the interface property STR; the other is long m_integer, which corresponds to the interface property integer.

(2) vc.net development steps
1. Create a blank solution.
2. Add an ATL Project in the solution. In the example program, the project name is simple18. Be sure not to select the "attribute-based programming" method.
3. Add the ATL class. Select "Simple Object of ATL ". All options are selected by default. In the example program, the short name of the ATL class is property and the class name is cmyproperty. (Note 1)
4. Add some attributes. In the previous chapter, we only introduced the method for adding an interface function. Because today is the first time that an interface attribute is added, it is a little more detailed. Step: Select an interface (iproperty) in the Class View card and right-click the interface and choose "add attribute ..."



5. Add the interface attribute 'str' of 'bstr' type. In the same way, add another interface attribute 'interger of 'long' type. In the example program, these two attributes are only for demonstration and have no practical significance.



6. In most cases, attributes in the interface correspond to a member variable in the object. Therefore, we need to add member variables. Select the object class name, right-click the menu, and choose "add variable ...."



7. Add two member variables. One is ccombstr m_str, which corresponds to the interface property STR; the other is long m_integer, which corresponds to the interface property integer.


(3) implementation code
So far, the framework of our components has been completed. The following describes how to implement the function:

STDMETHODIMP Cxxx::get_str(BSTR* pVal){*pVal = m_str.Copy();return S_OK;}STDMETHODIMP Cxxx::put_str(BSTR newVal){m_str = newVal;return S_OK;}STDMETHODIMP Cxxx::get_integer(LONG* pVal){*pVal = m_integer;return S_OK;}STDMETHODIMP Cxxx::put_integer(LONG newVal){m_integer = newVal;return S_OK;}

There is nothing complicated, that is, the setting and reading functions of STR and integer attribute values.
(4) add ipersistpropertybag Interface
Remember how we added ipersiststreaminit in the previous book? The method for adding ipersistpropertybag is the same, but this time we use another method, that is, we do not derive from ipersistpropertybag, but from ipersistpropertybagimpl <>. In ATL, the system has completed the default implementation of many interfaces.
Ixxximpl <> derived, and then add some necessary mappings and variables. This is obviously much easier than implementing all functions of the interface. In fact, if you understand this back ipersistpropertybagimpl <> derived method, you can modify the implementation method in the previous back book to derive from ipersiststreaminit and improve it to derive from ipersiststreaminitimpl <>.

Class atl_no_vtable CXXX: Public ccomobjectrootex <...>, public ccomcoclass <...>, public idispatchimpl <...>,Public ipersistpropertybagimpl <CXXX> // manually add a derived class{... Begin_com_map (CXXX ).........Com_interface_entry (ipersistpropertybag) // manually add an interface tableEnd_com_map ().........// Manually add the property ing table, which is required by ipersistxxximpl. // When you write ActiveX in the future, the ATL Wizard will help us add the attribute ing table begin_prop_map (CXXX) // parameter: "attribute name", interface attribute serial number (see the IDL file ), property page dialog window prop_entry ("str", 1, clsid_null) prop_entry ("integer", 2, clsid_null) end_prop_map ()... Public :.........// This member variable is the bool m_brequiressave required by ipersistxxximpl; // indicates whether the attribute data has changed and needs to be saved};

We only need to manually add the above content, instead of writing any ipersistpropertybag interface functions by ourselves. It's easy! The sky is colorful, and the ground is filled with red flowers... please raise your hand if you will sing this song. Everyone will reward vckbase experts with 500 points!

Iii. Implementation of callers
When reading the ipersistpropertybag interface function of msdn, you will find that an interface ipropertybag must be used together to implement the attribute Package function. Ipropertybag needs to be implemented in the caller (container. The relationship between them is as follows:



In the previous books, we have learned the iunknown derived class, the idispatch derived class, And the icallback derived class... Similarly, this time we have to derive from ipropertybag. In the example program, a class cpropertybag: Public ipropertybag is added, and all virtual functions are reloaded.

Stdmethodimp cpropertybag: QueryInterface (const struct _ guid & IID, void ** NPV) {* GMM = This; return s_ OK;} ulong _ stdcall cpropertybag: addref (void) {return 1;} // make a false statement, because this object will not exit until the end of the program. ulong _ stdcall cpropertybag: release (void) {return 0;} // make a false statement, because this object will not exit stdmethodimp cpropertybag: Read (lpcolestr pszpropname, variant * pvar, ierrorlog * perrorlog) {// provide the value of this attribute based on the attribute name specified by pszpropname. // The data type of the value has been specified in pval-> vt. If (if the specified data can be provided) return s_ OK; elsereturn e_fail;} stdmethodimp cpropertybag: Write (lpcolestr pszpropname, variant * pvar) {// Save the property name specified by psapropname and the value provided by pvar to the text. Return s_ OK ;}

The above is the key part of the caller (container) program. For other management and coordination parts, the reader will read the sample program code. Compile and register the component, and run the caller sample program, as shown below:



In the edit window, you can specify the STR and interger values and then "Start the component". Then, the attribute value you set will start the component at the same time, you can use the ipersistpropertybag interface to set it to the component (restoring the persistent environment ). Then, you can set/read the attributes of the component in the following property grouping operation. When the component is disabled, the program calls ipersistpropertybag
Interface function, and re-obtain the component property name and value and save it to the text in the editing window.

Iv. Summary
After understanding the functions of this property package interface, you can understand how IE loads ActiveX (note 2) controls and sets the control status.

NOTE 1: In vc.net, because the system already has a cproperty class, we change the name to cmyproperty.
NOTE 2: through the 18-back learning, we have learned some common interfaces of components, laying the foundation for learning ActiveX component programming. Next, we will start to learn ActiveX.

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.