2009-07-17 19 views
1

我正在嘗試編寫一個將公開事件的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)以外的所有內容。我做錯了什麼?

回答

3

COM事件接收器對於外行來說是非常邪惡的。

的步驟基本上是

  • 創建傳出(源)接口
  • 實現一個 的IConnectionPointContainer和 的IConnectionPoint接口,使用 這些傳遞源接口的客戶端實現

不錯消息是在interop namespac e有一些屬性可以幫助您自動完成此操作(主要是)(ComSourceInterfacesAttribute)。它有一個體面的例子here

+0

謝謝,但這正是我在我的例子中做的,那不工作。 – Kyle 2009-07-17 10:20:17