Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Raise COM Event within C#

Raise COM Event within C#

Scheduled Pinned Locked Moved C#
csharpquestionwpfcomhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    califax2k
    wrote on last edited by
    #1

    Hi all, I am currently trying to send a COM Event from a .NET Assembly to another application. Therefore I am applying the approach that's found multiple times across the internet. Here it's outlined for short.

    \[ComVisible(false)\]
    public delegate void OnNotifyDelegate();
    
    \[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)\]
    public interface Events
    {
        void OnNotify();
    }
    
    \[Guid(EventSource.ClsID)\]
    \[ClassInterface(ClassInterfaceType.None)\]
    \[ComSourceInterfaces(typeof(Events))\]
    public class EventSource : ServicedComponent, EventInterface
    {
        private const string ClsID = "4A235195-E8B3-473b-8F14-F7F0ADFC40E0";
        
        public event OnNotifyDelegate OnNotify;
    
        public EventSource() { }
    
        public void CauseOnNotify()
        {
            OnNotify();
        }
    }
    

    Now, to raise the OnNotify Event I created a small WPF Application with a button, whose Click handler invokes the method CauseOnNotify above. I don't show it here, because that would be verbose. What I get is a NullPointerException because OnNotify is at the time it's invoked null. My question is: None of the examples mentions a instanciation of the delegate (which is why its null, but under which circumstances does this exception not happen? Where is the instance coming from? thanks in advance Lars

    #pragma error( disable : * )

    B 1 Reply Last reply
    0
    • C califax2k

      Hi all, I am currently trying to send a COM Event from a .NET Assembly to another application. Therefore I am applying the approach that's found multiple times across the internet. Here it's outlined for short.

      \[ComVisible(false)\]
      public delegate void OnNotifyDelegate();
      
      \[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)\]
      public interface Events
      {
          void OnNotify();
      }
      
      \[Guid(EventSource.ClsID)\]
      \[ClassInterface(ClassInterfaceType.None)\]
      \[ComSourceInterfaces(typeof(Events))\]
      public class EventSource : ServicedComponent, EventInterface
      {
          private const string ClsID = "4A235195-E8B3-473b-8F14-F7F0ADFC40E0";
          
          public event OnNotifyDelegate OnNotify;
      
          public EventSource() { }
      
          public void CauseOnNotify()
          {
              OnNotify();
          }
      }
      

      Now, to raise the OnNotify Event I created a small WPF Application with a button, whose Click handler invokes the method CauseOnNotify above. I don't show it here, because that would be verbose. What I get is a NullPointerException because OnNotify is at the time it's invoked null. My question is: None of the examples mentions a instanciation of the delegate (which is why its null, but under which circumstances does this exception not happen? Where is the instance coming from? thanks in advance Lars

      #pragma error( disable : * )

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      When some other object has registered for the event? I haven't tested with COM, but it looks similar to raising a "normal" .NET event:

      if (this.MyEvent != null)
      {
      this.MyEvent(this, new EventArgs());
      }

      Only after an object has registered for MyEvent, it is different from null.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups