simple callback in visual c++


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"),
dispinterface _DEvents {
        [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"),
coclass C {
    [default] interface IE;
    [default, source] dispinterface _DEvents;

The sink interface:

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.

asked on Stack Overflow Nov 30, 2010 by Jayen • edited Nov 30, 2010 by Jayen

1 Answer


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;
 IConnectionPoint *cp;
 cpc->FindConnectionPoint(__uuidof(_DEvents), &cp);
 cp->Advise(es, &cookie);

Does that make sense?

answered on Stack Overflow Dec 1, 2010 by Logan Capaldo

User contributions licensed under CC BY-SA 3.0