Delphi: reference to class instance generated by TAutoObjectFactory

We have an application that is also a COM server and can work both standalone and automatically using a COM client.

The COM class is declared like this:

type
  TCommandApp = class(TAutoObject, IConnectionPointContainer, ICommandApp)

      

And the initialization part looks like this:

TAutoObjectFactory.Create(ComServer, TCommandApp, Class_CommandApp, 
                          ciSingleInstance, tmApartment);

      

Creates an instance TCommandApp

. The question is, how can I refer to the instance TCommandApp

created TAutoObjectFactory

? (Call its methods, etc.).

+3


source to share


1 answer


If you want to instantiate TCommandApp

in the same way that COM does, then you must call the factory method CreateInstance

that comes from the interface IClassFactory

.

var
  Factory: IClassFactory;
  App: ICommandApp;

Factory := TAutoObjectFactory.Create(...);
OleCheck(Factory.CreateInstance(nil, ICommandApp, App));

      



Alternatively, you can call the constructor directly:

var
  Factory: TComObjectFactory;
  App: TCommandApp;

Factory := TAutoObjectFactory.Create(...);
App := TCommandApp.CreateFromFactory(Factory, nil);

      

+4


source







All Articles