3

I have an IConnectionPointContainer and know the connection point whose events I want to sink. Now I need to write a class containing callback methods for these events.

So basically I am doing this:

OleCheck((ExtIntf as IConnectionPointContainer).FindConnectionPoint(IID_IEvents, cp));
OleCheck(cp.Advise(MySink {<--this one I need}, Cookie));

I need to provide MySink and I know how to do it so that MyIntf.Invoke gets called. But I don't want to map dispids to event methods by hand.

That means I don't want to do this in my Invoke:

// Handle the event
case DispID of
  102   :  OnStatusTextChange(pdpParams^.rgvarg^[lpDispIds[0]].bstrval);
  104   :  OnDownloadComplete;
  105   :  OnCommandStateChange(pdpParams^.rgvarg^[lpDispIds[0]].lval,
                                pdpParams^.rgvarg^[lpDispIds[1]].vbool);
  106   :  OnDownloadBegin;
  108   :  OnProgressChange(pdpParams^.rgvarg^[lpDispIds[0]].lval,
                            pdpParams^.rgvarg^[lpDispIds[1]].lval);
  112   :  OnPropertyChange(pdpParams^.rgvarg^[lpDispIds[0]].bstrval);
  {... doh}

For the connection point IID_IEvents I have both the interface IEvents and the dispinterface declaration (from Delphi type library import). Can't I just implement IEvents somehow? I want a clean class containing methods each one being a callback for one of the events offered by the IID_IEvents connection point.

I have great hope that somebody knows maybe

  • a base class I can inherit from that takes the job of calling my event methods
  • a Delphi wizard that creates the proper class for me
  • the tree I need to make my magic wand of to eventually master COM

Any tips?

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
  • This is a great question! I believe you want auto-generate a dual-interfaced event-sink class that implements IDispatch, and that you should be able to get it done for you using the type library importer. – Warren P Aug 09 '11 at 17:12
  • @Warren Thanks, I really hope somebody has the flash of inspiration to make our lifes easier :) Don't want to handle dispids again :( – Heinrich Ulbricht Aug 09 '11 at 17:17
  • Did you already do the usual `Component -> Import Component -> Type Library?` – Warren P Aug 09 '11 at 17:18
  • Um, yes I did. Are you hinting that the solution can be found there? Will check. – Heinrich Ulbricht Aug 09 '11 at 17:19
  • 1
    Well this stuff, if you selected it would be in the MyThing_TLB.pas you created. – Warren P Aug 09 '11 at 17:29
  • Not much to toggle in the import dialog. I had component wrapper creation disabled, but enabling it did just create some classes I don't need. So no luck here. – Heinrich Ulbricht Aug 09 '11 at 17:34
  • You may use late binding instead (CreateOleObject) and let all parameters and names be resolved at runtime. But not the most secure solution, and in all cases the slowest IMHO. – Arnaud Bouchez Aug 09 '11 at 18:34
  • Actually those classes, including the TOleServer are exactly what you need. Instantiate the event-handler component, and call its GetDefaultInterface method, which will do an internal `Connect` method in the TOleServer, providing you with an invocable set of callback events (OnThisThing,OnThatThing, etc) that are mapped to Delphi events! Just because the classes may not be what you thought you needed, doesn't mean they don't do either exactly what you wanted, or could serve as the original code to modify, to do exactly what you want. – Warren P Aug 10 '11 at 12:56
  • Unfortunately there is no event handler component, just a manager component. There is nothing implementing the event interface. If you are interested, this is about the Microsoft Feeds 2.0 Object Library. I need IID_IFeedEvents. – Heinrich Ulbricht Aug 11 '11 at 17:26

1 Answers1

1

TechVanguards has a tool that generates the code for you: COM Sink Event Generator.

You can also see my question; where i moaned about having to write what you're faced with writing.

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Thanks Ian, the generator is the solution! I ignored it at an earlier glance because the last update was in 2002. But it seems that hardly matters with over a decade old COM. – Heinrich Ulbricht Aug 15 '11 at 21:22
  • i remember the line from someone at Microsoft that, "*COM isn't dead, but it is done*". Nothing new is being added to it. But most new features of Windows are still being exposed through COM interfaces. – Ian Boyd Aug 15 '11 at 21:25