C#组件事件? [英] C# component events?
问题描述
我试图写一个C#组件将暴露事件。该组件将由非托管C ++应用程序导入。根据一些教程我已经拿出这个代码(对于C#侧):
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;
}
}
现在我的c ++代码有一个简单的:
Now my c++ code has a simple:
#import "COMTest.tlb" named_guids raw_interfaces_only
其中生成一个tlh文件。这个tlh文件包含除了我的事件(OnTest)之外的所有内容。我做错了什么?
Which generates a tlh file. This tlh file contains everything but my event (OnTest). What am I doing incorrectly?
推荐答案
COM事件接收器对于未经授权的用户来说是非常邪恶的。
COM Event Sinks are pretty evil to the uninitiated.
这些步骤基本上是
- 创建传出(来源)接口
- 实现
IConnectionPointContainer和
IConnectionPoint接口,使用
这些传递客户端实现
源接口
好的消息是,在interop命名空间中有属性可以帮助你(大部分)自动执行( ComSourceInterfacesAttribute )有一个体面的使用示例此处。
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.
这篇关于C#组件事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!