4

I'm a Linux programmer and new to COM programming and I've inherited a program which I'm now trying to modify. I've got an IDL file with the following dispinterface and I'm trying to set up a callback in C++. I've been searching the web and I've found some stuff with connection points, but I don't see a simple example that I can follow, so I was wondering someone could help me out.

The dispinterface:

[
  helpstring("Event interface"),
  helpcontext(0x00000006)
]
dispinterface _DEvents {
    properties:
    methods:
        [id(0x00000001), helpstring("Occurs when about to begin."), helpcontext(0x0000000d)]
        void Starting();
        [id(0x00000002), helpstring("Occurs at the beginning."), helpcontext(0x00000011)]
        void Begin();
        [id(0x00000003), helpstring("Occurs at the end."), helpcontext(0x00000012)]
        void End();
};

The coclass:

[
  helpstring("C Class"),
  helpcontext(0x0000009e)
]
coclass C {
    [default] interface IE;
    [default, source] dispinterface _DEvents;
};

The sink interface:

[
  odl
]
interface INotifySink : IUnknown {
    HRESULT _stdcall Starting();
    HRESULT _stdcall Begin();
    HRESULT _stdcall End();
};

I've found these two articles, but I can't make heads or tails of them:

I imagine I have to make a new class that extends INotifySink, and implements the functions, but then what do I do after that?

Thanks, Jayen

P.S. Let me know if I need to provide more information and I'll edit this question. Thanks.

Jayen
  • 5,653
  • 2
  • 44
  • 65
  • have a look at http://www.codeproject.com/KB/COM/intro_connectionpoints.aspx – Preet Sangha Nov 30 '10 at 00:56
  • that doesn't quite help. 1) i already have a coclass (which i now added to the question). 2) VS2008 isn't showing me a 'Implement Connection Point...' option – Jayen Nov 30 '10 at 01:39
  • What are you using to implement the coclass? MFC? ATL? MFC and ATL both have facilities to make implementing IConnectionPointContainer and IConnectionPoint, etc. not make you lose your sanity. – Logan Capaldo Nov 30 '10 at 01:55
  • The coclass I have in the question is coming from a third-party software that is already being used in my inherited program. I tried following Preet's link, and used ATL to create an example coclass for myself, but that didn't help me. – Jayen Nov 30 '10 at 22:40

1 Answers1

1

Are you asking how to consume the extant coclass's events? To do that, you need to create an object that implements the _DEvents interface, not a new interface.

Something like:

 class EventSink : public _DEvents
 {
     AddRef() { ... }
     Release() { ... }
     QueryInterface(...) { ... }
     Starting() { printf("Starting happend\n"); }
     Begin() { ... }
     End() { ... }
 }
 EventSink *es = new EventSink;
 IE *objectOfInterest = ...;
 IConnectionPointContainer *cpc;
 objectOfInterest->QueryInterface(&cpc);
 IConnectionPoint *cp;
 cpc->FindConnectionPoint(__uuidof(_DEvents), &cp);
 cp->Advise(es, &cookie);
 objectOfInterest->somethingthatfiresanevent();

Does that make sense?

Logan Capaldo
  • 39,555
  • 5
  • 63
  • 78
  • Yes, that looks like it makes sense. I'll try it out and let you know. Do I have to override AddRef, Release, and/or QueryInterface? Now I wonder what the INotifySink interface in the third-party software is useful for... – Jayen Dec 01 '10 at 04:37
  • You'll have to implement them somehow (they're pure virtual). – Logan Capaldo Dec 01 '10 at 12:14