Problem with triggering events in .NET DLL via COM from Delphi

I have a problem with COM objects and trigger events. I have:

  • A DLL written in C # .NET (3.5) that fires events
  • An application in Delphi5 that uses a DLL as a COM object.

So far so good. Events are as follows: - There are 2 events in the DLL. One of them is internal and is not COM-visible. The second - external and visible COM - the DLL has an OnChanged function, which is associated with an internal event and fires an external event. Thus, basically every time an internal event is a trigger, just like an external event. - this OnChanged function is also visible to COM and can be called from a Delphi application

I used this solution to make it all work, I can successfully bind the Delphi procedure to the .NET event, but there is a catch: - if I call OnChanged from the Delphi application, all peach - the Delphi function with the binding will be executed, and also (obviously) .NET function - if the DLL calls OnChanged from a callback function in .NET, the associated Delphi function will NOT execute.

Bottomline: if I fire an external event from Delphi, everything is fine. If the DLL fires an internal event, nothing will happen in Delphi (although, of course, an external event is triggered).

Any ideas are welcome!

+4
source share
1 answer

It is very difficult to answer this question without debugging the real code, but I will tell you about one problem that I encountered in the past with COM interoperability.

One possibility you might want to entertain is that the connection to the event is freed by the GC. When you attach to an event on an object that the event usually accepts a link to the attache and both objects will not be cleared by the GC, but when you have a COM object, the GC does not necessarily know about this link, and this may clear one of the objects. I came across this in the past with Office automation through .net. The way to solve this problem is to save the link to the COM object and attach to it.

For example, this is bad:

public class Foo { public Foo(ICOMObject obj) { obj.Changed += OnChanged; } private void OnChanged(object arg) { } } 

This will work just fine if obj is a .net object. The event will remain connected as long as one of the objects remains a link. However, this will not work with the COM object. With a COM object, the event seems to simply stop working at some point in time, usually quite quickly.

Instead, try the following:

 public class Foo { ICOMObject obj; public Foo(ICOMObject obj) { this.obj = obj; this.obj.Changed += OnChanged; } private void OnChanged(object arg) { } } 

I'm not sure if this is your problem, but maybe. Good luck

0
source

All Articles