C# 组件事件?
我正在尝试编写一个将公开事件的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
COM 事件接收器对于不熟悉的人来说是相当邪恶的。
这些步骤基本上是
IConnectionPointContainer 和
IConnectionPoint 接口,使用
这些传递给客户端实现
好消息是,在互操作命名空间中,
有一些属性可以帮助您(大部分)自动执行此操作(ComSourceInterfacesAttribute) 有一个很好的用法示例 此处。
COM Event Sinks are pretty evil to the uninitiated.
The steps basically are
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.