CComPtr and CComQIPtr

Source: Internet
Author: User

Ccomptr and ccomqiptr

Ccomptr and ccomqiptr are smart interface pointer classes. When they are destroyed, they do not need to manually release interface pointers. When assigning values, they do not need manual addref, when an exception occurs, the system automatically handles the exception without any additional exception code.

The difference between ccomptr and ccomqiptr is that ccomptr can only create fixed and specific interface pointer instances. While ccomqiptr not only implements all the functions of ccomptr, but also when we assign a different type of interface pointer to ccomqiptr, ccomqiptr will automatically call the QueryInterface interface of the interface pointer, to obtain the corresponding correct interface pointer.

(1) constructor

The first parameter is the smart interface pointer type, and the second parameter is the interface ID of the smart pointer.

CComPtr <IUnknown> punk;

// The following three examples are identical.

CComPtr <IXXX> pno;

CComPtr <IXXX, <__ uuidof (IXXX)> pno;

CComPtr <IXXX, & IID_IXXX> pno;

CComQIPtr can be initialized using any type of interface pointer. If the initialization value is of the same type as CComQIPtr, the constructor simply calls AddRef of the pointer. However, if the type is different, it first calls the QueryInterface of the pointer to obtain the same type of interface pointer. When the QueryInterface fails, the internal pointer is set to NULL.

All the following code can be used to check whether the conversion is successful:

Void func (iunknown * punk) </P> <p >{</P> <p> ccomqiptr <ixxx> PNO (punk ); </P> <p> If (PNO) </P> <p >{</P> <p> // Correct Conversion </P> <p> PNO-> dosomething (); </P> <p >}< br/>

(2) Assignment

When assigning values, the following three events occur:

111. If the current pointer is not null, release the current pointer.

222. If the source pointer is not empty, then AddRef

333. Set the current pointer as the source pointer

Oprator = allows us to assign any CComPtr value to the CComQIPtr object. If necessary, we will call QueryInterface.

For example:

Ccomqiptr <ifoo> fooptr; </P> <p> ccomqiptr <Ibar> barptr; </P> <p> barptr = fooptr; <br/>

(3) CoCreateInstance Method

CComPtr provides a method for instantiating objects.

Hresult cocreateinstance (refclsid rclsid, lpunknown punkouter = NULL, DWORD dwclscontext = clsctx_all );

Hresult cocreateinstance (lpcolestr szprogid, lpunknoswn

Punkouter = NULL, DWORD dwclscontxt = clsctx_all );

The last two parameters do not matter. The first parameter does not include CLSID or string-form progid.

The sample code is as follows:

Ccomptr <ixxx> PTR; </P> <p> hresult hR = PTR. cocreateinstance (_ uuidof (ixxx); </P> <p>

(4) operator *()

When a pointer is removed from ccomptr, a reference of the internal pointer type is returned, just like a normal pointer.

(5) operator &()

Retrieving the address of a smart pointer object is actually the address of the internal pointer returned, just like a normal pointer.

(6) You must manually release all global or static variables before calling the ComUninitialize method.

How to manually release it:

111. Set pointer to NULL

222. call the Release method. Note that the Release method of the smart pointer should be called, instead of the internal pointer. The Release method of the pointer will call the Relase of the internal pointer, and then set the internal pointer to NULL, this prevents multiple interfaces from being released.

(7) In the CopyTo method, the life cycle of the copied smart pointer is completely independent.

We use the CopyTo method to copy the pointer to an out parameter.

(8) Detach and Attach Methods

When returning an interface pointer that we no longer need to the caller, we can use the Deatch method.

When we need to transfer the ownership of the original pointer to the smart pointer, use the Attach method.

(9) QueryInterface Method

You only need to input the desired Variable address of the interface type.

CComPtr <IFoo> pFoo = ...;

CComPtr <IBar> pBar;

HRESULT hr = pFoo. QueryInterface (& pBar );

(10) isdomainobject Method

The isequalobject method is used to determine whether the two interface pointers release the referenced object.

(11 )! = And = Operators

Same as normal

(12) ccomptr's unique idispatch

Ccomptr <idispatch> iptr;

Auxiliary Functions called by Properties

111. getidofname (lpcolestr lpsz, dispid * pdispid)

This method obtains the DISPID of the attribute.

222. GetProPerty (DISPID dwDispid, VARIANT * pVar)

This method is used to obtain attributes.

SetProPerty (DISPID dwDispid, VARIANT * pVar)

This method sets attributes.

333. GetPropertyByName (LPCOLESTR lpsz, VARIANT * pVar)

SetPropertyByName (LPCOLESTR lpsz, VARIANT * pVar)

Obtain and set attributes by name.

Auxiliary Functions of method call:

111. HRESULT Invoke0 (DISPID dispid, VARIANT * pvarRet = NULL );

Call a method without parameters through DISPID.

HRESULT Invoke0 (LPCOLESTR lpszName, VARIANT * pvarRet = NULL );

Call a method without parameters by method name.

HRESULT Invoke1 (DISPID dispid, VARIANT * param1, VARIANT *

PvarRet = NULL );

Call a method with a parameter through DISPID.

Hresult invoke1 (maid, variant * param1,

Variant * pvarret = NULL );

You can call a method with a parameter by using the method name.

Hresult invoken (dispid, variant * Params, int nparams, variant * pvarret = NULL );

Call a method with N parameters through dispid.

HRESULT InvokeN (maid, VARIANT * params, int nParams, VARIANT * pvarRet = NULL );

Call a method with N parameters by method name.

Note: When calling a method through the parameter list, the parameters are in reverse order, and the last parameter is element 0. Remember.

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.