COM Factory error 0x80040154

1
CoCreateGuid( fIID );
TComObjectFactory.Create( ComServer, TMyCustomComClass, fIID, aClassName, aClassName, ciInternal, tmApartment );
fResult := CoGetClassObject( fIID, CLSCTX_SERVER, nil, IClassFactory, fFactory );
assert( fFactory <> nil, 'ERROR! fFactory is nil..' ); // fResult ~> 0x80040154 REGDB_E_CLASSNOTREG

TMyCustomComClass inherits from TComObject.

According to MSDN, such error occurs because the CLSID is not in the registry, but by Delphi's TComObjectFactory help, no registry is needed to do this, and the original code did the same and just worked.
I'm missing something, but what?

delphi
com
factory
asked on Stack Overflow Jan 14, 2015 by pilgrimdeveloper • edited Jan 14, 2015 by bummi

1 Answer

0

TComObjectFactory instance is not visible by Windows COM system until it is registered with CoRegisterClassObject API. When you create a new ActiveX library using Delphi - this API is called for each class factory present in library when library is loaded and it's DllGetClassObject method is called.

Here is modified version of your code, TMyCustomComClass COM object will be visible inside process that created it. This way you can register a new COM object visible in the current process only, without registering it in Windows registry. For example, you can instantiate this COM object using fIID inside WebBrowser control in your process:

var
  Factory: IClassFactory;
  Reg: LongInt;
...
  CoCreateGuid( fIID );
  Factory := TComObjectFactory.Create( ComServer, TMyCustomComClass, fIID, aClassName, aClassName, ciInternal, tmApartment );
  CoRegisterClassObject(fIID, Factory, CLSCTX_INPROC_HANDLER, REGCLS_MULTIPLEUSE, FReg);  // This line registers TComObjectFactory in Windows
  fResult := CoGetClassObject( fIID, CLSCTX_SERVER, nil, IClassFactory, fFactory );
  assert( fFactory <> nil, 'ERROR! fFactory is nil..' ); 
answered on Stack Overflow Mar 1, 2018 by Eugene Mala

User contributions licensed under CC BY-SA 3.0