Board index » delphi » Com Object within a Com Object

Com Object within a Com Object

I can create a simple OLE Automation Server w/ Delphi 4 easy enough.  What I
really want to do is be able to get a second com object from within the
server.

Something like:

IServer:= IServer.CoServer.Create;
ISecondObject:= IServer.GetObject(3);
ISecondObject.Initialize;

Can someone lead the way for me with this?
Do I just need to create another interface in the Type Lib Editor and
another CoClass constructor or what?  Would I call the CoClass constructor
in the GetObject method?

Thanks,
Phil

 

Re:Com Object within a Com Object


If I understand your question correctly, here's what you need to do...

Use the type library editor to create an interface definition for the
second object, say ISecondObject.

Then add a new property to the interface definition of the main server
object that returns the second object.

So your type library will look something like this...

IServer (interface definition)
  SecondObject: Type ISecondObject, Invoke Kind: Property Get

ISecondObject (interface definition)
  PropA
  PropB

Server (coClass definition)
  Implements: IServer

NOTE: You do not need to define coClass for SecondObject, as SecondObject
should not be independently instantiated (it is always created by accessing
the SecondObject property of IServer).

Next, you need implement the ISecondObject property in the server module
code. Something like this (the type library should have automatically
generated the function prototype)...

function TServer.Get_SecondObject: TSecondObject;
begin
  Result := TSecondObject.Create(ComServer.TypeLib, ISecondObject);
end;

Then, in the client module, the code is as simple as this:

var
  TheServer: IServer
  TheSecondObject: ISecondObject
begin
  TheServer := CoServer.Create
  TheSecondObject := TheServer.SecondObject;
end.

Phil Swenson <swen...@bigfoot.com> wrote in article
<703j96$l...@forums.borland.com>...

Quote
> I can create a simple OLE Automation Server w/ Delphi 4 easy enough.
What I
> really want to do is be able to get a second com object from within the
> server.

> Something like:

> IServer:= IServer.CoServer.Create;
> ISecondObject:= IServer.GetObject(3);
> ISecondObject.Initialize;

> Can someone lead the way for me with this?
> Do I just need to create another interface in the Type Lib Editor and
> another CoClass constructor or what?  Would I call the CoClass
constructor
> in the GetObject method?

> Thanks,
> Phil

Re:Com Object within a Com Object


You definitely need to create an Interface for your child object in the
TypeLib.  From your example, stay away from a method called Initialize, as
Delphi has already used that name in the base COM class TCOMObject for
initializing private members of the object, and it is called automatically
from the constructor.

Depending on whether your second object will ever need to be created outside
of a method of your Server object, you may or may not need a CoClass in the
TypeLib for the second object.  If you want to be able to say
CreateComObject(Class_SecondObject) sometimes and Server.GetObject(3) other
times, then you need a CoClass.  If it will only ever be Server.GetObject(3)
or some other server method that returns an ISecondObject, then you don't
need the CoClass.

If you do opt for making the SecondObject externally creatable, using a
CoClass, then the implementation will need to decend from TAutoObject.  If
it is internal only, then you can decend it from either TAutoObject or
TAutoIntfObject.  TAutoIntfObject doesn't need a factory, and is all around
a lighter way to implement an internal Automation object.

Hope that helps,
Stuart

Quote
Phil Swenson wrote in message <703j96$l...@forums.borland.com>...
>I can create a simple OLE Automation Server w/ Delphi 4 easy enough.  What
I
>really want to do is be able to get a second com object from within the
>server.

>Something like:

>IServer:= IServer.CoServer.Create;
>ISecondObject:= IServer.GetObject(3);
>ISecondObject.Initialize;

>Can someone lead the way for me with this?
>Do I just need to create another interface in the Type Lib Editor and
>another CoClass constructor or what?  Would I call the CoClass constructor
>in the GetObject method?

>Thanks,
>Phil

Other Threads