C# 组件事件?

发布于 2024-07-27 22:23:45 字数 1031 浏览 1 评论 0原文

我正在尝试编写一个将公开事件的 C# 组件。 该组件将由非托管 C++ 应用程序导入。 根据一些教程,我想出了这个代码(针对 C# 端):

namespace COMTest
{
[ComVisible(true),
Guid("02271CDF-BDB9-4cfe-B65B-2FA58FF1F64B"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITestEvents
{
    void OnTest();
}

[ComVisible(true),
Guid("87BA4D3A-868E-4233-A324-30035154F8A4")]
public interface ITest
{
    void RaiseTest();
} // End of ITest

[ComVisible(true),
Guid("410CD174-8933-4f8c-A799-8EE82AF4A9F2"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ITestEvents))]
public class TestImplimentation : ITest
{
    public TestImplimentation()
    {
    }

    public void RaiseTest()
    {
        if (null != OnTest)
            OnTest();
    }

    public delegate void Test (); //No need to expose this delegate
    public event Test OnTest;
}
}

现在我的 C++ 代码有一个简单的:

#import "COMTest.tlb" named_guids raw_interfaces_only

它生成一个 tlh 文件。 这个 tlh 文件包含除我的事件(OnTest)之外的所有内容。 我做错了什么?

I am attempting to write a C# component which will expose events. The component is to be imported by an unmanaged C++ application. According to a few tutorials I have come up with this code (for the C# side):

namespace COMTest
{
[ComVisible(true),
Guid("02271CDF-BDB9-4cfe-B65B-2FA58FF1F64B"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITestEvents
{
    void OnTest();
}

[ComVisible(true),
Guid("87BA4D3A-868E-4233-A324-30035154F8A4")]
public interface ITest
{
    void RaiseTest();
} // End of ITest

[ComVisible(true),
Guid("410CD174-8933-4f8c-A799-8EE82AF4A9F2"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ITestEvents))]
public class TestImplimentation : ITest
{
    public TestImplimentation()
    {
    }

    public void RaiseTest()
    {
        if (null != OnTest)
            OnTest();
    }

    public delegate void Test (); //No need to expose this delegate
    public event Test OnTest;
}
}

Now my c++ code has a simple:

#import "COMTest.tlb" named_guids raw_interfaces_only

Which generates a tlh file. This tlh file contains everything but my event (OnTest). What am I doing incorrectly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2024-08-03 22:23:45

COM 事件接收器对于不熟悉的人来说是相当邪恶的。

这些步骤基本上是

  • 创建一个传出(源)接口
  • 实现一个
    IConnectionPointContainer 和
    IConnectionPoint 接口,使用
    这些传递给客户端实现
    消息是,在互操作命名空间中,

有一些属性可以帮助您(大部分)自动执行此操作(ComSourceInterfacesAttribute) 有一个很好的用法示例 此处

COM Event Sinks are pretty evil to the uninitiated.

The steps basically are

  • create an outgoing (source) interface
  • Implement an
    IConnectionPointContainer and
    IConnectionPoint Interfaces, use
    these to pass a client implementation
    of the source Interface

The good news is that in the interop namespace there are attributes to help you do this (mostly) automatically (ComSourceInterfacesAttribute) There is a decent example of its usage here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文