C # component events?

I am trying to write a C # component that will expose events. The component must be imported by an unmanaged C ++ application. According to several tutorials, I came 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 an easy way:

 #import "COMTest.tlb" named_guids raw_interfaces_only 

Creates a tlh file. This tlh file contains everything except my event (OnTest). What am I doing wrong?

+1
source share
1 answer

COM Event Sinks are pretty evil for the uninitiated.

The steps are mainly

  • create outgoing (source) interface
  • Implement IConnectionPointContainer and IConnectionPoint Interfaces, use the source interface to implement the client

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

+3
source

Source: https://habr.com/ru/post/1310270/


All Articles