Use the COM interface:
When you create a COM object, the method for creating a COM Object returns a pointer to the interface. Then you can use the pointer of this interface to access other methods of this interface. The usage of pointers is the same as that of pointers in C ++. The following code segment extends the code segment in the previous article. After the directplay8 object is created, an idirectplay8peer interface pointer is created using the cocreateinstance function, and then the idirectplay8peer: initialize method is called using this interface pointer for initialization. The following code omits the verification of the error code for cleanup.
Idirectplay8peer * g_pdp = NULL;
...
Coinitialize (null );
...
HR = cocreateinstance (clsid_directplay8peer, null, clsctx_inproc_server,
Iid_idirectplay8peer, (lpvoid *) & g_pdp );
HR = g_pdp-> initialize (null, directplaymessagehandler, 0 );
Apply for additional interfaces:
In many cases, the interface pointer returned when you create an object may be the one you need. In fact, apart from iuknown, it is very common for an object to output only one interface. However, there are still many objects that can output multiple interfaces, and you can get pointers to them. If you need multiple interfaces of an object, you do not need to create a new object. Instead, you can call the iunkoown of the object :: queryInterface method to obtain the other interface pointers you need.
If you use cocreateinstance to create an object, you can get the iunkonw interface pointer. You can call iunkonwn: QueryInterface to get the pointer of each interface you need. However, if you only need to get a simple interface, this method is inconvenient, and if you use a method that does not allow you to specify the return interface pointer to create a COM object, this method also does not work. In practical applications, you usually do not need to obtain the iunkonwn pointer explicitly, because all COM objects are inherited or extended from the iunknown interface.
Extending an interface is similar to class inheritance in C ++. The sub-interface can expose all methods of the parent interface and add one or more of its own methods. In fact, you will often see that "inheritance" is used to replace "extension". What you need to remember is that inheritance is relative to the internal object. Your application cannot inherit or extend an object interface. However, you can use the sub-interface pointer to access any sub-interface or parent interface method.
You can call QueryInterface to obtain any existing interface pointer, because all interfaces are inherited from the iunknown interface. When you do this, you must provide it with the IID of the interface you requested and the address of a pointer variable pp. When this function returns, the pointer to the interface is saved in the PP variable. For example, the following code snippet creates a primary sound buffer object by calling idirectsound8: createsoundbuffer. This object exposes several interfaces. Call the createsoundbuffer method to obtain an idirectsoundbuffer8 interface. The subsequent Code calls the QueryInterface method through the idirectsoundbuffer8 interface to obtain the idirectsound3dlistener8 interface.
Idirectsoundbuffer8 * pdsnprimary = NULL;
Idirectsound3dlistener8 * pdslisterner;
...
If (failed (hR = g_pps-> createsoundbuffer (& dsbd, & pdsbsprimary, null )))
Return hr;
If (failed (hR = pdsbprimary-> queryinsterface (idd_idirectsound3dlistener8,
(Lpvoid *) & pdslistener )))
Return hr;
Note: Due to the limited translation level, errors are inevitable. If you find that the translation is incorrect, ask your friends on the road to correct it. Thank you.
Can be reproduced at will.
Translator: closeall
Translated by 2005.09.04