1

如何在 C# 中处理 com 事件?

我在 c# 中创建了一个类库,并使用无注册表 com 技术作为 com 组件公开。

[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);
    }
}

这是我的客户 Windows 申请表。

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();
}

当我调用 service.RaiseEvent 时,什么也没有发生。任何帮助,将不胜感激。

4

0 回答 0