Not able to handle com events in c#

73 Views Asked by At

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.

0

There are 0 best solutions below