42
COM Connection Points 主主主 主主主

COM Connection Points

  • Upload
    albin

  • View
    20

  • Download
    1

Embed Size (px)

DESCRIPTION

COM Connection Points. 主講人:虞台文. Content. The Client-Object-Sink Relationship IConnectionPointContainer & IConnectionPoint Implementation using ATL/COM. COM Connection Points. The Client-Object-Sink Relationship. Connectable Object. Sink. The Process of Connecting Source & Sink. - PowerPoint PPT Presentation

Citation preview

Page 1: COM Connection Points

COM Connection Points

主講人:虞台文

Page 2: COM Connection Points

Content

The Client-Object-Sink Relationship IConnectionPointContainer &IConnectionPoint

Implementation using ATL/COM

Page 3: COM Connection Points

COM Connection Points

The Client-Object-Sink Relationship

Page 4: COM Connection Points

The Process of Connecting Source & Sink

ConnectableObject

ConnectableObject

ClientClient

Sink

Client passes sink interface pointer to connectable object.

Connectable object calls sink when appropriate.

Page 5: COM Connection Points

One-to-Many Relationship

ConnectableObject

ConnectableObject

ClientClient

Sink

ClientClient

Sink

Sink

ClientClient

Sink

Page 6: COM Connection Points

Many-to-One Relationship

ConnectableObject

ConnectableObject

ClientClient

SinkConnectable

Object

ConnectableObject

ConnectableObject

ConnectableObject

Page 7: COM Connection Points

COM Connection Points

IConnectionPointContainer

&

IConnectionPoint

Page 8: COM Connection Points

Connection Point ContainerConnection Point Container

IConnectionPointContainer &IConnectionPoint

Connection Point 1

Connection Point 2

Connection Point n

IConnectionPointContainer

IConnectionPoint

IConnectionPoint

IConnectionPoint

. .

.

Page 9: COM Connection Points

IConnectionPointContainer

interface IConnectionPointContainer : IUnknown

{

HRESULT EnumConnectionPoints(

IEnumConnectionPoints **ppEnum) = 0;

HRESULT FindConnectionPoint(REFIID riid,

IConnectionPoint **ppCP) = 0;

};

interface IConnectionPointContainer : IUnknown

{

HRESULT EnumConnectionPoints(

IEnumConnectionPoints **ppEnum) = 0;

HRESULT FindConnectionPoint(REFIID riid,

IConnectionPoint **ppCP) = 0;

};

Page 10: COM Connection Points

IConnectionPoint

interface IConnectionPoint : IUnknown

{

HRESULT GetConnectionInterface(IID *pIID) = 0;

HRESULT GetConnectionPointContainer(

IConnectionPointContainer **ppCPC) = 0;

HRESULT Advise(IUnknown *pUnk, DWORD *pdwCookie) = 0;

HRESULT Unadvise(DWORD dwCookie) = 0;

HRESULT EnumConnections(IEnumConnections **ppEnum) = 0;

};

interface IConnectionPoint : IUnknown

{

HRESULT GetConnectionInterface(IID *pIID) = 0;

HRESULT GetConnectionPointContainer(

IConnectionPointContainer **ppCPC) = 0;

HRESULT Advise(IUnknown *pUnk, DWORD *pdwCookie) = 0;

HRESULT Unadvise(DWORD dwCookie) = 0;

HRESULT EnumConnections(IEnumConnections **ppEnum) = 0;

};

Page 11: COM Connection Points

COM Connection Points

Implementation using

ATL/COM

Page 12: COM Connection Points

Step1. Create ATL/COM Project

Page 13: COM Connection Points

Step1. Create ATL/COM Project

Page 14: COM Connection Points

Step2. Create ATL Object

Page 15: COM Connection Points

Step2. Create ATL Object

Page 16: COM Connection Points

Step2. Create ATL Object

Page 17: COM Connection Points

Step2. Create ATL Object

Page 18: COM Connection Points

Step2. Create ATL Object

Page 19: COM Connection Points

Step2. Create ATL Object

Page 20: COM Connection Points

Step2. Create ATL Object

Page 21: COM Connection Points

