all groups > dotnet interop > november 2007 >
You're in the

dotnet interop

group:

Cant use a COM Component from .NET if it was exported from .NET



Cant use a COM Component from .NET if it was exported from .NET Ben
11/29/2007 12:10:04 PM
dotnet interop: I have a COM server executable, written in .NET. I exported a type library
and registered it.

So now I can create an instance of the component using CreateInstance from
non .NET code. That works fine. I can also get hold of the instance by
calling GetObject from non .NET code.

So at this point I have the server running, but I also want to talk to it
from .NET clients.

If I try to add the the exported type library to a .NET client, I get an
error that I I can't add the type library because it was exported from a .NET
component and that I should add the assembly instead.

So I can add the assembly as a reference. If I then call
Marshall.GetActiveInstance, then I can not cast it using
Marshall.CreateWrapperOfType it gives me a type mismatch error because the
type is not derived from a COM object.

I want to be able to talk to the already running COM server instance from
another .NET application. But it wont let me because that COM instance was
written in .NET!

RE: Cant use a COM Component from .NET if it was exported from .NET Ben
11/29/2007 12:29:01 PM
So you can write COM components in .NET but you cant use such COM components
from .NET as COM components? Is that it?

[quoted text, click to view]
Re: Cant use a COM Component from .NET if it was exported from .NET Phil Wilson
11/29/2007 5:30:32 PM
I think that's correct, yes, you can't have a .NET client with a .NET COM
object (because you can just add the assembly reference to the client
anyway). Internally there'd be a RCW talking to a CCW, and I don't believe
that works.
--
Phil Wilson
[MVP Windows Installer]

[quoted text, click to view]

Re: Cant use a COM Component from .NET if it was exported from .NE Ben
11/30/2007 9:37:02 AM
Yes, but then you end up with a different instance of the component dont
you?!?!?

In this case I want to talk to the COM instance that is already running
because there is data in it.

Or is there a way in .NET to get hold of that instance and talk to it the
way you would talk to a .NET component?

Note we also want non .NET software to be able to talk to the same instance
of it, which they can only do by talking to it as a COM object by finding it
Re: Cant use a COM Component from .NET if it was exported from .NET Willy Denoyette [MVP]
11/30/2007 11:59:01 AM
[quoted text, click to view]

Yes, that's it. But why would you ever want to?, your .NET client can simply
use the .NET "components" as ... .NET components.

Willy.
Re: Cant use a COM Component from .NET if it was exported from .NE Willy Denoyette [MVP]
12/1/2007 6:17:34 PM
[quoted text, click to view]



A .NET client must call Marshal.GetActiveObject to connect to a running
instance, note that YOU are responsible for registering the instance in the
ROT at the server side, this is not done automatically.
Anyway, this is old hat, much better and a lot easier is to use
System.Enterprises and derive your class from ServicedComponent. All you
need to do is tag your assembly, interface and class with the right
attributes in order to run this class in a COM+ surrogate process as a
pooled singleton. Attributes you need to set are JustInTimeActivation,
ObjectPooling with both MinPoolSize and MinPoolSize set to 1.
This way the class object becomes the one and only instance clients can
connect with. Object life-time and state management can be implemented by
overriding the ServicedComponent methods Activate, Deactivate,
CanBePooled....
A .NET client can early bind using the metadata supplied by the interface,
while COM clients can early bind using the typelib created when registering
the .NET assembly with the COM+ catalog (using regsvcs.exe), there is no
need for the COM client to bind to a running object, as there is only one
single object of that type running, both .NET and COM clients will always
connect to the same instance.

Willy.



Re: Cant use a COM Component from .NET if it was exported from .NE Ben
12/3/2007 11:31:00 AM
Thanks. I shall have to Google that a bit to make sure I understand it all,
but I think its the right answer. <g>

[quoted text, click to view]
AddThis Social Bookmark Button