开发者

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"),
  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:

  • http://www.codeproject.com/KB/COM/TEventHandler.aspx
  • http://www.codeproject.com/KB/COM/com_in_c5.aspx

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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