1

How to handle com events in c#?

I have created one class library in c# and exposed as com component with registry free com technique.

[ComVisible(true)]
[Guid("SOME GUID")]
[InterfaceType(InterfaceType.InterfaceIsIDispatch)]
public interface ICameraEvents //Event Source Interface
{
    void OnImageCaptured(object sender, EventArgs e);
}

[Guid("SOME GUID")]
[ComVisible(true)]
public interface IService
{
    void RaiseEvent();
}

[Guid("SOME GUID")]
[ComVisible(true)]
[ComSourceInterface(typeof(ICameraEvents))]
[ClassInterface(ClassInterfaceType.None)]
public class Service : IService
{
    event EventHandler OnImageCaptured;

    public void RaiseEvent()
    { 
        if(OnImageCaptured != null)  
            OnImageCaptured(null,null);
    }
}

Here is my client windows application form.

public class MyForm : Form
{
    void Form_Load()
    {
        Type type = Type.GetTypeFromProgID("someid")//Create com object
        IService instance = (IService)Activator.CreateInstance(type);
        instance.RaiseEvent();
    }

    void OnImageCaptured(object sender, EventArgs e)
    {
       MessageBox.Show("Event Fired");
    }
}


//Re declare the interface in client application
[Guid("SOME GUID")]
[ComVisible(true)]
public interface IService
{
    void RaiseEvent();
}

When i call service.RaiseEvent nothing happens. Any help would be appreciated.

  • Works fine, after fixing the ridiculously large number of typos in the snippets. Gets to be pretty hard to guess which one is the critical one. Probably forgetting to make ICameraEvents *public*. – Hans Passant Nov 24 '17 at 15:33
  • Thanks for the response. I forgot to make interface public. Even after making it public it doesn't work. Please do not comment if you didn't get original question. I have explained the issue that i am facing. I just want to handle com events in client application. – Chintan Dave Nov 27 '17 at 15:42

0 Answers0