COM object and connection point mechanism and MFC program implementation

Source: Internet
Author: User
Tags implement

1, the basic principle of the connecting object and the connection point mechanism

To provide greater interaction between component objects and customers, component objects also need to actively communicate with customers. Component objects communicate with customers through an outbound interface (outgoing Interface). If a Component object defines one or more outbound interfaces, this component object is called a connecting point object.

The so-called outbound interface is also a COM interface. Each outbound interface contains a set of member functions, each representing an event, a notification, or a request. But these interfaces are implemented in the customer's event receiver (sink), so the interface is called out. The event sink is also a COM object.

The connected object must implement a IConnectionPointContainer interface for managing all the outbound interfaces. Each outbound interface corresponds to a connection point object, and the connection point object implements the IConnectionPoint interface. It is through the IConnectionPoint interface that the customer establishes a connection to the connected object. Each connection is described with a connectdata structure.

ConnectData contains two members: Iunknown* Punk and DWORD Dwcookie. Punk corresponds to the IUnknown interface pointer of the event receiver in the customer; Dwcookie is a 32-bit integer generated by the connection point object to uniquely identify this connection.

The client has access to all the connection points of the IEnumConnectionPoints object through an enumerator interface implemented by the reachable object. But to get the IEnumConnectionPoints interface pointer, through the iconnectionpointcontainer::enumconnectionpoints (ienumconnectionpoints**) function, This function returns the enumerator interface pointer.

Through another enumerator interface ienumconnections with a reachable object, all connections on a connection point can be accessed by both the client and the connected object. The IEnumConnections interface pointer can be obtained through the iconnectionpoint::enumconnections (ienumconnections**) function.

To sum up, a connecting object must implement four interfaces: IConnectionPointContainer, IConnectionPoint, IEnumConnectionPoints, IEnumConnections. Please read the MSDN documentation for the definitions of these four interfaces.

The following example is now a brief description of the process of connecting objects to customer communication. In the following example, the connecting object Connobject defines the interface Ieventsink, corresponds to the interface, implements a connection point object Sampleconnpoint (This object implements the connection point interface corresponding to the outbound interface IConnectionPoint, Interface ID is iid_ieventsink).

1. The customer calls M_piunknown->queryinterface (IID_IConnectionPointContainer, (void**) after acquiring the IUnknown interface pointer M_piunknown of the object that can be connected. &pconnptcont); If the call succeeds, Pconnptcont will hold the IConnectionPointContainer interface pointer for the object to be connected. If the call is unsuccessful, it indicates that the object is not a connected object.

2. Call Pconnptcont->findconnectionpoint (IID_IEVENTSINK,&PCONNPT). If the call succeeds, PCONNPT will store the connection point interface IConnectionPoint Pointer to the connection point object Sampleconnpoint corresponding to the outbound interface Ieventsink; If the call is unsuccessful, Indicates that the interface Ieventsink is not supported by the connected object.

3. Call Pconnpt->advise (Pieventsink,&m_dwcookie) to establish the connection between the event receiver (EventSink) and the connection point. Where Pieventsink is a pointer to the client event receiver IUnknown interface passed through this function to the connected object so that the connected object initiates communication to the customer; M_dwcookie is the connection identity, which is saved by the customer by the Connection object setting, The customer also uses this value to disconnect.

4. A connected object can invoke a method in the client event receiver through a connection point. After the customer has successfully established a connection to the connection point, a pointer to the client's event receiver interface has been saved in the connection point and can be called pconnpt->getconnections () for acquisition.

5. The client invokes Pconnpt->unadvise (M_dwcookie) to cancel the connection, while calling Pconnpt->release () to free the connection point object.

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.