2

I'm trying to work with an old com control (a control array), the following samples: 5435293, 39541, 5497403, 5738092 explain (or at least what I understand) how to handle events of control arrays with .net controls, so they have Sender and EventArgs.

My question will be: How can you handle the events of an old com control array?.

EDIT: The array will be created dynamically at the start, for example: Q. How many connections do you want? A. 5

example: the control has this event: control_connected(int status, string description)

I can make some function with the same arguments and asign it to the connected event, but i cant figure out how to do it with a control array.

Ty so much for your help, and sorry about the crappy English... I'm not a navite English speaker

Community
  • 1
  • 1
Jonathan B
  • 169
  • 1
  • 1
  • 11

1 Answers1

1

COM events have a different modal, you do not have one handler per event, you have an event sink object that hooks every event the COM server plan to raise. If you just hook the ActiveX events with delegates, event sink RCWs will be created and may cause crashes later, so I assume you are creating your own event sink class.

Since you have your own event sink class, you must follow the event publisher's event signature. The signatures do not have a sender argument, since the COM server assumes you have a reference to the sender, thus there is no need to send it again every time an event is raised.

You can, of cause, cache the server's reference in your event sink object for later use. Your event sink object can declare its own version of managed events with a sender parameter, and pass the cached COM server as the sender argument when it raises events.

Something like

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[TypeLibType(TypeLibTypeFlags.FHidden)]
[Guid("eventGuid")]
[CLSCompliant(false)]
public interface IEvent
{       
   [DispId(123)]
   void control_connected(int status, string description);
}
public class EventSink:IEvent
{
   object control;
   public EventSink (object control)
   {
        this.control=control;
   }  
   public event EventHandler<ControlConnectedEventArgs> ControlConnected;
   void control_connected(int status, string description);
   {
       EventHandler<ControlConnectedEventArgs> temp=this.ControlConnected;
       if(temp!=null)
           temp(this.control, new ControlConnectedEventArgs(status,description));
   }
}

If you have an array of COM servers, just declare an array of event sinks, attach each sink to each COM server with ConnectionPointCookie, and wire the event handlers from the event sink instead of the COM servers.

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
  • Ty so much for your help, not only you helped me to answer it but you also show me where i can find more information. – Jonathan B Aug 18 '11 at 02:31