Код: Выделить всё
Process.Start("path\\to\\comserver.exe", "-embedding");
Thread.Sleep(1000);
Type serverType = Type.GetTypeFromCLSID(new Guid("..."));
dynamic c = Activator.CreateInstance(serverType);
Console.WriteLine(c.SomeProperty); // works
Console.WriteLine(c.SomeMethod(42)); // works
// c.SomeEvent += Handler; // works only with COM registration
var cpc = (IConnectionPointContainer) c;
Guid guidOfIServerEvents = ...;
cpc.FindConnectionPoint(ref guidOfIServerEvents, out IConnectionPoint? ppCP);
EventsSinkHelper sinkHelper = new();
ppCP.Advise(sinkHelper, out int cookie); // COMException 0x80040202 without COM registration
// ...
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("...")]
interface IServerEvents
{
[DispId(1)]
void SomeEvent();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("... (a different GUID)")]
public sealed class EventsSinkHelper : IServerEvents
{
public void SomeEvent() { /* ... */ }
}
Запуск всего в потоке STA ведет себя так же. Это очень удобно для тестирования, особенно для модульных тестов.
Как я могу заставить события работать?
Подробнее здесь: https://stackoverflow.com/questions/797 ... gistration
Мобильная версия