/*!
  Searches for the component identifier \a cid in the system component registry,
  loads the corresponding component server and queries for the interface \a iid.
  \a iface is set to the resulting interface pointer. \a cid can either be the
  UUID or the name of the component.

  The parameter \a outer is a pointer to the outer interface used
  for containment and aggregation and is propagated to the \link
  QComponentFactoryInterface::createInstance() createInstance() \endlink
  implementation of the QComponentFactoryInterface in the component server if
  provided.

  The function returns QS_OK if the interface was successfully instantiated, QE_NOINTERFACE if
  the component does not provide an interface \a iid, or QE_NOCOMPONENT if there was
  an error loading the component.

  Example:
  \code
  QInterfacePtr<MyInterface> iface;
  if ( QComponentFactory::createInstance( IID_MyInterface, CID_MyComponent, (QUnknownInterface**)&iface ) == QS_OK )
      iface->doSomething();
      ...
  }
  \endcode
*/
QRESULT QComponentFactory::createInstance( const QString &cid, const QUuid &iid, QUnknownInterface** iface, QUnknownInterface *outer )
{
    QSettings settings;
    settings.insertSearchPath( QSettings::Windows, "/Classes" );
    bool ok = FALSE;
    QString cidStr = cid;
    QRESULT res = QE_NOCOMPONENT;

    QUuid uuid( cidStr ); // try to parse, and resolve CLSID if necessary
    if ( uuid.isNull() ) {
	uuid = settings.readEntry( "/" + cid + "/CLSID/Default", QString::null, &ok );
	cidStr = uuid.toString().upper();
    }

    if ( cidStr.isEmpty() )
	return res;

    QString file = settings.readEntry( "/CLSID/" + cidStr + "/InprocServer32/Default", QString::null, &ok );
    if ( !ok )
	return res;

    QComLibrary *library = new QComLibrary( file );
    library->setAutoUnload( FALSE );

    QComponentFactoryInterface *cfIface =0;
    library->queryInterface( IID_QComponentFactory, (QUnknownInterface**)&cfIface );

    if ( cfIface ) {
	res = cfIface->createInstance( uuid, iid, iface, outer );
	cfIface->release();
    } else {
	res = library->queryInterface( iid, iface );
    }
    QLibraryInterface *libiface = 0;
    if ( library->queryInterface( IID_QLibrary, (QUnknownInterface**)&libiface ) != QS_OK || !qApp ) {
	delete library; // only deletes the object, thanks to QLibrary::Manual
    } else {
	libiface->release();
	library->setAutoUnload( TRUE );
	liblist()->prepend( library );
    }
    return res;
}
Exemple #2
0
void build(const char *filename,const char *lib = 0, const char *obj = 0) 
{
   if (obj!=0 && strlen(obj) ) {
      TString s = gSystem->GetMakeSharedLib();
      TString r(" $ObjectFiles ");
      r.Append(obj);
      s.ReplaceAll(" $ObjectFiles",r);
      //gDebug = 5;
      gSystem->SetMakeSharedLib(s);
   }
   if (lib && strlen(lib)) {
      TString liblist(lib);
      TObjArray *libs = liblist.Tokenize(" ");
      TIter iter(libs);
      TObjString *objstr;

      TString s = gSystem->GetMakeSharedLib();
      TString what("..nothing..");
      if (s.Contains("$DepLibs")) {
         what = " $DepLibs";
      } else {
         what = " $LinkedLibs";
      }
      TString libstolink(" ");
      while ( (objstr=(TObjString*)iter.Next()) ) {
         gSystem->Load(objstr->String());
         TString libfile( gSystem->GetLibraries(objstr->String(),"DSL",kFALSE));
         libstolink.Append(libfile);
         libstolink.Append(" ");
      }
      libstolink.Append(what);
      s.ReplaceAll(what,libstolink);
      gSystem->SetMakeSharedLib(s);
   }
#ifdef __CLING__
   TString r;
#ifdef  ClingWorkAroundCallfuncAndInline
   r.Append(" -DClingWorkAroundCallfuncAndInline ");
#endif
#ifdef  ClingWorkAroundCallfuncAndVirtual
   r.Append(" -DClingWorkAroundCallfuncAndVirtual ");
#endif
#ifdef ClingWorkAroundJITandInline
   r.Append(" -DClingWorkAroundJITandInline ");
#endif
#ifdef ClingWorkAroundCallfuncAndReturnByValue
   r.Append(" -DClingWorkAroundCallfuncAndReturnByValue ");
#endif
#ifdef ClingWorkAroundNoPrivateClassIO
   r.Append(" -DClingWorkAroundNoPrivateClassIO ");
#endif
   if (r.Length()) {
      r.Append(" $IncludePath");
      TString s = gSystem->GetMakeSharedLib();
      s.ReplaceAll(" $IncludePath",r);
      gSystem->SetMakeSharedLib(s);
   }
#endif
      
#if defined(_WIN32) && !defined(__CYGWIN__)
   TString fname(filename);
   if (filename[0]=='/') {
     // full path name we need to turn it into a windows path
     fname = gSystem->GetFromPipe(TString::Format("cygpath -w %s",filename));
   }
   fprintf(stderr,"from %s to %s\n",filename,fname.Data());
   int result = gSystem->CompileMacro(fname,"kc");
#else
   int result = gSystem->CompileMacro(filename,"kc");
#endif
   if (!result) gApplication->Terminate(1);
}