I am trying to access C# objects from another process than the one where they reside via COM. The C# objects are exposed to COM.
The problem is that when I call a method on those objects, the calls are executed loccally, not on the outproc COM server where I want the side effects of these calls to happen.
More precisely, here is what I do in the process trying to access the objects residing in the outproc COM server:
Type oType = Type.GetTypeFromProgID("myProgId");
var obj = Activator.CreateInstance(oType);
var result = oType.InvokeMember("GetObjForMyClassMethod",
BindingFlags.InvokeMethod, null, obj, null);
Then I cast result into the appropriate interface (IMyInterface
) of the C# class and when I make calls on methods of that interface they are executed remotely on the outproc COM server just like I expect.
The problem starts when I use that interface to retrieve other C# objects (I use a method IMyInterface.GetOtherObject()
that returns IOtherObjectInterface
).
When I make calls on IOtherObjectInterface
, they are executed locally, not on the outproc COM server so their side effects are lost (e.g if I call a SetValue method, the value doesn't arrive on the outproc server and it is lost).
Furthermore when I look under the debugger at runtime I see that the type of the interface for which the remote calls work is System._ComObject
whereas the type for the interface where the remote calls don't work is the actual type (IOtherObjectInterface
).
I've tried to replicate everything (all the attributes and other interfaces implemented) about the way IMyInterface
(the one for which remote calls work) is exposed to COM on IOtherObjectInterface
(the one for which remote calls don't work) but no luck.
What must done so that the calls I make on IOtherObjectInterface
are executed in the outproc server via RPC, not in the local process (the one where the calls are issued) where they are no use to anybody?
EDIT:
I have since found out that if I retrieve the object for which it doesn't work (IOtherObjectInterface
) using the same method that I use for the object for which it works ( oType.InvokeMember("GetOtherObjForMyClassMethod",....) it still doesn't work (the calls I make on the object are still not forwarded to the outproc COM server).
So the problem is with the class itself, not the method used to retrieve the objects.
I strongly suspect that this problem is somehow related to serialization even though both classes (working and not working) seem to implement serialization the same way...
EDIT2:
The part I don't get is the fact that the class for which this "COM remoting" works (the one for which I get System._ComObject when I retrieve the object outside the COM server) has the attribute "serializable". I thought the default for types with this attribute was marshal-by-value.