locked
Subscribing to .Net event from COM RRS feed

  • Question

  • User2036759390 posted

     Hi,

    Can somebody tell me how to subscribe to .net event from the COM through interoperability?

    namespace EventSource
    {
        [Guid("A66356CF-7408-4bf5-B02E-17158FE30DA3")]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface IBugEvents
        {
            [DispId(1)]
            void Hungry();
    
            [DispId(2)]
            void Found(string item);
        }
    [ComSourceInterfaces(typeof(IBugEvents))]
        public class Bug
        {
            public delegate void HungryEventHandler();
            public delegate void FoundEventHandler(string item);
    
            public event HungryEventHandler Hungry;
            public event FoundEventHandler Found;
    
        }
    }
    

    Can somebody tell me how to Subscribe to the Hungry and Found events from the COM?If possible help me with COM Code.

    Tuesday, July 6, 2010 6:33 AM

All replies

  • User2036759390 posted

    Help me on subscribing to .net events from COM environment.

    Help me with some COM Code. 

    Monday, July 12, 2010 12:48 AM
  • User1933979053 posted

    Hi,

    I had the same problem. You must fire the events; actually you just declare them.

    namespace EventSource
    {
        [Guid("A66356CF-7408-4bf5-B02E-17158FE30DA3")]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface IBugEvents
        {
            [DispId(1)]
            void Hungry();
    
            [DispId(2)]
            void Found(string item);
        }
    
        [ComSourceInterfaces(typeof(IBugEvents))]
        public class Bug
        {
            public delegate void HungryEventHandler();
            public delegate void FoundEventHandler(string item);
    
            public event HungryEventHandler Hungry;
            public event FoundEventHandler Found;
            
            public void OnHungry()
            {
                   if(Hungry != null)
                   {
                             Hungry();
                   }
            }
              
            public void OnFound( string item)
            {
                   if(Found != null)
                   {
                            Found(item);
                   }
            } 
        }
    }

    Here are some interesting reference:

    - http://www.softinsight.com/DotNetAndCOM.pdf 

    - COM and .NET Interoperability by Andrew Troelsen  Apress Ed

    Bye

    Friday, September 10, 2010 10:42 AM