simple callback in visual c++

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.

events
visual-c++
com
event-handling
callback
asked on Stack Overflow Nov 30, 2010 by Jayen • edited Nov 30, 2010 by Jayen

1 Answer

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?

answered on Stack Overflow Dec 1, 2010 by Logan Capaldo

User contributions licensed under CC BY-SA 3.0