Step3. Modification of IDL// Timer.idl : IDL source for Timer.dll. . . . . . . . . . . . . . . . . . . . library TIMERLib{

importlib("stdole32.tlb");importlib("stdole2.tlb");[

uuid(A1EE119D-1107-4BB9-B23A-2D338F7F955F),helpstring("_IPeriodicTimerEvents Interface")

]dispinterface _IPeriodicTimerEvents{

properties:methods:

};

[uuid(FB1C62DE-5499-4C69-BC79-3EC6D5260D05),helpstring("PeriodicTimer Class")

]coclass PeriodicTimer{

[default] interface IPeriodicTimer;[default, source] dispinterface _IPeriodicTimerEvents;

};};

// Timer.idl : IDL source for Timer.dll. . . . . . . . . . . . . . . . . . . . library TIMERLib{

importlib("stdole32.tlb");importlib("stdole2.tlb");[

uuid(A1EE119D-1107-4BB9-B23A-2D338F7F955F),helpstring("_IPeriodicTimerEvents Interface")

]dispinterface _IPeriodicTimerEvents{

properties:methods:

};

[uuid(FB1C62DE-5499-4C69-BC79-3EC6D5260D05),helpstring("PeriodicTimer Class")

]coclass PeriodicTimer{

[default] interface IPeriodicTimer;[default, source] dispinterface _IPeriodicTimerEvents;

};};

Page 22: COM Connection Points

Step3. Modification of IDL

// Timer.idl : IDL source for Timer.dll. . . . . . . . . . . . . . . . . . . . library TIMERLib{

importlib("stdole32.tlb");importlib("stdole2.tlb");[

uuid(A1EE119D-1107-4BB9-B23A-2D338F7F955F),helpstring("_IPeriodicTimerEvents Interface")

]interface _IPeriodicTimerEvents : IUnknown{};

[uuid(FB1C62DE-5499-4C69-BC79-3EC6D5260D05),helpstring("PeriodicTimer Class")

]coclass PeriodicTimer{

[default] interface IPeriodicTimer;[default, source] interface _IPeriodicTimerEvents;

};};

// Timer.idl : IDL source for Timer.dll. . . . . . . . . . . . . . . . . . . . library TIMERLib{

importlib("stdole32.tlb");importlib("stdole2.tlb");[

uuid(A1EE119D-1107-4BB9-B23A-2D338F7F955F),helpstring("_IPeriodicTimerEvents Interface")

]interface _IPeriodicTimerEvents : IUnknown{};

[uuid(FB1C62DE-5499-4C69-BC79-3EC6D5260D05),helpstring("PeriodicTimer Class")

]coclass PeriodicTimer{

[default] interface IPeriodicTimer;[default, source] interface _IPeriodicTimerEvents;

};};

Page 23: COM Connection Points

Step4. Add Properties and Methods

Page 24: COM Connection Points

Step4. Add Properties and Methods

Page 25: COM Connection Points

Step4. Add Properties and Methods

Page 26: COM Connection Points

Step4. Add Properties and Methods

Page 27: COM Connection Points

Step4. Add Properties and Methods

Page 28: COM Connection Points

Step4. Add Properties and Methods

Page 29: COM Connection Points

Step4. Add Properties and Methods

Page 30: COM Connection Points

Step4. Add Properties and Methods

Page 31: COM Connection Points

Step4. Add Properties and Methods

Page 32: COM Connection Points

Step4. Add Properties and Methods

Page 33: COM Connection Points

Step4. Add Properties and Methods

Page 34: COM Connection Points

Step4. Add Properties and Methods

Page 35: COM Connection Points

Step5. Build Type Library

Page 36: COM Connection Points

Step5. Build Type Library

Page 37: COM Connection Points

Step5. Build Type Library

Page 38: COM Connection Points

Step6. Implement Connection Point

Page 39: COM Connection Points

Step6. Implement Connection Point

Page 40: COM Connection Points

Step7. Detail the Source Code

See the Source Code for detail of other implementation.

Page 41: COM Connection Points

Demonstration

Page 42: COM Connection Points

Exercise

1. Implement an alarm clock object using ATL. In this COM object, you can set the alarm time, and the object will notify its sink when the time is up.