1

I can use an out-of-process COM server that is not registered with C# dynamic, and this works fine, but I cannot get events to work.

My code basically is something like this:

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() { /* ... */ }
}

This works perfectly when the COM server is registered, but when it is not registered, calling Advise results in a COMException with error 0x80040202.

Running everything in an STA thread behaves the same way.

I understand that this scenario is not supported and may stop working at any time, but it is extremely handy for testing, especially for unit tests.

How could I make events work?

7
  • Read following : opcti.com/callback-fail-dcom-error.aspx Commented Aug 12 at 12:26
  • @jdweng Thanks. How I understand it, this is not directly related to my question, but it might be a hint that the error code indicates something like a generic "cannot communicate with (D)COM object receiving the events". Please correct me if I'm wrong. Commented Aug 12 at 12:59
  • In windows events are registered and put on a chain. The events are processed as part of the Timer Tick. So when an event is received the Timer Tick looks for the application that registered the event and switches control to the event routine. So either an event was not returned or the event is not properly registered. Windows contains tunnels in the OS that are not very well documented. I think you are failing in a tunnel. It may be the GUID is wrong. The tunnel may be checking the GUID. Commented Aug 12 at 16:15
  • Do you have a reproducing project to be sure to understand what you're doing? Commented Aug 12 at 18:03
  • @SimonMourier Of course, and I already thought of adapting it to a COM object that is available on every Windows installation or can be easily installed and that provides events that can be easily triggered and that can be easily registered and unregistered, but so far didn't come up with such a COM object. Commented Aug 13 at 8:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.